Reputation: 575
I am trying to display data from a database based on the logged in user in laravel.
If there is data for one user everything works well but if another user adds data to the table I get the error Cannot use object of type stdClass as array
Here is how I am getting the data in controller
public function viewBookings() {
$allProducts = Booking::get()->where('client', Auth::user()->name);
$products = json_decode(json_encode($allProducts));
foreach ($products as $key => $val) {
$service_name = Service::where(['id' => $val->service])->first();
$service_fee = Charge::where(['id' => $val->charge])->first();
$service_status = Status::where(['id' => $val->status])->first();
$products[$key]->service_name = $service_name->name;
$products[$key]->service_fee = $service_fee->total;
$products[$key]->service_status = $service_status->name;
}
return view('client.booking.view_bookings')->with(compact('products'));
}
The error is indicated at this line
$products[$key]->service_name = $service_name->name;
I have tested it by deleting the data for the other user and all works fine but If I return the other users data to the table I get that error
Upvotes: 0
Views: 463
Reputation:
I would suggest removing this line
$products = json_decode(json_encode($allProducts));
and simply use
$products = $allProducts;
since what you are doing with the json_decode is the one creating the problems
Upvotes: 1