laravel 我如何加入 Eloquent:关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42641789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I join with Eloquent: Relationships?
提问by moses toh
My query is like this :
我的查询是这样的:
<?php
public function getListReviews()
{
$reviews = Review::where('user_id', auth()->user()->id)
->get();
return $reviews;
}
From the query, it can get all review data by id
从查询中,它可以通过id获取所有评论数据
I want get user photo, store photo and product photo
我想获取用户照片、商店照片和产品照片
I want get it use Eloquent: Relationships
我想使用 Eloquent:Relationships
How can I get it with Eloquent: Relationships?
我如何通过 Eloquent: Relationships 获得它?
My review model is like this :
我的评论模型是这样的:
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Review extends Eloquent
{
use HybridRelations;
protected $connection = 'mongodb';
protected $fillable = ['user_id', 'user_name', 'product_id', 'product_name', 'store_id', 'store_name', 'invoice_number', 'rating', 'comments', 'created_at', 'updated_at'];
public function user()
{
return $this->belongsTo(User::class);
}
}
My user model is like this :
我的用户模型是这样的:
<?php
namespace App;
...
class User extends Authenticatable
{
...
protected $fillable = ['name', 'email', 'password', 'birth_date', 'mobile_number', 'photo'];
public function store()
{
return $this->hasOne(Store::class);
}
}
My store model is like this :
我的店铺模式是这样的:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{
protected $fillable = ['user_id', 'name', 'address', 'phones', 'total_product', 'photo'];
public function products()
{
return $this->hasMany(Product::class);
}
}
My product model is like this :
我的产品型号是这样的:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['store_id','category_id','name', 'photo','description'];
public function store()
{
return $this->belongsTo(Store::class);
}
}
回答by Vaibhavraj Roham
If you want to find out product and store from review model then add two more methods to Review
model as below.
如果您想从评论模型中找出产品和商店,请添加另外两种方法来Review
建模,如下所示。
Edit App\Review.php
编辑 App\Review.php
//You already have key to to find store i.e. store_id
public function store()
{
return $this->belongsTo(Store::class);
}
//You already have key to to find product i.e. product_id
public function product()
{
return $this->belongsTo(Product::class);
}
then execute your query as below
然后执行您的查询如下
$reviews = Review::where('user_id', auth()->user()->id)
->with('store')
->with('product')
->with('user')
->get();
and you can access them as below,
您可以按如下方式访问它们,
Iterate through $reviews
object as $review
遍历$reviews
对象作为$review
$review->store->photo;
$review->product->photo;
$review->user->photo;