Skip to content
Advertisement

Laravel – Property [owner] does not exist on this collection instance

I’m trying to get data from multiple rows in the same call by their id but I get this error everytime when I submit the form.. ‘Property [owner] does not exist on this collection instance.’ Can you tell me what I’m doing wrong?

My controller

public function delivery(Request $request) {

   $iid = $request->inv_id;


   $item = DB::table('inventory')
   ->join('products', 'products.id', '=', 'inventory.product_id')
   ->whereIn('inventory.id', $iid)
   ->select('products.*' , 'inventory.id as id1')
   ->get();


//Check if user owns the items
if ($item->owner != Auth::user()->id)
return back()->with('error', 'Nu detii acest obiect.');
//Check if item is not sold
if ($item->sold == 1)
return back()->with('error', 'Din pacate acest obiect este vandut.');
//Check if the item is not put in the marketplace
if ($item->on_market == 1)
return back()->with('error', 'Acest obiect este plasat la vanzare pe market.');
// Set Inventory Item redemeed to 1
Inventory::whereIn('id', $iid)
          ->update(['redeemed' => 1]);


return redirect('/inventory')->with('success', 'Success');

 }

My Blade:

<input type="hidden" name="inv_id[]" value="{{$product->id1}}">

dd($item) Output

Collection {#679 ▼
  #items: array:3 [▼
    0 => {#677 ▼
      +"id": 339
      +"product_id": 13
      +"game_key": null
      +"owner": "6"
      +"redeemed": 0
      +"visible": 1
      +"delivered": 0
      +"sold": 0
      +"on_market": 0
      +"item_nr": "8062731872"
      +"created_at": "2020-02-07 17:13:52"
      +"updated_at": "2020-02-07 17:13:52"
      +"id1": 339
    }
    1 => {#674 ▶}
    2 => {#664 ▶}
  ]
}

Advertisement

Answer

Basically your $item variable is a collection [] which mean you need to loop through it. However if you’re expecting one row result you must use first() instead of get()

Edit (assuming you’re getting a list of items)

public function delivery(Request $request) {

    $iid = $request->inv_id;
    $items = DB::table('inventory')
      ->join('products', 'products.id', '=', 'inventory.product_id')
      ->whereIn('inventory.id', $iid)
      ->select('products.*' , 'inventory.id as id1')
      ->get();

    $errors = [];

    foreach($items as $item) {
        if ($item->owner != Auth::user()->id) {
          $errors[] = 'Nu detii acest obiect.';
        }

        if ($item->sold == 1) {
          $errors[] = 'Din pacate acest obiect este vandut.';
        }

        if ($item->on_market == 1) {
          $errors[] = 'Acest obiect este plasat la vanzare pe market.';
        }
    }


    if(!empty($errors)) {

        $back = back();

        foreach($errors as $error) {
          $back = $back->with('error', $error); 
        }
        return $back;
    }

    Inventory::whereIn('id', $iid)
              ->update(['redeemed' => 1]);


    return redirect('/inventory')->with('success', 'Success');
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement