mardi 27 octobre 2015

Laravel 5: Redirect "blog/{id}" to "blog/{slug}" - Trying to get property of non-object error

I'm trying to redirect users that go to blog/{id} to blog/{slug} with the slug being the slug of the id they entered.

What I am doing is not working. I dd{$article->slug} and get the correct string, yet when I try to redirect I get "Trying to get property of non-object" error.

I'm using route/model binding. This is my RouteServiceProvider:

    $router->bind('blog', function($slug)
    {
        if ($slug) {
            if(is_numeric($slug)) {
                $article = Article::findOrFail($slug);

                return redirect('blog/' . $article->slug);
            }

            return Article::with('images')->where('slug', $slug)->first();
        }
    });

This is my show method in controller:

public function show(Article $article)
{
    return view('blog.show', compact('article'));
}

Any ideas would be greatly appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire