
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
- Update if exist else insert new record in laravel
- How to get single column value in laravel
- Split an Eloquent Collection by half in Laravel
- Laravel append URI in route
- Pagination in laravel
- How to add script on specific view file in laravel while extending layout
- Cast Array to an Object in Controller and then pass to view in laravel
- Laravel insert query not working
- Get today records in Laravel
- On delete set foreign id column value null using migration in laravel 8
- Laravel route redirect not working
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to get query string value in laravel
- Order by multiple columns in Laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Validation errors for multiple forms on same page Laravel
- Ajax GET request in laravel
- Add class to body in laravel view
- Get Array of IDs from Eloquent Collection
- Use withCount() to get total number of records with relationship
- Send id with route Laravel
- How to display validation error in laravel
- Laravel save object to database
- Laravel get count with where condition
- Call to a member function update() on null