Laravel 5.5 model pivot table count

Posted 1 year ago by afoysal

I have 3 tables. users,gallery and gallery_blog.gallery_blog has gallery_id and blog_id.gallery_blog is a pivot table. gallery has user_id and name. users has id.One User has many gallery. One gallery has many blog.

I need a query to know how many blogs in a gallery along with gallery name of current user.

I wrote below query

use App\User as user_gallery;    
$galleries = user_gallery::find(Auth::user()->id)->gallery;

I have below code in User model.

public function gallery() {   
        return $this->hasMany('App\Models\Gallery');  

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.