2016-12-25 20 views
1

すでにstackoverflowでこの問題に関するいくつかの質問を読んで、その答えはどれも私に当てはまりません。 私は実行すると:Laravel非オブジェクトのプロパティを取得しようとする(雄弁なモデルから)

$item_price = ItemPrice::where('item_name',$itemname)->first(); 

、その後

$item_price->price 

を私はは非オブジェクトのプロパティを取得しようとしているが、私は実行時に取得:

dd($item_price = ItemPrice::where('item_name',$itemname)->first()); 

それは属性を持つオブジェクトを返します名前、価格など私は実際に何が起こっているのか分かりません。

全コード:

foreach ($inventorydecoded->assets as $asset) { 

    $i = 0; 
    $a = 0; 
    while ($a < 1) { 

     if ($inventorydecoded->descriptions[$i]->classid == $asset->classid) { 
     $a = 1; 
     $classid = $inventorydecoded->descriptions[$i]->classid; 
     $itemname = $inventorydecoded->descriptions[$i]->market_hash_name; 
     $tradable = $inventorydecoded->descriptions[$i]->tradable; 
     $name_color = $inventorydecoded->descriptions[$i]->name_color; 

    ; 

     } 
     $i++; 
    } // end of while 
     if ($tradable === 1 && strpos_arr($itemname, $blacklist) == false) { 
      $item_price = ItemPrice::whereItemName($itemname)->first(); 
     //  dd(ItemPrice::where('item_name',$itemname)->first()); 
      $items[] = ['assetid' => $asset->assetid,'classid'=> $classid,'itemname'=>$itemname,'name_color'=>$name_color,'price'=> $item_price->price]; 
      $serialized_inventory = serialize($items); 

     } 
    } // end of foreach 
+0

このコードを 'for'または' foreach'ループ内で使用していますか?そうです、完全なコードを表示してください。 –

+0

@AlexeyMezeninええ、投稿した投稿 – Michael

答えて

2

あなたは、ループ内でこのクエリを使用しているので、それらの一つは空で、nullを返します。したがって、単純なチェックが必要です:

if (is_null($item_price)) { 
    // There is no price for this item, do something. 
} 
+0

ええ、問題はすべての商品の価格です – Michael

+0

'if(is_null($ item_price))dd( 'このアイテムには価格はありません:' $ itemname);'。見つからないアイテムが表示されます。 –

+1

あなたは正しく、1つのアイテムにヌルフィールドがありました – Michael

0

はこれを試してみてください:

$item_price = ItemPrice::whereItemName($itemname)->first(); 
+0

を使用すると同じエラーが発生する – Michael

+0

コントローラ全体または機能を表示してください。 – GabMic

+0

ええ、私の投稿を編集しました – Michael