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": "russel.llewellyn@example.org"
+"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
- Get comma separated email from input array
- Create model with migration and seeder
- How to get specific columns using with method in laravel Eloquent relationship
- Import/Use Storage facade in laravel
- Setup laravel project with docker
- Laravel csrf token mismatch for ajax POST Request
- Laravel delete all rows older than 30 days
- How to check column value of a record is null or not in laravel
- How to restore multiple records after soft-deletes in Laravel
- Use of undefined constant laravel
- Use withCount() to get total number of records with relationship
- Class 'App\Rules\Hash' not found in Laravel
- Extra Filter Query on Relationships in Laravel
- Get laravel version
- Multiple Level eager loading in Laravel
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to customize pagination view in laravel
- Method Illuminate\Http\Request::validated does not exist
- How to return a column with different name in Laravel
- How to pass link from controller to view in laravel on ajax call
- Redirect to another view from controller in laravel
- Save or update pivot table data with additional column in Laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- How to create event and listener in laravel ?
- Order by multiple columns in Laravel