Site Admin
Site Admin Founder of MeaningArticles

Laravel - How to get Data From Two Models?

Hello Dev.

In this web-article, i will explain you get data from two models in laravel. you could easily and simply get records from two different models in laravel.

we've two way to get records from different models, the first one is use two foreach loop in the blade file, and other one is merge models in the controller and get records using only a single variable.

So let's start the lesson...



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Parentuser;
use App\Models\Student;

class TestController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index()
        $parent = Parentuser::get();
        $student = Student::get();
        $data = $parent->concat($student);



Illuminate\Database\Eloquent\Collection {#281 ?
  #items: array:4 [?
     0 => App\Models\Parentuser {#297 ?}
     1 => App\Models\Parentuser {#298 ?}
     2 => App\Models\Student {#1015 ?}
     3 => App\Models\Student {#1014 ?}


Table 1: 


Table 2: 

I hope it's assist you, thanks for visit my article if you like my article then share with your friends on social media platform.
Happy Coding.....