sanainfotech sanainfotech - 20 days ago 7
PHP Question

how to pass multiple table data to view in laravel 5.2

I have 3 tables in database,

--User



--Profile



--Education



User model

public function profiledetails()
{
return $this->hasOne('App\Profiledetails');
}

public function educationHasOne()
{
return $this->hasOne('App\Education');
}


Profiledetails model

public function user()
{
return $this->belongsTo('App\User');
}


Education model

public function user()
{
return $this->belongsTo('App\User');
}


I am able to store the data.

I would like to view all table data in to one single webpage
I use the below code to make that happen.

public function index()
{
$user = Auth::user()->id;
$profile = array(
User::find($user)->profiledetails,
User::find($user)->educationHasOne
);
return view('profile.index', ['profile' => $profile]);
}


When is use dd($variablename), I am able to see all required field I needed,

Now, I would like to know how to pass all this data to view in single page.

Answer

You can pass an array to the view, like so:

return view('profile', ['profile' => $profile]);

In the view, you can access $profile.

If you need this data for each, it's recommend to use view composers. Nice documentation + examples can you find here: https://laravel.com/docs/5.1/views#passing-data-to-views