【发布时间】: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_countfromphyto_productleft joinphyto_productonphyto_product.id=phytos.phyto_idleft joinphyto_productonphyto_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']);
}
}
【问题讨论】: