简体   繁体   中英

laravel 5.8: slug 404 Not Found

I want to use slug, but when I click and jump to a particular post, 404 Not Found shows up.

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>

You have defined your model to use the slug key for the Implicit Route Model Binding. The route you have defined, results/{post} , takes 1 parameter, post . You are passing an id and a slug to the route helper which is making it use the id as the parameter:

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

You don't need to be passing the Post's id for this route, you want to be using the slug for the parameter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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