简体   繁体   中英

How To Solve This Trying to get property of non-object Error In Laravel

I'm creating a web site. And I have created a registration page. I want to update my details.

But, It gives me this error and I have also uploaded a picture of errors below. - Error Picture

ErrorException (E_ERROR) Trying to get property of non-object (View: D:\\wamp64\\www\\FinalProject\\resources\\views\\AdminUpdate.blade.php)

I used dd($edd); and it gave me correct details. But, when I try with below codes it gives me that above error.

How can I Fix this ??

Here is my AdminPanel.blade.php

<table class="table table-bordered">

<tr>
<td> Name </td>
</tr>


@foreach($data as $value )
<tr>
<td> {{ $value->username }} </td>
<td> <a href="edit/{{ $value->id }}"><input type="submit" name="update" value="Update" class="btn-primary"></a> </td>
</tr>
@endforeach
</table>

Here is my AdminPanelController.php

public function edit($id)
{
$edd = User::find($id);
//dd($edd);
      return view('AdminUpdate', ['edd' => $edd]);

}

    public function adminedit($id, Request $request, User $user)
{
    // Add Validation

    $users = $user->find($id);
    $users->username = $request->get('username');
    $users->email = $request->get('email');
    $users->save();

    return redirect()->back();
}

Here is my AdminUpdate.blade.php

<form action="edit/{{ $edd[0]->id }}" method="post" enctype="multipart/form-data">

    {{ method_field('PUT') }}
    {{ csrf_field() }}

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{$edd[0]->username}}" placeholder="Enter Your Username" required>
  </div>

    <div class="form-group">
    <label>Email : *</label>
    <input type="email" class="form-control" name="email" value="{{$edd[0]->email}}" placeholder="Enter Your Username" required>
  </div>

  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{$edd[0]->password}}" placeholder="Enter Your Password" required>
  </div>

  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>

  <input type="submit" class="btn btn-primary" value="Update User">
                    </form>

Here are my Routes.

Route::get('/edit/{id}', 'AdminPanelController@edit');

Route::post('/edit/{id}', 'AdminPanelController@adminedit');

User::find() gives you a single object , not array.

So, in blade just use

$edd->id

Same applies to other instances of $edd : username, email, password.

As a sidenote: you can add a check if user is really found by id and if not - show 404 page, for example.

您不应该写{{ $edd[0]->id }}来代替这种用法:

{{ $edd->id }}

Change { $edd[0]->id }} line to

{{ $edd->id }}

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