Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to make a crud in Laravel. Im done with the create and read functions but update is still a huge issue and while it does work it wont update at all the message i got is
Laravel Attempt to read property "id" on null''



Here is my updaterequest

{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $bedrijf = $this->route('bedrijf');
        return [
            'name' => 'string|min:2|max:75|required|unique:bedrijven,name,'.$bedrijf->id
        ];
    }
}


My updatecontroller

public function update(BedrijfUpdateRequest  $request, Bedrijf $bedrijven)
   {
       $bedrijven->name = $request-> name;
       $bedrijven->save();
       return redirect()->route('bedrijven.index')->with ('meassage', 'bedrijven gewijzigd');

   }


the route

Route::get('admin/bedrijven/update',[BedrijfController::class, 'update'])
->name('bedrijven.update');


the page

@extends('layouts.layout')
@section ('content')
<h2 class="my-4 text-4xl font-semibold dark:text-gray-400">
				Bewerk Bedrijven
			</h2>

@if($errors-> any())
 @foreach ($errors->all() as $error)
<li>{{$error}}</li>

 @endforeach
 @endif

<div class="w-full max-w-xs">
  <form class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4"
  action="{{route('bedrijven.update', ['bedrijven'=>$bedrijven->id])}}" method="POST">
  @method('PUT')    
  @csrf
    <div class="mb-4">
      <label class="block text-gray-700 text-sm font-bold mb-2" for="name" >
        Name
      </label>
      <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"  @error('name') border-red-500 @enderror 
      id="name" type="text" placeholder="name" name="name" value = "{{ old('name', $bedrijven->name)}}">
</div>
<div class="mb-4">
      <label class="block text-gray-700 text-sm font-bold mb-2" for="body" >
        Description
      </label>
      <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"  @error('name') border-red-500 @enderror 
      id="body" type="textfield" placeholder="body" name="body" value ="{{old('body',$bedrijven->body)}}">
</div>
    <div class="flex items-center justify-between">
      <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" 
      type="submit">
       Voeg toe
      </button>
</div>
</form>
@endsection


What I have tried:

I tried to change the variables, i recreated the page twice
Posted
Updated 28-May-23 10:26am
Comments
Richard MacCutchan 24-Jun-22 10:56am    
"I tried to change the variables, i recreated the page twice"
You need to find the line of code that causes the error. Use your debugger to step through the part of the code where it occurs. Once you get to the failing line you should be able to see which reference is the one that is null.

What this means is that your code is assuming an operation worked and returned a valid object, when it didn't.

For example:
... $this->route('bedrijf');

Did route find the 'bedrijf' index string? Did that index hold a valid object? Maybe, maybe not. Your code never checks to see if what was returned was valid before it tried to use (call a method or property) on whatever was returned.

Never assume a lookup like that worked.
 
Share this answer
 
In my case, in my model I had defined a relationship as follows:

PHP
public function nationality(){
    return $this->belongsTo(Country::class, 'nationality', 'countryid');
}


After trying many things and having a nearly-identical relationship working, I changed the name of the method to nation

PHP
public function nation(){
    return $this->belongsTo(Country::class, 'nationality', 'countryid');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900