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
- Post model with title and body in laravel 8
- How to display user profile after login in laravel
- How to get all posts which contains comments in laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to remove package from laravel
- Wheredate in laravel not working
- Conditional validation in laravel
- Composer create project laravel/laravel example app
- Display message with session flash using bootstrap alert class in laravel
- Comment .env file in laravel
- Check if Relationship Method Exists in Laravel
- How to fetch single row data from database in laravel
- Pagination in laravel
- Create model with migration and seeder
- How to automatically update the timestamp of parent model in Laravel
- Symlink(): No such file or directory
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Create a record if not exist in laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Class 'App\Rules\Hash' not found in Laravel
- How to get the random value form a specific column in laravel ?
- How to check data inserted or deleted in pivot after toggle method
- Laravel create table migration with model
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Cast Array to an Object in Controller and then pass to view in laravel