Home > Enterprise >  I am in a basic level of laravel9 and trying to save() or update() in a database and this error happ
I am in a basic level of laravel9 and trying to save() or update() in a database and this error happ

Time:01-16

this is controller code and this is route and this is the blade code:

<div  >
  <div >
    <div >
      <p >Update Members</p>
    </div>
    <div >
      <form  action="/update" method="POST">
        @csrf
        <div >
          <label for="id" >Id</label>
          <input type="hidden" value="{{$data['id']}}"   id="id">
        </div>
        <div >
          <label for="name" >Name</label>
          <input type="text"  value="{{$data['Name']}}"  id="name">
        </div>
        <div >
          <label for="email" >Email</label>
          <input type="text"  value="{{$data['Email']}}" id="email">
        </div>
        <div >
          <label for="address" >Address</label>
          <input type="text"  value="{{$data['Address']}}" id="address">
        </div>
        <div >
          <label for="phone" >Phone</label>
          <input type="text"  value="{{$data['Phone']}}" id="phone">
        </div>
        
        <div >
          <button type="submit" >Update</button>
          <hr >
        </div>
      </form>
      
    </div>
  </div> 
</div>

I am tried to save this four columns in the database name email address phone then this error happen. please answer me quickly if you know?

I am trying to save data in this columns

CodePudding user response:

One possibility is that, in the controller, $data is null. You can make the code fail by changing "find" to "findOrFail". The code will return a NotFoundException. Then you can investigate why $data is null.

Read https://laravel.com/docs/9.x/eloquent#not-found-exceptions.

CodePudding user response:

the problem is probably in your update function it should be like this:

function update(Request $req){
$data = Member::find($req->id);

$data->Name = $req->Name;
$data->Email= $req->Email;
$data->Address = $req->Address;
$data->Phone = $req->Phone;
$data->save();

return redirect()->input();
}

in your update function you have changed the upper case to a lower case which will cause the Attempt to assign property "name" on null Error

  • Related