Reputation: 23
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
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