Imran hadid
Imran hadid

Reputation: 23

Collect data from multiple table from database and facing problem to show that data in blade

I'm new to laravel. I'm collecting data from three database table for a logged-in user.

$Employee = Employee::where("id", $id)->get();
$Salary = Salary::where("emp_id", $id)->get();
$SalaryDetails = SalaryDetails::where("emp_id", $id)->get();
$data = array(
    'Employee'      => $Employee,
    'Salary'        => $Salary,
    'SalaryDetails' => $SalaryDetails
);
return view('pages.manager.salaryDetails')->with($data);

When I send those data to the blade it says

Property ... does not exist on this collection instance.

Here if I use foreach the problem is solved. The problem arises when I call it by {{$Employee->fullname}}. I don't want to use foreach here because I'm collecting data for one single user. Is there any way to show data in the blade without using foreach?

Upvotes: 2

Views: 29

Answers (1)

Roman Meyer
Roman Meyer

Reputation: 2872

To get single user use find instead of get:

$Employee = Employee::find($id);

When you use where to filter some data and then getting it via get method, you will get a collection. In your case you will get collection with one Employee.

In that case you can also do like that:

$Employee = Employee::where("id", $id)->first();

Method first returns first element in collection.

Upvotes: 1

Related Questions