fael24's avatar

Trying to get property 'id' of non-object error (Asset management system)

This error has been haunting me for days, the whole project has been fully functional except for the create feature. When I'm logged on as a regular user, i can view the index of the clothes without any problem. But when logged on as an admin, i can only view the index when i create a new clothe for the first time (which regular user doesn't have access to). After that first time, the second time I add a new clothe, this error pops up, and I'm losing my mind trying to figure this out. I'm just starting out with laravel and my project deadline is tomorrow :( Hope somebody can help..

This is the code inside my index function on the controller:

    $clothe = Clothe::all();

    return view('clothes.index')->with('clothe',$clothe)->with('brand',Brand::all())->with('stock',Stock::all());

The brand pertains to the brand model which is the brands of clothing, while stock is the stock model which pertains to 2 pieces of data, 'Available' and 'Not Available' (take note this works when I'm only logged in as a regular user, but doesn't when I create a second clothe as an admin)

On my clothes index blade, for some reason it reads $brand, $stock and $clothe as non objects.

When the admin creates the second clothing, the data does get stored as I have checked in my database, the only problem is displaying it. Thank you for reading.

0 likes
3 replies
bobbybouwmann's avatar

Do you have the full error trace? Just Trying to get property 'id' of non-object error is really unclear and can happen in many different places. It's probably going wrong in the view. Maybe you can show a bit about that as well?

fael24's avatar

this is the code in my create method which has a 'Clothe $clothe' argument

$this->authorize('create',$clothe); $brand = Brand::all(); $stock = Stock::all(); return view('clothes.create')->with('brand',$brand)->with('stock',$stock);

and this is the code in the store method which has 3 arguments 'Clothe $clothe, Stock $stock, Request $request'

$this->authorize('create',$clothe,$stock); $request->validate([ 'name' => 'required|string', 'price' => 'required|numeric', 'brand-id' => 'required|numeric', 'stock-id' => 'required|numeric', 'description' => 'required|string', 'image' => 'required|image|max:5000' ]);

$clothe = new Clothe;

$clothe->name = $request->input('name');
$clothe->price = $request->input('price');
$clothe->brand_id = $request->input('brand-id');
$clothe->stock_id = $request->input('stock-id');
$clothe->status_id = 1;
$clothe->description = $request->input('description');
$clothe->image = $request->image->store('public');

$clothe->save();

return redirect(route('clothes.index'));

Please or to participate in this conversation.