jeudi 3 septembre 2015

How do you get the user_id of an article in Laravel 5?

So I'm following L5 Fundamentals, trying to improve on things but it's getting a bit complicated especially that you can do the same thing in many different ways. I keep forgetting which is which.

I am trying to check if the article the user is trying to edit belongs to him, if not, I'll redirect him with a message. I'm applying this to my edit() method in ArticlesController

I was able to get the id of the logged in user but I couldn't get the user_id in articles so I can compare them.

This is what I have so far, as you can see it's commented as it's not the result I want and this is the closest I got to getting anything out of it.

Also should I implement this condition on the controller or the view? and why?

public function edit(Article $article) {
    $user = Auth::id();
    //$articleUserId = $article->user()->getResults();
    dd($user);

    if($user) {
        $tags = Tag::lists('name', 'id');
        //$article = Article::findOrFail($id);

        return view('blog/edit')->with('article', $article)
            ->with('tags', $tags);
    } else {
        return 'you are not logged in';
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1UvESLs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire