PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

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

 March 03, 2022     backend, eloquent, laravel-5, php     No comments   

Issue

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?.


Solution

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;


Answered By - Aniket Das
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing