Reputation:
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');
Upvotes: 1
Views: 4250
Reputation: 936
You shouldn't write {{ $edd[0]->id }}
instead of this use:
{{ $edd->id }}
Upvotes: 3
Reputation: 54841
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.
Upvotes: 4