
Get posts belongs to a specific user in Laravel
Get posts belongs to a specific user in Laravel
Hello everyone, in this post we will look at how to solve "Get posts belongs to a specific user in Laravel" in programming.
You can get posts belongs to a specific user in Laravel. Sometimes we have to get all records from posts table which are created by a specific or logged in user. In that case it will help you to find the records from posts table.-
Get all posts created by logged in user with whereBelongsTo method in Laravel 8
$user = App\Models\User::find(Auth::user()->id); $posts = App\Models\Post::whereBelongsTo($user)->get(); dd($posts); //Or - You can specify the relationship name manually by providing it as the second argument. You can create method with name of user using belongsTo() relation in post model. $user = App\Models\User::find(1); $posts = App\Models\Post::whereBelongsTo($user, 'user')->get(); dd($posts); //Code for demo: Route::get('/get-posts-by-user', function(){ $user = App\Models\User::find(1); $posts = App\Models\Post::whereBelongsTo($user)->get(); dd($posts); });
Output:
Illuminate\Database\Eloquent\Collection {#1631 ▼ #items: array:17 [▶] }
This code snippet will help you to get all the records from the posts table which are created by a specific user in Laravel.
You have to create a foreing key (user_id) in posts table which is the primary key of the users table in database.
You have to also specify the belongsTo() relationship in post model with name user or you can use different name like author as per your requirment.
user relationship using belongsTo() method in Post model
public function user(){ return $this->belongsTo('App\Models\User'); }
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to create static page in Laravel
- Insert Comma Separated Values in laravel
- Include External CSS and JS file in Laravel
- Get the post details if it has at least one comment in comments table
- Laravel route redirect not working
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Undefined property: stdClass::$title
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Get products with number of orders in Laravel
- How to upload image in laravel 8
- How to prevent host header attack in Laravel
- Skip first n record and display rest records in laravel view
- Get previous date data in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Send id with route Laravel
- Redirect to another view from controller in laravel
- Laravel create default admin user
- Laravel get all records with pagination
- Laravel API response format
- Extract only time from datetime in laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Insert current date time in a column using Laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Laravel clone model
- FirstOrCreate() Not Inserting Model