0

由于下面的代码 URL 看起来像:/downloadpage/34 我想让 URL 对 SEO 友好,例如:/downloadpage/slug

在表“媒体”中有一个列slug。如何更改媒体通过 id 搜索的代码,但在 URL 中显示 slug?

routes.php 文件:

Route::get('/downloadpage/{id}', [
    'as' => 'downloadpage', 'uses' => 'DownloadController@index'

下载Controller.php 文件:

public function index($id){
    $images   = DB::table('media')->where("id", "=", $id)->get();
    $popular  = DB::table('media')->orderBy('count_download', 'DESC')->take(5)->get();
    $mostlike = DB::table('media')->orderBy('count_like', 'DESC')->take(5)->get();
    $alsolike = DB::table('media')->orderByRaw("RAND()")->where("id", "!=", $id)->take(3)->get();
    $settings = Setting::first();
    $previous = DB::table('media')->where('id', '<', $id)->max('id');
    $next     = DB::table('media')->where('id', '>', $id)->min('id');
    $imgshare = DB::table('media')->where("id", "=", $id)->first();
    return view('download.downloadpage', ['images'=>$images , 'popular'=>$popular, 'mostlike'=>$mostlike, 'alsolike'=>$alsolike, 'settings'=>$settings, 'previous'=>$previous, 'next'=>$next, 'imgshare'=>$imgshare]);
}
4

1 回答 1

1

类似的东西。

路线:

Route::get('/downloadpage/{slug}', [
    'as' => 'downloadpage', 'uses' => 'DownloadController@index'

控制器:

public function index($slug){
    $images   = DB::table('media')->where("slug", "=", $slug)->first();
    $popular  = DB::table('media')->orderBy('count_download', 'DESC')->take(5)->get();
    $mostlike = DB::table('media')->orderBy('count_like', 'DESC')->take(5)->get();
    $alsolike = DB::table('media')->orderByRaw("RAND()")->where("id", "!=", $id)->take(3)->get();
    $settings = Setting::first();
    $previous = DB::table('media')->where('id', '<', $images->id)->max('id');
    $next     = DB::table('media')->where('id', '>', $images->id)->min('id');
    return view('download.downloadpage', ['images'=>$images , 'popular'=>$popular, 'mostlike'=>$mostlike, 'alsolike'=>$alsolike, 'settings'=>$settings, 'previous'=>$previous, 'next'=>$next, 'imgshare'=>$images]);
}

我将$images = DB::table()...->first()行更改为以 结尾->first(),因此您没有得到数组。您可能还需要更改视图中的某些内容。

我强烈建议您阅读基本的 Laravel 文档来进行路由,它清楚地解释了这一点。并浏览Eloquent文档并将您的DB调用转换为模型。

于 2018-04-02T19:13:08.823 回答