Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

php - LARAVEL 6.x = Trying to get property 'id' of non-object

I wish to be able to edit my users through the admin panel but this returns the following error to me:

Trying to get property 'id' of non-object

it will be an error in my view with the call of the variable ID if I change it I have the same thing with my variable name.

I use the users table and in no other place in my code do I have problems

help me please

URI : /role-edit/{id}

View : 

    <div class="container">
        <div class="row">
            <div class="col-md-12">

                <div class="card">
                    <div class="card-header">
                        <h4>Edit register roles</h4>
                    </div>
                    <div class="card-body">
                            <form action="/role-register-update/{{ $users->id }}" method="POST">
                                {{ csrf_field() }}
                                {{ method_field('PUT') }}

                                <div class="form-group">
                                    <label>Name</label>
                                    <input type="text" name="name" value="{{ $users->name }}" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>Give role</label>
                                    <select name="type" class="form-control">
                                        <option value="admin">Admin</option>
                                        <option value="vendor">Vendor</option>
                                        <option value="">None</option>
                                    </select>
                                    <button type="submit" class="btn btn-success">Update</button>
                                    <a href="/role-register" class="btn btn-danger">Cancel</a>
                                </div>
                            </form>
                    </div>
                </div>

            </div>
        </div>
    </div>



Controller :

class DashboardController extends Controller
{
    public function registered()
    {
        $users = User::all();
        return view('admin.registeradmin')->with('users', $users);
    }

    public function edit(Request $request,$id)
    {
        $users = User::findOrFail($id);
        return view('admin.edit-register')->with('users',$users);
    }

    public function update(Request $request, $id)
    {
        $users = User::findOrFail($id);

        $users->name = $request->input('name');
        $users->usertype = $request->input('type');
        $users->update();

        return redirect('/role-register')->with('status', 'You data is update');
    }

    public function destroy($id)
    {
        $users = User::where('id', $id);

        if ($users != null)
        {
            $users->delete();
            return redirect('/role-register')->with('status', 'User is correctly deleted !');
        }
        return redirect('/role-register')->with('status', 'User is not correctly deleted !');
    }


}


Routes :

Route::get('/', function () {
    return view('pages.home');
});

Route::get('/aboutus', function () {
    return view('pages.aboutus');
})->name('aboutus');

Auth::routes();

Route::get('profile', 'UserProfileController@show')->middleware('auth')->name('profile.show');
Route::post('profile', 'UserProfileController@update')->middleware('auth')->name('profile.update');

Route::get('/home', 'HomeController@index')->name('home');

Route::group(['middleware' => ['auth', 'admin']], function () {

    Route::get('/dashboard', function () {
        return view('admin.dashboard');
    });

    Route::get('/role-register', 'AdminDashboardController@registered');

    Route::get('/role-edit/{id}', 'AdminDashboardController@edit');

    Route::put('/role-register-update/{id}', 'AdminDashboardController@update');

    Route::delete('/role-delete/{id}', 'AdminDashboardController@destroy');

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add dd($users) in the edit function of your controller. If you get the data, add the following to the form action form action:

{{route('routename',['id'=>$users->id])}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...