【问题标题】:Laravel Get Slug from DatabaseLaravel 从数据库中获取 Slug
【发布时间】:2019-08-15 14:09:31
【问题描述】:

我正在尝试使用最新版本的 laravel 构建一个类似博客的应用程序。我试图弄清楚如何从每篇文章的数据库中提取一个 slug,然后将其路由到所有工作正常。

我已经搞定了,但是如果您使用 slug 来查看,内容将不会显示在文章上。

localhost/articles/1 - 工作正常,页面上显示内容(标题等)

localhost/articles/installing-example - 这可行,但内容错误

当您尝试使用数据库中的 slug 导航到页面时会发生这种情况:Trying to get property 'title' of non-object (View: C:\xampp\htdocs\blogtest\resources\views\articles\show.blade.php)

此行出错:<h1><?php echo e($articles->title); ?></h1>

app/http/controllers/ArticlesController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Article;

class ArticlesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $articles =  Article::all();
        return view('articles.index')->with('articles', $articles);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $articles = Article::find($id);
        return view('articles.show')->with('articles', $articles);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}

app/Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $table = 'articles';
    public $primaryKey = 'id';
    public $timestamps = true;
}

路由/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::resource('articles', 'ArticlesController');

资源\视图\文章\show.blade.php

@extends('layouts.master')

@section('content')

    <h1>{{$articles->title}}</h1>

@endsection

数据库

任何帮助和建议将不胜感激。

【问题讨论】:

    标签: php laravel controller routes slug


    【解决方案1】:
    Trying to get property 'title' of non-object (View: C:\xampp\htdocs\blogtest\resources\views\articles\show.blade.php)
    

    暗示$articles 不是一个对象,如果你转储它,它应该输出null - 完全正确。

    find 函数用于使用您的主键查找行,而您的主键不是您的slug 列,因此无法找到记录。

    您需要设置一个路由来接受 {slug},然后根据 slug 您需要进行以下更改:

    $articles = Article::find($id);
    

    到,

    $articles = Article::where('slug', $slug)->first();
    

    并确保 $slug 是实际的 slug 而不是 id 列值。

    【讨论】:

      【解决方案2】:

      这是因为您从数据库中获取了空数据。

      原因是,你在 id 字段中传递 slug 所以

      localhost/articles/installing-example

      你传递了 id = 安装示例,因此没有找到 id,所以你没有得到任何数据。

      所以对于这个 将您的 slud 转换为原始形式,不使用 id 进行搜索,而是使用您的 slug 字段进行搜索。

      让我们假设你制作的标题。 标题是installing example 所以当你得到蛞蝓时,把它变成原始形式 然后用原样搜索

      $articles = Article::where('titile',$original)-&gt;first();

      【讨论】:

        【解决方案3】:

        试试这个

        没有蛞蝓

        路线

        Route::resource('articles', 'ArticlesController');
        

        /article/{id}

        控制器

        public function show($id)
        {
            $articles = Article::first($id);
            return view('articles.show', compact('articles'));
        }
        

        刀片

        @extends('layouts.master')
        
        @section('content')
            @if(!is_null($articles))
                <h1>{{$articles->title}}</h1>
            @endif
        @endsection
        

        使用蛞蝓

        路线

        Route::get('/articles/{slug}', ['as'=>'articles.by.slug', 'uses'=> 'ArticlesController@showArticles']);
        or
        Route::get('/articles/{slug}', 'ArticlesController@showArticles')->name('articles.by.slug');
        

        /article/{slug}

        控制器

        public function showArticles($slug)
        {
            $articles = Article::where('slug', $slug)->first();
            return view('articles.show', compact('articles'));
        }
        

        刀片

        @extends('layouts.master')
        
        @section('content')
            @if(!is_null($articles)) //or @if($articles)
                <h1>{{$articles->title}}</h1>
            @endif
        @endsection
        

        【讨论】:

          【解决方案4】:

          您需要在模型中设置路由键

          // Article model
          public function getRouteKeyName(){
              return 'slug';
          }
          

          【讨论】:

            猜你喜欢
            • 2015-08-11
            • 1970-01-01
            • 2021-07-02
            • 2017-08-13
            • 1970-01-01
            • 2015-11-26
            • 2018-01-13
            • 2022-11-21
            • 2019-11-12
            相关资源
            最近更新 更多