Для вашей структуры таблицы и ваших требований, я предлагаю следующее решение:
// database\migrations\2019_10_04_153155_create_vendors_table.php
Schema::create('vendors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',100);
$table->timestamps();
});
// database\migrations\2019_10_04_153325_create_products_table.php
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',100);
$table->integer('year');
$table->timestamps();
});
// database\migrations\2019_10_04_154713_create_product_vendor_table.php
Schema::create('product_vendor', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('product_id');
$table->bigInteger('vendor_id');
$table->timestamps();
});
// app\Product.php
class Product extends Model
{
public function vendors()
{
return $this->belongsToMany(Vendor::class,'product_vendor');
}
}
// app\Vendor.php
class Vendor extends Model
{
public function products()
{
return $this->belongsToMany(Product::class,'product_vendor');
}
}
// app\ProductVendor.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use \App\Product;
use \App\Vendor;
class ProductVendor extends Model
{
protected $table = 'product_vendor';
public function GetColumnProductNameAttribute()
{
if($this->product_id){
return Product::find($this->product_id)->name;
}else{
return '';
}
}
public function GetColumnVendorNameAttribute()
{
if($this->vendor_id){
return Vendor::find($this->vendor_id)->name;
}else{
return '';
}
}
public function GetColumnProductYearAttribute()
{
if($this->product_id){
return Product::find($this->product_id)->year;
}else{
return '';
}
}
}
// app\Nova\Product.php
public static $title = 'name';
// app\Nova\Vendor.php
public static $title = 'name';
// app\Nova\ProductVendor.php
<?php
namespace App\Nova;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Number;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
class ProductVendor extends Resource
{
public static $model = 'App\ProductVendor';
public static $title = 'id';
public static function indexQuery(NovaRequest $request, $query)
{
return $query->join('vendors', 'product_vendor.vendor_id', '=', 'vendors.id')
->join('products', 'product_vendor.product_id', '=', 'products.id')
->select('vendors.name as column_vendor_name','products.name as column_product_name','products.year as column_product_year','product_vendor.*','products.*','vendors.*');
}
public static $search = [
'id','products.name','vendors.name'
];
public function fields(Request $request)
{
return [
ID::make()->sortable(),
ID::make('Vendor Id','vendor_id')
->sortable(),
Text::make('Vendor Name','column_vendor_name')
->sortable()
->hideWhenCreating(),
ID::make('Product Id','product_id')
->sortable(),
Text::make('Product Name','column_product_name')
->sortable()
->hideWhenCreating(),
Text::make('Product Year','column_product_year')
->sortable()
];
}
public function cards(Request $request)
{
return [];
}
public function filters(Request $request)
{
return [];
}
public function lenses(Request $request)
{
return [];
}
public function actions(Request $request)
{
return [];
}
}
Основная идея заключается в объединении сводной таблицы product_vendor с двумя таблицами: products, ипоставщиков. Затем с помощью некоторой базовой настройки команды вы сможете искать название продукта, имя поставщика (или более) и сортировать (все). Удачи, и спросите меня, если вам нужно!
ОБНОВЛЕНИЕ:
Чтобы исправить ошибку в сортируемом и поисковом идентификаторе, нужно немного изменить в ProductVendor Nova:
// indexQuery:
public static function indexQuery(NovaRequest $request, $query)
{
return $query->join('vendors', 'product_vendor.vendor_id', '=', 'vendors.id')
->join('products', 'product_vendor.product_id', '=', 'products.id')
->select('product_vendor.id as temp_id','products.id as temp_product_id','vendors.id as temp_vendor_id','vendors.name as column_vendor_name','products.name as column_product_name','products.year as column_product_year','product_vendor.*','products.*','vendors.*');
}
// $search
public static $search = [
'product_vendor.id','products.name','vendors.name'
];
// fields function
public function fields(Request $request)
{
return [
ID::make('ID','temp_id')->sortable(),
ID::make('Vendor Id','vendor_id')
->sortable(),
Text::make('Vendor Name','column_vendor_name')
->sortable()
->hideWhenCreating(),
ID::make('Product Id','product_id')
->sortable(),
Text::make('Product Name','column_product_name')
->sortable()
->hideWhenCreating(),
Text::make('Product Year','column_product_year')
->sortable()
];
}