Of course, this returns a Collection
$rentings =$house->rentings()->where('id', '!=', $renting->id)->get();
Should you be updating the Route-Model bound $renting instead?
$renting->update($request->validated());
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
My controller
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Renting;
use App\Models\House;
use App\Enums\HouseStatus;
use App\Enums\HouseLocation;
use Illuminate\Support\Carbon;
use App\Http\Requests\RentingsStoreRequest;
use Illuminate\Support\Facades\Storage;
class RentingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$rentings = Renting::all();
return view('admin.rentings.index', compact('rentings'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$houses = House::where('status',HouseStatus::Available)->get();
return view('admin.rentings.create',compact('houses'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(RentingsStoreRequest $request)
{
$house = House::findOrFail($request->house_id);
if($request->ternant_number > $house->ternant_number){
return back()->with('warning','PLease choose house based on number of ternants');
}
$request_date = Carbon::parse($request->ren_date);
foreach ($house->rentings as $ren) {
if($ren->ren_date->format('Y-m-d') == $request_date->format('Y-m-d')){
return back()->with('warning','This house is already reserved for this date');
}
}
Renting::create($request->validated());
return to_route('admin.rentings.index')->with('success', 'Renting completed successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Renting $renting)
{
$houses = House::where('status', HouseStatus::Available)->get();
return view('admin.rentings.edit', compact('renting', 'houses'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(RentingsStoreRequest $request, Renting $renting)
{
$house = House::findOrFail($request->house_id);
if($request->ternant_number > $house->ternant_number){
return back()->with('warning','PLease choose house based on number of ternants');
}
$request_date = Carbon::parse($request->ren_date);
$rentings =$house->rentings()->where('id', '!=', $renting->id)->get();
foreach ($rentings as $ren) {
if($ren->ren_date->format('Y-m-d') == $request_date->format('Y-m-d')){
return back()->with('warning','This house is already reserved for this date');
}
}
$rentings->update($request->validated());
return to_route('admin.rentings.index')->with('success', 'Renting updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Renting $renting)
{
$renting = Renting::findOrFail($id);
$renting->delete();
return to_route('admin.rentings.index')->with('success', 'Renting deleted successfully');
}
}
my model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Renting extends Model
{
use HasFactory;
protected $fillable = [
'first_name',
'last_name',
'tel_number',
'email',
'house_id',
'ren_date',
'ternant_number',
];
protected $dates = [
'ren_date'
];
public function house(){
return $this->belongsTo(House::class);
}
}
my view
<x-admin-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class=" flex justify-items-end m-2 p-2">
<a href="{{ route('admin.rentings.create') }}" class="px-4 py-2 bg-indigo-500 hover:bg-indigo-600 rounded-lg text-white">New Rentings</a>
</div>
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Email
</th>
<th scope="col" class="px-6 py-3">
Date
</th>
<th scope="col" class="px-6 py-3">
House
</th>
<th scope="col" class="px-6 py-3">
Ternants
</th>
<th scope="col" class="px-6 py-3">
Action
</th>
</tr>
</thead>
<tbody>
@foreach ($rentings as $renting)
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{ $renting->first_name }} {{ $renting->last_name }}
</th>
<td class="px-6 py-4">
{{$renting->email }}
</td>
<td class="px-6 py-4">
{{$renting->ren_date }}
</td>
<td class="px-6 py-4">
{{$renting->house->name}}
</td>
<td class="px-6 py-4">
{{$renting->ternant_number}}
</td>
<td class="px-6 py-4">
<div class="flex space-x-2">
<a href="{{ route('admin.rentings.edit', $renting->id) }}"
class="px-6 py-4 bg-indigo-500 hover:bg-red-600 rounded-lg text-white">Edit</a>
<form class="px-4 py-2 bg-red-500 hover:bg-red-700 rounded-lg text-white"
method="POST"
action="{{ route('admin.categories.destroy', $renting->id) }}"
onsubmit="return confirm('Are you sure ?');">
@csrf
@method('DELETE')
<button type="submit" class="inline-flex items-center px-4 py-2">Delete</button>
</form>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</x-admin-layout>
@Farirai for just one Renting record, right? You are using the $rentings variable in the update action to check if there are existing records for the same date (assuming a different date was selected); so I suggested an exists query instead of the Collection:
public function update(RentingsStoreRequest $request, Renting $renting)
{
$house = House::findOrFail($request->house_id);
if($request->ternant_number > $house->ternant_number){
return back()->with('warning','PLease choose house based on number of ternants');
}
/* Check if the new date is already taken */
$request_date = Carbon::parse($request->ren_date);
$existingRenting = $house->rentings()
->where('id', '!=', $renting->id)
->whereDate('ren_date', $request_date->format('Y-m-d'))
->exists();
if($existingRenting){
return back()->with('warning','This house is already reserved for this date');
}
$renting->update($request->validated());
return to_route('admin.rentings.index')->with('success', 'Renting updated successfully');
}
Please or to participate in this conversation.