I got this error when I tried to update data users. I have browse to other source but don't found answer from my problem.
ErrorException
Creating default object from empty value
http://localhost:8000/ec-admin/users/updateact/{$users->id}
This is my user controller.
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\User;
use File;
public function updateact($id, Request $request)
{
// Menyiapkan data gambar yg diupload ke variable $file
$file_name = $request->hidden_image;
$file = $request->file('file');
if($file != '')
{
// Validasi data
$this->validate($request,[
'name' => 'required|alpha',
'email' => 'required|email',
'level' => 'required|alpha',
'password' => 'required',
'file' => 'required|file|image|mimes:jpeg,jpg,png|max:1048'
]);
$file_name = time()."_".$file->getClientOriginalName();
// Isi dengan nama folder tempat kemana file diupload
$upload_directory = 'p_users';
$file->move($upload_directory, $file_name);
// Hapus file
$gambar = User::where('id',$id)->first();
File::delete(public_path('p_users/'.$gambar->file));
}
else
{
// Validasi data
$this->validate($request,[
'name' => 'required|alpha',
'email' => 'required|email',
'level' => 'required|alpha',
'password' => 'required'
]);
}
// Update data
$users = User::find($id);
$users->name = $request->name;
$users->email = $request->email;
$users->level = $request->level;
$users->password = $request->password;
$users->file = $file_name;
$users->save();
return redirect('/ec-admin/users')->with('usrsupdateno', 'Data update Successfully!');
}
And this is my user form view. Please help because I new in laravel.
<form role="form" action="/ec-admin/users/updateact/{$users->id}" method="post" enctype="multipart/form-data">
@csrf
@method('put')
<input type="hidden" name="id" value="{{$users->id}}"><br/>
<div >
<div >
<div >
<div >
<label for="#">Name</label>
<input type="text" name="name" id="#" value="{{$users->name}}" autofocus>
</div>
</div>
<div >
<div >
<label for="#">Email</label>
<input type="email" name="email" id="#" value="{{$users->email}}">
</div>
</div>
<div >
<div >
<label for="#">Level</label>
<select name="level">
<option disabled selected>-Choose-</option>
<option value="user" {{($users->level === 'user') ? 'Selected' : ''}}>User</option>
<option value="admin" {{($users->level === 'admin') ? 'Selected' : ''}}>Admin</option>
</select>
</div>
</div>
<div >
<div >
<label for="#">Password</label>
<input type="password" name="password" id="#" value="{{$users->password}}">
</div>
</div>
<div >
<div >
<label for="">Profile Pic</label>
<div>
<img src="{{ url('p_users/'.$users->file) }}" width="100">
</div>
<input type="file" name="file">
<input type="hidden" name="hidden_image" value="{{$users->file}}">
</div>
</div>
</div>
</div>
<!-- /.box-body -->
<div >
<button type="submit" >Submit</button>
</div>
</form>
This is my route, I hope this help.
Route::get('/ec-admin/users/update/{id}', 'UsersController@update')->name('usrupdate');
Route::put('/ec-admin/users/updateact/{id}', 'UsersController@updateact');
CodePudding user response:
in your form action change {$users->id} to {{$users->id}} it would work
but for best practice you should assign a name to your route then use the route helper
CodePudding user response:
This error is triggered when yourObj
is NULL
or not yet initialized:
That's because your $users = User::find($id);
return NULL ,
Your form URL is incorrect and so Eloquent can't find your expected User.
Best practice
Change your **PUT** route to :Route::put('/ec-admin/users/updateact/{id}', 'UsersController@updateact')->name('act.update');
Then in your <form>
change action="/ec-admin/users/updateact/{$users->id}"
to
action="{{ route('act.update',$users->id) }}"
Simple solution
Change your form action from action="/ec-admin/users/updateact/{$users->id}"
to
action="/ec-admin/users/updateact/{{ $users->id }}"