
How to get data from two tables in laravel
How to get data from two tables in laravel
With this article, we will examine several different instances of how to solve the "How to get data from two tables in laravel".
You can get data from two tables in laravel using concat() method on collections or array, using joins on two tables and relationship method.-
Concat two model data in laravel 8
Route::get('/concat-table', function(){ $users = App\Models\User::get(); $posts = App\Models\Post::get(); $data = $users->concat($posts); dd($data); });
Output :
^ Illuminate\Database\Eloquent\Collection {#1344 ▼
#items: array:11 [▼
0 => App\Models\User {#1362 ▶}
1 => App\Models\User {#1363 ▶}
2 => App\Models\User {#1364 ▶}
3 => App\Models\User {#1365 ▶}
4 => App\Models\User {#1366 ▶}
5 => App\Models\Post {#1369 ▶}
6 => App\Models\Post {#1370 ▶}
7 => App\Models\Post {#1371 ▶}
8 => App\Models\Post {#1372 ▶}
9 => App\Models\Post {#1373 ▶}
10 => App\Models\Post {#1374 ▶}
]
} -
Get two table data using join in laravel
--PATH routes\web.phpRoute::get('/get-data', function(){ $data = DB::table('users') ->join('posts', 'users.id', '=', 'posts.user_id') ->select('users.*', 'posts.title') ->get(); dd($data); });
Output :
^ Illuminate\Support\Collection {#1355 ▼
#items: array:6 [▼
0 => {#1363 ▼
+"id": 5
+"name": "Niko Quitzon"
+"email": "[email protected]"
+"email_verified_at": "2021-08-02 00:38:40"
+"password": "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
+"remember_token": "SlVghemhw7"
+"created_at": "2021-08-02 00:38:40"
+"updated_at": "2021-08-02 00:38:40"
+"title": "Iste quis blanditiis harum."
}
1 => {#1358 ▶}
2 => {#1360 ▶}
3 => {#1364 ▶}
4 => {#1357 ▶}
5 => {#1361 ▶}
]
}Raw SQL query :
select `users`.*, `posts`.`title` from `users` inner join `posts` on `users`.`id` = `posts`.`user_id`You have to create two tables (users, posts). You have to define the relationship between these two tables using foreign key (user_id) in the posts table with the primary key in the users table. Now you can get the data from tables by using join().
Here we have data with column name id, name, email, email_verified_at, password, remember_token, created_at, updated_at from users table and data with column title from posts table.
-
Get users data with posts using relationship in laravel
//# Create hasMany() relation with name posts in user model //app\Models\User.php public function posts(){ return $this->hasMany('App\Models\Post'); } //# Get posts table data with users Route::get('/get-users-with-post', function(){ $data = App\Models\User::with('posts')->get(); dd($data); });
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
- Route group with URI prefix using middleware and route name prefixes
- Create a record if not exist in laravel
- Redirect to previous page or url in laravel
- Laravel 9 route group with controller
- How to upload multiple images after preview in laravel using cropper js
- Get comma separated email from input array
- How to customize or Change validation error messages
- How to add dynamic page title in Laravel view
- How to check record exist or not in relationship table
- Post model with title and body in laravel 8
- Create record with unique slug in laravel
- Get latest record by created at in Laravel
- Route not defined in Laravel
- How to validate URL with https using regex in laravel
- If condition in Laravel 9
- External link not working in laravel blade
- Target class [HomeController] does not exist
- How to get images from AWS s3 and display in Laravel blade
- How to start websocket server in laravel
- How to insert ckeditor data into database in Laravel?
- On delete set foreign id column value null using migration in laravel 8
- Get content from web URL in laravel
- Insert data with form validation using ajax in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- Pass variable from blade to controller Laravel