2016-07-14 16 views
0

2つのテーブルの内部結合を使用する単純な選択クエリがあります。特定の結果のみを返すLaravel内部結合

Trying to get property of non-object

しかし、私は、JSONのすべてを返すとき、それは完璧に動作:問題は、私はタイトルを返す場合にのみ、私はこのエラーを得ています。私は質問にタイトルを使用したい。どうすればこの問題を解決できますか?

$products = DB::table('products'); 
    $messages = $products 
     ->join('bookings', 'products.id', '=', 'bookings.ProductID') 
     ->where('EmailAddress', '=', ''. $userEmail . '') 
     ->orderBy("bookings.DateCreated", "desc") 
     ->get(); 
    return $messages->Title; 
+0

$ messagesはオブジェクトではなく配列です。配列全体を繰り返します。 – itachi

+0

よろしくお願いいたします。試してみてください。しかし、私はクエリにタイトルを使用したいですか? – Joseph

答えて

0

あなたのコードまたはクエリが複数の結果を返すので、あなたがループする必要があるか、それを繰り返す: -

$products = DB::table('products'); 
$messages = $products 
    ->join('bookings', 'products.id', '=', 'bookings.ProductID') 
    ->where('EmailAddress', '=', ''. $userEmail . '') 
    ->orderBy("bookings.DateCreated", "desc") 
    ->get(); 
return $messages->Title; 

あなたのコードによると、あなたが結果の最初の行だけを必要とするようですので、あなたのコードを変更 -

$products = DB::table('products'); 
$messages = $products 
    ->join('bookings', 'products.id', '=', 'bookings.ProductID') 
    ->where('EmailAddress', '=', ''. $userEmail . '') 
    ->orderBy("bookings.DateCreated", "desc") 
    ->first(); 
return $messages->Title; 
0

なぜEloquent Eager Loadingで試してみることができないのですか?オブジェクトが返されます。

$messages = Products::with(['bookings' => function ($query) use ($userEmail) { 
    $query->where('EmailAddress', '=', ''. $userEmail . ''); 
    $query->orderBy("bookings.DateCreated", "desc"); 
}])->get(); 

Try similar like this. Hope you will get the collection. 
0

答えに感謝します。しかし私は私の解決策を見つけました。私は内部の結合に使用され、エイリアスを入れました。

$products = DB::table('products'); 
    $messages = $products 
     ->join('bookings AS b1', 'products.id', '=', 'b1.ProductID') 
     ->join('booking_notifications as b2', 'b1.Code', '=', 'b2.Code') 
     ->where('Products.EmailAddress', '=', ''. $userEmail . '') 
     ->orderBy("b1.DateCreated", "desc")  
     ->get(); 

    return view('messages.index')->with('messages', $messages);; 
+0

これはうまくいかないので、同じエラーが発生します。 – itachi

+0

それは私の作品です。 – Joseph

関連する問題