Home > Back-end >  How to get two tables data in one single view in Laravel 8
How to get two tables data in one single view in Laravel 8

Time:02-18

I have two tables one is division and another is user_designations and I want to get these two tables' data on one single page. I'm new to Laravel so I can't find any way. I have tried the below method but it's not working

This is my controller code

public function createUser(){
        $data['divisions'] = Division::get(['division_name', 'id']);
        return view('admin.create_user', $data);
    }

    public function viewDesignation(){
        $designations = userDesignation::all();
        return view('admin.create_user', compact('designations'));
    }

This is my view page code

<select name="" id="">
    <option value="">Select Devision</option>
    @foreach ($divisions as $data)
    <option value="{{$data->id}}">{{$data->division_name}}</option>
</select>


<select name="" id="">
    <option value="">Select Designation</option>
    @foreach($designations as $data)
    <option value="{{$data->id}}">{{$data->designation_name}}</option>
</select>

When I tried this method I got an error, Please see attached the image Error

CodePudding user response:

you have to pass all data through the same method you are calling from route

public function createUser(){
        $data['divisions'] = Division::get(['division_name', 'id']);
        $data['designations'] = userDesignation::all();
        return view('admin.create_user', $data);
    }

CodePudding user response:

read about eloquent-relationships: https://laravel.com/docs/5.1/eloquent-relationships

  • Related