简体   繁体   中英

I am in a basic level of laravel9 and trying to save() or update() in a database and this error happen Attempt to assign property "name" on null

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

<div class="container mt-5 mb-5 w-25" >
  <div class="card">
    <div class="card-header">
      <p class="fs-1">Update Members</p>
    </div>
    <div class="card-body">
      <form class="row g-3" action="/update" method="POST">
        @csrf
        <div class="col-12">
          <label for="id" class="form-label">Id</label>
          <input type="hidden" value="{{$data['id']}}" class="form-control "  id="id">
        </div>
        <div class="col-12">
          <label for="name" class="form-label">Name</label>
          <input type="text" class="form-control" value="{{$data['Name']}}"  id="name">
        </div>
        <div class="col-12">
          <label for="email" class="form-label">Email</label>
          <input type="text" class="form-control" value="{{$data['Email']}}" id="email">
        </div>
        <div class="col-12">
          <label for="address" class="form-label">Address</label>
          <input type="text" class="form-control" value="{{$data['Address']}}" id="address">
        </div>
        <div class="col-12">
          <label for="phone" class="form-label">Phone</label>
          <input type="text" class="form-control" value="{{$data['Phone']}}" id="phone">
        </div>
        
        <div class="col-12">
          <button type="submit" class="btn btn-primary">Update</button>
          <hr class="border border-primary border-3 opacity-75">
        </div>
      </form>
      
    </div>
  </div> 
</div>

I am trying to save this four columns in the database name email address and phone then this error happened. please answer me quickly if you can?

I am trying to save data in these columns

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM