【问题标题】:Generate post id in url在 url 中生成帖子 ID
【发布时间】:2018-08-16 02:59:34
【问题描述】:

有没有一种方法可以生成 id 配置文件 id。例如,我想要 localhost/lary/quickstart/public/ 而不是 localhost/lary/quickstart/public/profile/35/edit profile/habb778/edit这是我的代码

public function edit($companyname)
{
    $pro=Profile::find($companyname);
    return view('layouts.profileedit')->with('pro',$pro);
}

<a href="{{url('profileedit',$use->companyname)}}" style="color:#F88B22;"><span class="glyphicon glyphicon-pencil"></span></a> 

路线:

Route::resource('profile','ProfileController',['except'=>'edit']);
Route::get('profileedit/{companyname}','ProfileController@edit');

【问题讨论】:

    标签: laravel laravel-routing


    【解决方案1】:

    您可以通过挂钩RouteServiceProviderboot 方法来自定义路由中模型的解析逻辑

    Route::bind('profile', function($value) {
        if (is_int($value)) {
            return Profile::findOrFail($value);
        } else {
            return Profile::where('string_column', $value)->firstOrFail();
        }
    });
    

    其中的一些变体将起作用。然后你可以调用如下:

    {{route('profile.edit', $use->string_column}}
    

    string_column 替换为您想要将字符串与表格中的任何列进行比较。

    【讨论】:

    • 在尝试您的答案之前,我将向您展示我所做的。我要更新我的问题
    • 我编辑了它我在这里收到一条错误消息,说正在尝试获取非对象的属性 'companyname'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    相关资源
    最近更新 更多