Join WhatsApp ChannelJoin Now

Laravel 8 Two Models get Data Example

Hi Dev,

Today, i we will show you laravel 8 two models get data example. This article will give you simple example of laravel 8 two models get data example. you will learn laravel 8 two models get data example.

We have two examples to gate the models data, The first one is to use two foreach loops in the Blade file, and the second one is to Merge these models in the controller and get data using only a single variable. this Example you can use with laravel 6, laravel 7 and laravel 8 version as well.

So let’s follow few step to create example of laravel 8 two models get data example.

Example:

<?php

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);
        dd($data);
    }

}

Output:

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 ?}

  ]

}

Recommended Posts