简体   繁体   English

Laravel与两个表的关系[类似union]

[英]Laravel relationship with two tables [something like union]

I have two tables: wishlist and products I want to read products table according to product_id in wishlist table but for specific user... 我有两个表: 愿望清单产品我想根据愿望清单表中的product_id读取商品表,但要针对特定​​用户...

First from wishlist table I read columns with user_id which would be equall to logged in user id ( Auth::user()->id )... When that is done, I read products table with product_id from wishlist table which would be same as id in products... 首先,从愿望清单表中读取具有user_id的列,该列等于登录的用户ID( Auth :: user()-> id )...完成后,我从愿望清单表中读取具有product_id的产品表作为产品中的ID ...

Controller: 控制器:

public function getWhishlist() {
    $wishItems = WishItem::where('user_id', '=', Auth::user()->id);
    return View::make('wishlist')->with('wishItems', $wishItems);
}

View: 视图:

@foreach ($wishItems as $wishItem)
    {{ dd($wishItem) }}
    <?php $getProducts = Product::find($wishItem->product_id); ?>
    @foreach ($getProducts as $getProduct)
        {{ $getProduct->title }}
    @endforeach
@endforeach

Model: 模型:

class WishItem extends Eloquent {
     protected $table = 'wishlist';
}

{{ dd($wishItem) }} -> nothing happens and nothing happens at all with whole code... there is no any results from table {{dd($ wishItem)}} ->在整个代码中什么也没发生,也什么也没有发生...表中没有任何结果

My question is how to read from two tables?? 我的问题是如何从两个表中读取?

You are not actually fetching anything. 您实际上并没有获取任何东西。

Try this 尝试这个

$wishItems = WishItem::where('user_id', '=', Auth::user()->id)->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM