如何在一个“查看所有资源”页面中让一个资源视图显示所有关系的数据(并希望仍然可以排序和搜索)?
例如,我有2个表:
供应商-ID,供应商名称 产品-ID,产品名称,年份
我想要显示表格的视图:
供应商ID |供应商名称|产品编号|产品名称年
目前,我的供应商页面一页,产品页面一页。我想将结果合并为一个。
答案 0 :(得分:1)
对于您的表结构和需求,我建议采用以下解决方案:
// 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与2个表连接:产品和供应商。然后,通过一些基本的命令设置,您将能够搜索产品名称,供应商名称(或更多)和可排序的(全部)。祝你好运,问我是否需要!
更新:
要解决可排序和搜索ID上的错误,需要在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()
];
}