BrokenHeart
BrokenHeart

Reputation: 11

How to get result from database based on the user with laravel

I started coding laravel and everything was fine till I wanted to get the result based on the user, how to do it, what do I need to do?.

Upvotes: 0

Views: 39

Answers (1)

Aniket Das
Aniket Das

Reputation: 398

You can use where clause.

Suppose you wanna fetch all the blogs of a user.

like:

Blog::where('user_id', Auth::user()->id)->get();

// Auth::user()->id returns the logged in user's id.

Or

create a relation in User model

App\Models\User
use App\Models\Blog;

 public function blogs()
 {
     return $this->hasMany(User::class, 'user_id', 'id');
 }

Then

Auth::user()->blogs;

Upvotes: 2

Related Questions