I’m having a problem with a CRUD route. On my method edit in my controller UserGroupsController, the parameter $userGroup is empty.
UserGroupsController
class UserGroupsController extends Controller { public function edit(UserGroup $userGroup) { if (Auth::check()) { $users = User::pluck('name','id')->all(); $groups = Group::pluck('name','id')->all(); return view('users-group.edit', compact('userGroup','users','groups')); }else{ return redirect('login'); } } }
edit.blade.php
<div class="panel panel-default"> <div class="panel-heading">Edit</div> <div class="panel-body"> {{ $userGroup }} {!! Form::model($userGroup,['method' => 'PATCH', 'route' => ['users-group.update', $userGroup->id], 'id' => 'user_group-form', 'class' => 'form-horizontal'] ) !!} {{ csrf_field() }} <input type='hidden' name='id' value='{!! $userGroup->id !!}'> @include('users-group.form',array('submitButton' => 'Edit User Group')) {!! Form::close() !!} </div>
form.blade.php
<div class="form-group"> <label for="user_id" class="col-md-4 control-label">User</label> <div class="col-md-6"> {!! form::select('user_id', $users, $userGroup->user_id,['class' => 'form-control']) !!} </div> </div> <div class="form-group"> <label for="group_id" class="col-md-4 control-label">Group</label> <div class="col-md-6"> {!! form::select('group_id', $groups, $userGroup->group_id,['class' => 'form-control']) !!} </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> {!! Form::submit($submitButton) !!} </div> </div>
Routes:
Route::group(['middleware'=>['web']], function () { Route::resource('users','UsersController'); Route::resource('users-group','UserGroupsController'); Route::resource('companies','CompaniesController'); });
php artisan route:list :
| | POST | users-group | users-group.store | AppHttpControllersUserGroupsController@store | web | | | GET|HEAD | users-group/create | users-group.create | AppHttpControllersUserGroupsController@create | web | | | PUT|PATCH | users-group/{users_group} | users-group.update | AppHttpControllersUserGroupsController@update | web | | | DELETE | users-group/{users_group} | users-group.destroy | AppHttpControllersUserGroupsController@destroy | web | | | GET|HEAD | users-group/{users_group} | users-group.show | AppHttpControllersUserGroupsController@show | web | | | GET|HEAD | users-group/{users_group}/edit | users-group.edit | AppHttpControllersUserGroupsController@edit
dd($userGroup):
UserGroup {#199 ▼ #fillable: array:2 [▼ 0 => "user_id" 1 => "group_id" ] #connection: null #table: null #primaryKey: "id" #keyType: "int" #perPage: 15 +incrementing: true +timestamps: true #attributes: [] #original: [] #relations: [] #hidden: [] #visible: [] #appends: [] #guarded: array:1 [▶] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] +exists: false +wasRecentlyCreated: false }
In the file edit.blade.php, I print the variable $userGroup, but returns “[ ]”. And I have the same code to UsersControllers, but there works.
When I submit the form, I got this error:
MethodNotAllowedHttpException in RouteCollection.php
Sorry for the english, I hope that you understand my problem and I can give more information. thanks
Advertisement
Answer
An edit
method usually gets an id. To fix your problem, remove the type from the header and get the model in your method:
class UserGroupsController extends Controller { public function edit($id) { $userGroup = UserGroup::findOrFail($id); if (Auth::check()) { $users = User::pluck('name','id')->all(); $groups = Group::pluck('name','id')->all(); return view('users-group.edit', compact('userGroup','users','groups')); }else{ return redirect('login'); } } }