【问题标题】:How to show belongsTomany data in a datatable如何在数据表中显示 belongsTomany 数据
【发布时间】:2019-12-30 23:08:34
【问题描述】:

DataTables 警告:table id=dataTableBuilder - 异常消息:

SQLSTATE[42000]: 语法错误或访问冲突: 1066 Not unique table/alias: 'phyto_product' (SQL: select count(*) as aggregate from (select '1' as row_count from phyto_product left join phyto_product on phyto_product.id = phytos.phyto_id left join phyto_product on phyto_product.id = products.deleted_atproduct_id为空) count_row_table)

我在植物和产品之间有一个belongsTomany 关系,如下所示。我正在寻找一种在datatable 中显示以下内容的方法

phyto_number, product_name, weight, charge

我的查询(PhytoProduct 是一个数据透视表)

 public function query(PhytoProduct $model)
    {
        return $model->newQuery()->leftjoin('phyto_product','phyto_product.id', '=','phytos.phyto_id')
        ->leftjoin('phyto_product','phyto_product.id', '=', 'products.product_id')
        ->select('phyto_product.*', 'phytos.phyto_number','products.product_name');
    }

我的数据表

 protected function getColumns()
    {
        return [

            [ 'data' => 'phyto_number', 'name' => 'phytos.phyto_number', 'title' => 'Phyto Number' ],
            [ 'data' => 'product_name', 'name' => 'products.product_name', 'title' => 'Product Name' ],
            [ 'data' => 'weight', 'name' => 'phyto_product.weight', 'title' => 'Weight' ],
            [ 'data' => 'charge', 'name' => 'phyto_product.charge', 'title' => 'charge' ],

        ];
    }

植物模型

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class Phyto
     * @package App\Models
     * @version December 27, 2019, 1:08 am UTC
     *
     * @property string phyto_number
     * @property integer destination_id
     * @property string indate
     */
    class Phyto extends Model
    {
        use SoftDeletes;

        public $table = 'phytos';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'phyto_number',
            'destination_id',
           'indate'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'phyto_number' => 'string',
            'destination_id' => 'integer',
            'indate' => 'date:d/m/y',
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'phyto_number' => 'required',
            'indate' => 'required'
        ];


        public function products()
        {
            return $this->belongsToMany(Product::class)->withPivot(['weight','charge']);
        }
        public function destinations()
        {
            return $this->hasMany(Destination::class);
        }

    }

PhytoProduct Model

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class PhytoProduct
     * @package App\Models
     * @version December 27, 2019, 2:20 am UTC
     *
     * @property integer phyto_id
     * @property integer product_id
     * @property number weight
     * @property number charge
     */
    class PhytoProduct extends Model
    {
        use SoftDeletes;

        public $table = 'phyto_product';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'phyto_id',
            'product_id',
            'weight',
            'charge'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'phyto_id' => 'integer',
            'product_id' => 'integer',
            'weight' => 'float',
            'charge' => 'float'
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'phyto_id' => 'required',
            'product_id' => 'required'
        ];


    }

Product Model

    <?php

    namespace App\Models;

    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    /**
     * Class Product
     * @package App\Models
     * @version December 27, 2019, 1:07 am UTC
     *
     * @property string product_name
     */
    class Product extends Model
    {
        use SoftDeletes;

        public $table = 'products';

        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';


        protected $dates = ['deleted_at'];



        public $fillable = [
            'product_name'
        ];

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer',
            'product_name' => 'string'
        ];

        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'product_name' => 'required'
        ];

        public function phytos()
        {
            return $this->belongsToMany(Product::class)->withPivot(['weight','charge']);
        }
    }

【问题讨论】:

    标签: sql laravel datatable


    【解决方案1】:

    答案是基于这个sql

    SELECT phyto_product.weight,
    phytos.phyto_number,
    products.product_name 
    FROM phyto_product 
    LEFT JOIN phytos ON phytos.id = phyto_product.phyto_id 
    Left join products ON products.id = phyto_product.product_id  
    

    然后翻译成这个

      public function query(PhytoProduct $model)
        {
            return $model->newQuery()->leftjoin('phytos','phytos.id', '=', 'phyto_product.phyto_id')
            ->leftjoin('products','products.id', '=', 'phyto_product.product_id')
            ->select('phyto_product.*', 'phytos.*','products.*');
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2020-02-12
      • 2018-05-09
      • 2012-12-31
      相关资源
      最近更新 更多