laravel 5.8:未找到 404 段塞

laravel 5.8: slug 404 Not Found

我想使用 slug,但是当我点击并跳转到特定的 post 时,404 未找到显示。

URL is working well so I don't figure it out why I cannot see the result.

web.php

Route::get('results/{post}', 'ResultsController@show')->name('posts.show');

post.php

public function getRouteKeyName()
{
    return 'slug';
}

ResultsController.php

public function show(Post $post)
{
    $recommended_posts = Post::latest()
                        ->whereDate('date','>',date('Y-m-d'))
                        ->where('category_id','=',$post->category_id)
                        ->where('id','!=',$post->id)
                        ->limit(7)
                        ->get();


    $posts['particular_post'] = $post;
    $posts['recommended_posts'] = $recommended_posts;

    return view('posts.show',compact('posts'));
}

table

Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('image');
        $table->unsignedBigInteger('category_id');
        $table->string('title');
        $table->string('slug');
        $table->string('place');
        $table->string('map');
        $table->date('date');
        $table->string('organizer');
        $table->string('organizer_link');
        $table->timestamp('published_at')->nullable();
        $table->text('description');
        $table->timestamps();
    });

PostsController.php

 public function store(CreatePostsRequest $request)
{
    //upload the image to strage
    //dd($request->image->store('posts'));
    $image = $request->image->store('posts');

    //create the posts
    $post = Post::create([
        'image' => $image,
        'category_id' => $request->category,
        'title' => $request->title,
        'slug' => str_slug($request->title),
        'place' => $request->place,
        'map' => $request->map,
        'date' => $request->date,
        'organizer' => $request->organizer,
        'organizer_link' => $request->organizer_link,
        'published_at' => $request->published_at,
        'description' => $request->description
    ]);

result.blade.php

<a href="{{ route('posts.show', [$post->id,$post->slug]) }}" class="title-link">{{ str_limit($post->title, 20) }}</a>

您已将模型定义为使用隐式路由模型绑定的 slug 键。您定义的路由 results/{post} 采用 1 个参数 post。您正在将一个 id 和一个 slug 传递给路由助手,这使得它使用 id 作为参数:

route('posts.show', [$post->id, $post->slug])

您不需要为此路线传递 Post 的 ID,您希望使用 slug 作为参数:

route('posts.show', $post->slug);
// or
route('posts.show', ['post' => $post->slug]);