Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Farirai's avatar

Method Illuminate\Database\Eloquent\Collection::update does not exist.

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>

0 likes
17 replies
tykus's avatar

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());
Sinnbeck's avatar

Here you get multiple items

        $rentings =$house->rentings()->where('id', '!=', $renting->id)->get();
//and then try to update on the collection. Not possible
        $rentings->update($request->validated());
//instead you could do
         $house->rentings()->where('id', '!=', $renting->id)->update($request->validated());
Farirai's avatar

@Sinnbeck i thought i could customize the create action and use it to update because im storing the values of the record i want to edit can you help me on the action to update

Sinnbeck's avatar

@Farirai Update what exactly? I have shown you how to update all rentings that are attached to the house, and not the $renting.. Is that not what you wanted?

tykus's avatar

If you're getting $rentings only to check the date, then you can simplify with this query:

$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());
Farirai's avatar

@tykus i am getting renting's so i can update any input field may it be the name or surname

tykus's avatar

@Farirai that's not what you're doing in the update Controller action.

Farirai's avatar

@tykus can you help its difficult for me to upadte this rentings because of datetime

Farirai's avatar

@tykus what i want to achieve is to update the entries i have made after the create action for example if i want to cahange the name and save the updated name

tykus's avatar
tykus
Best Answer
Level 104

@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');
}
Farirai's avatar

@tykus this worked thank you can you explain what i did so i dont gace this problem again

tykus's avatar

@Farirai you confused the $rentings variable where you wanted $renting!

The original problem was the attempt to call update on the $rentings Collection variable rather than the Renting model instance (coming from the Route-Model binding).

Mark the thread closed if you're all set

Farirai's avatar

my edit 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.houses.index') }}" class="px-4 py-2 bg-indigo-500 hover:bg-indigo-600 rounded-lg text-white">Rentings Index</a>
          </div>
          <div class="m-2 p-2 bg-slate-100 rounded">
              <div class="space-y-8 divide-y divide-gray-200 w-1/2 mt-10">
                  <form  method="POST" action="{{ route('admin.rentings.update',$renting->id) }}" enctype="multipart/form-data">
                    @csrf
                    @method('PUT')
                  
                    <div class="sm:col-span-6">
                      <label for="first_name" class="block text-sm font-medium text-gray-700">First Name</label>
                      <div class="mt-1">
                          <input type="text" id="first_name" name="first_name" value="{{$renting->first_name}}" 
                                 class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('first_name') border-red-400 @enderror" />
                      </div>
                      @error('first_name')
                      <div class="text-sm text-red-400">{{ $message }}</div>
                @enderror
                  </div>
                  <div class="sm:col-span-6">
                    <label for="last_name" class="block text-sm font-medium text-gray-700">Last Name</label>
                    <div class="mt-1">
                        <input type="text" id="last_name" name="last_name" value="{{$renting->last_name}}" 
                               class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('last_name') border-red-400 @enderror" />
                    </div>
                    @error('last_name')
                    <div class="text-sm text-red-400">{{ $message }}</div>
              @enderror
                </div>
                <div class="sm:col-span-6">
                  <label for="email" class="block text-sm font-medium text-gray-700">Email</label>
                  <div class="mt-1">
                      <input type="text" id="email" name="email" value="{{$renting->email}}" 
                             class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('email') border-red-400 @enderror" />
                  </div>
                  @error('email')
                  <div class="text-sm text-red-400">{{ $message }}</div>
            @enderror
              </div>
              <div class="sm:col-span-6">
                <label for="tel_number" class="block text-sm font-medium text-gray-700">Phone Number</label>
                <div class="mt-1">
                    <input type="text" id="tel_number" name="tel_number" value="{{$renting->tel_number}}" 
                           class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('tel_number') border-red-400 @enderror" />
                </div>
                @error('tel_number')
                <div class="text-sm text-red-400">{{ $message }}</div>
          @enderror
            </div>
            <div class="sm:col-span-6">
              <label for="ren_date" class="block text-sm font-medium text-gray-700">Reservation Date</label>
              <div class="mt-1">
                  <input type="datetime-local" id="ren_date" name="ren_date" value="{{$renting->ren_date}}" 
                         class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('ren_date') border-red-400 @enderror" />
              </div>
              @error('ren_date')
              <div class="text-sm text-red-400">{{ $message }}</div>
        @enderror
          </div>
          <div class="sm:col-span-6">
            <label for="ternant_number" class="block text-sm font-medium text-gray-700">Ternant Number</label>
            <div class="mt-1">
                <input type="number" id="ternant_number" name="ternant_number" value="{{$renting->ternant_number}}" 
                       class="block w-full appearance-none bg-white border border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5 @error('ternant') border-red-400 @enderror" />
            </div>
            @error('ternant_number')
            <div class="text-sm text-red-400">{{ $message }}</div>
      @enderror

        </div>       
          <div class="sm:col-span-6 pt-5">
            <label for="status" class="block text-sm font-medium text-gray-700">House</label>
            <div class="mt-1">
                <select id="house_id" name="house_id" class="form-multiselect block w-full mt-1">
                    @foreach ($houses as $house)
                        <option value="{{ $house->id }}" @selected($house->id == $renting->house_id)>{{ $house->name }} ({{ $house->ternant_number }} Ternants)</option>
                    @endforeach
                </select>
            </div>
        </div>

        @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                <li>
                    {{ $error}}
                </li>  
                @endforeach
            </ul>
        </div>
        @endif

        <div class="mt-6 p-4">
            <button type="submit" 
            class="py-2 px-4 border bg-indigo-500 hover:bg-indigo-700 rounded-lg text-white">
                update
                </button>
        </div>

      
                    
                  </form>
               
                </div> 
          </div>
      </div>
  </div>
</x-admin-layout>
tykus's avatar

@Farirai okay, this is all fine. The original problem was the attempt to call update on the $rentings Collection variable rather than the Renting model instance (coming from the Route-Model binding). I have given you the solution for this.

You could consider validating instead; to ensure that the date is not already taken - see the unique validation rule.

1 like

Please or to participate in this conversation.