2017-03-07 1 views
2

내 쿼리는 다음과 같다 :Eloquent : Relationships에 어떻게 가입하나요? (Laravel 5.3)

쿼리에서
<?php 
public function getListReviews() 
{ 
    $reviews = Review::where('user_id', auth()->user()->id) 
        ->get(); 
    return $reviews; 
} 

,이 ID로 모든 검토 데이터를 얻을 수 있습니다

내가

사용자의 사진, 매장 사진과 제품 사진을 얻을 싶어

나는 그것을 사용하고 싶다 Eloquent : Relationships

나는 어떻게 그것을 Eloquent : Relationships와 함께 얻을 수 있는가?

내 리뷰 모델은 다음과 같이이다 :

<?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); 
    } 
} 

내 사용자 모델은 다음과 같이이다 :

<?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); 
    } 
} 

내 매장 모델은 다음과 같이이다 :

<?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); 
    } 
} 

내 제품 모델은 이렇게 :

<?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); 
    } 
} 

답변

3

검토 모델의 제품 및 상점을 찾으려면 Review 모델에 두 가지 방법을 추가하십시오. $review

$reviews 객체를 통해

반복 처리를,

편집 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); 
} 

$reviews = Review::where('user_id', auth()->user()->id) 
      ->with('store') 
      ->with('product') 
      ->with('user') 
      ->get(); 

아래로 쿼리를 실행하면 아래와 같이 액세스 할 수 있습니다

$review->store->photo; 
$review->product->photo; 
$review->user->photo; 

희망이 있으면 도움이됩니다.

+0

우수. 그것은 작동합니다. 감사 –