简体   繁体   English

Laravel CRUD删除路由参数

[英]Laravel CRUD delete route parameters

Id' like to use a CRUD Controller to handle the admin profile. 我想使用CRUD控制器来处理管理员个人资料。 I created the Controller and edited my routes/web.php like this. 我创建了Controller并像这样编辑了route / web.php。

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'as' => 'admin.', 'middleware' => ['auth:admin']], function(){
    Route::resource('profile', 'ProfileController')->only(['index', 'edit', 'update', 'destroy']);
});

This creates the following routes. 这将创建以下路线。

admin/profile -> admin.profile.index
admin/profile/{profile} -> admin.profile.destroy
admin/profile/{profile} -> admin.profile.update
admin/profile/{profile}/edit -> admin.profile.edit

Since I get the admin_id through the session I don't need to get it through the URL via the profile parameter. 由于我是通过会话获取admin_id的,因此不需要通过profile参数通过URL获取它。 I know that I could just ignore the parameter, but it makes the URL unnecessary long. 我知道我可以忽略该参数,但这会使URL不再冗长。 Is there a chance to delete the {profile} parameter from the routes, so that my routes are like teacher/profile/edit and not like teacher/profile/12345678/edit ? 是否有机会从路线中删除{profile}参数,以使我的路线像教师/个人资料/编辑,而不是教师/个人资料/ 12345678 / edit

You can use separate routes instead of resource for your individual operation. 您可以为自己的操作使用单独的路由而不是资源。 Like 喜欢

For index operation you can use:-
Route::get('profile', 'ProfileController@index');
For destroy operation you can use:-
Route::delete('profile', 'ProfileController@destroy');
For edit operation you can use:-
Route::put('profile', 'ProfileController@edit');

And you can send your admin_id in input or from session. 您可以在输入中或从会话中发送admin_id。 Hope it will work for you :) 希望它对你有用:)

  • Yes you can remove that perticular parameters from route. 是的,您可以从路线中删除该垂直参数。
  • but instead on this you have to send profile data through post variable 但是相反,您必须通过post变量发送配置文件数据
  • This is not possible to send post data in every redirection. 不可能在每个重定向中发送发布数据。

I will suggest you to use 'Slugs' instead of profile id 我建议您使用“ Slugs”代替个人资料ID

Yes, you can remove the {profile} part if you call the profile needed first in the controller like such: 是的,如果您首先在控制器中调用所需的配置文件,则可以删除{profile}部分,例如:

public function edit(){
    $teacher = Auth::user();
    return view('teacher.profile.edit', compact('teacher'));
}

And inside the view page (teacher/profile/edit), you can call the data like such: 在查看页面(教师/个人资料/编辑)中,您可以像这样调用数据:

<form method="post" action="{{ route('teacher.profile.update', $teacher->id) }}">
    @method('PATCH')
    @csrf
    ID: <input type="text" name="id" value={{ $teacher->id}} />
    <button type="submit">Update</button>
</form>

I hope this help. 希望对您有所帮助。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM