How to get data from two tables in laravel

Updated at 19-Sep-2021, By samar

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.php
    Route::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);
    });
    

Back to code snippet queries related laravel