我尝试将所有折扣产品放在一个页面中,但它不会返回结果
,而完全相同的查询返回会在主页中生成。
function
public function promotions() {
$promotions = Discount::with('products')->orderby('id', 'desc')->paginate(12);
return view('front.promotions', compact('promotions'));
}
route
Route::get('/promotions', 'frontend\FrontendController@promotions')->name('promotions');
blade
@foreach($promotions as $product)
{{$product->title}} // return nothing!
@endforeach
这是我的$product
dd code
Discount {#686 ▼
#table: "discounts"
#dates: array:2 [▶]
#fillable: array:4 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▼
"id" => 4
"value_from" => "2018-02-18"
"value_to" => "2018-02-19"
"amount" => "10000"
"product_id" => 16
"stock" => 25
"created_at" => "2018-02-19 09:05:48"
"updated_at" => "2018-02-19 09:47:10"
]
#changes: []
#casts: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"products" => Product {#697 ▼
#fillable: array:16 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [▶]
#original: array:23 [▼
"id" => 16
"title" => "new product"
"slug" => "new-product"
"imageOne" => "productone-1519001703.png"
"imageTwo" => "producttwo-1519001703.jpg"
"short_description" => """
<!DOCTYPE html>\r\n
<html>\r\n
<head>\r\n
</head>\r\n
<body>\r\n
<p>eagtheyh</p>\r\n
</body>\r\n
</html>
"""
"description" => """
<!DOCTYPE html>\r\n
<html>\r\n
<head>\r\n
</head>\r\n
<body>\r\n
<p>shw4 bw 6r uwrurjw ryt jyhkiuggk</p>\r\n
</body>\r\n
</html>
"""
"price" => "100000"
"meta_description" => "gjyrj"
"meta_tags" => "yrujr,ujrtsdjr,jrjurysj,"
"arrivalDays" => "1"
"height" => "546"
"weight" => "56"
"lenght" => "56"
"width" => "56"
"sku" => "47ghg"
"stock" => "24"
"status_id" => 1
"brand_id" => 1
"category_id" => 1
"subcategory_id" => 1
"created_at" => "2018-02-19 07:55:03"
"updated_at" => "2018-02-21 09:58:04"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#slugOptions: null
#configSettings: array:4 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
答案 0 :(得分:1)
您需要迭代折扣和产品:
@foreach($promotions as $discount)
{{ $discount->products->title }}
@endforeach