lundi 26 février 2018

HasMany Object or Eloquent: Which is better?

I have the following controller for blog

public function show($slug)
{
    $post = Blog::where('slugs', '=', $slug)->first();

    $vars['pageTitle'] = Config::get('site.pageTitle') . $post['title'];

    // The breadcrumbs... needs to be repopulated every page
    $vars['breadCrumbs'] = [[
        'url'   => action('SimpleController@index'),
        'title' => 'CovertDEV'
    ],[
        'url'   => action('BlogController@index'),
        'title' => 'Blog'
    ],[
        'url'   => route('blog_post', ['slug' => $slug]),
        'title' => $post['title']
    ]];

    $vars['blog'] = $post;

    // The following line is what the question is about
    $vars['comments'] = $post->Blog_comments->groupBy('comment_id');

    return view('blog', $vars);
}

I'm retrieving the comments for the post and displaying it on the page. Everything is working perfectly... but then I tried to use my relationship instead... (that changes that line to...)

$vars['comments'] = $post->blog_comments()->get()->groupBy('comment_id');

And that works just as well. My post isn't big, just a simple 'Lorem Ipsum' test text and 7 comments to test the feature so I don't see any difference in load times between the two methods.

Or are they both the same way? The previous way looks better, but I'm not sure if I'm implementing the ORM as intended, if not then which way is better?

But I'm new to Laravel (this is my first framework) so I don't know... which way is better/faster/more efficient? Maybe there's an even better/more efficient way to accomplish this that I don't know of yet.

Sorry if this seems obvious. I want to get this out of the way before continuing on the project... don't want to be redoing everything after finding out a better way to tie pieces of information together.

Thanks in advance!



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

Aucun commentaire:

Enregistrer un commentaire