【问题标题】:Laravel eloquent join three tableLaravel 雄辩联接三表
【发布时间】:2018-04-22 00:18:12
【问题描述】:

我是Laravel 的新手。现在我需要一些关于连接表 eloquent 的帮助。

这是我的表结构,

产品表

product_id    product_code    product_name
     1            1434            ABC

产品历史记录表

history_id    product_id    cost    invoice_number
    1              1        100         ABC-01

产品导览表

tour_id       product_id    provider_name
    1              1            TEST

现在我加入这 3 个表,

$product =  product::join('product_history as ph', 'product.product_id', '=', 'ph.product_id', 'inner')
            ->join('product_tour as pt', 'product.product_id', '=', 'pt.product_id', 'inner')
            ->where('product.product_id', '1')
            ->get()
            ->toArray();

它工作正常。但我认为 Laravel 也提供了雄辩的关系。比如hasOnehasManybelongsTobelongsToMany等等......

知道我的结构使用哪种方法吗?

提前致谢。

【问题讨论】:

    标签: php laravel inner-join laravel-eloquent


    【解决方案1】:

    像这样更改您的查询:

    $product =  product::Select("*")
                //->with('product_history','product_tour')
                //Use this with() for condition.
                ->with([
                       'ProductHistory' => function ($query){
                                        $query->select('*');
                       },
                       'ProductTour' => function ($query) use ($ProviderName) {
                                        $query->select('*')->where("provider_name", "=", $ProviderName);
                       },
                ])
                ->where('product.product_id', '1')
                ->get()
                ->toArray();
    

    这是模型文件的代码:

    namespace App;
    use DB;
    use Illuminate\Database\Eloquent\Model;
    
    class Product extends Model
    {
        public function producthistory()
        {
            return $this->hasMany('App\ProductHistory', 'product_id','history_id');        
        }
        public function producttour()
        {
            return $this->hasMany('App\ProductTour', 'product_id','tour_id');        
        }
    
    }
    

    以及命名为Product_historyproduct_tour 的其他表的模型文件

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class ProductHistory extends Model
    {    
    
        public function products()
        {
            return $this->belongsTo('App\Product', 'history_id','product_id');
        }
    
    }
    

    还有

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class ProductTour extends Model
    {    
    
        public function products()
        {
            return $this->belongsTo('App\Product', 'tour_id','product_id');
        }
    
    }
    

    【讨论】:

    • 谢谢。是的,我需要设置关系。
    • 好的,谢谢。你能解释一下为什么使用 hasMany 方法吗?
    • HasMany 关系表明,对于每个产品,product_historyproduct_tour 表中都有许多条目。如果它们只有一个条目,那么您可以使用 hasOne关系
    • @JaydeepMor 更多信息可以在这个链接中找到:laravel.com/docs/5.5/eloquent-relationships
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2016-05-23
    • 2018-03-28
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多