php - Laravel nova 使资源只显示用户的数据
问题描述
我正在尝试做一些似乎开箱即用的 laravel-nova 工作方式......
我有一个超级管理员使用的批处理模型/资源。这些批次重新报告属于几个商家。我们决定为门户添加一层连接,允许商家登录并查看那里的数据。所以很明显,当商家访问批量报告页面时,他只需要看到与自己账户相关的数据。
所以我们所做的就是在批处理页面中添加商家 ID,如下所示:nova/resources/batch?mid=0123456789
然后我们发现的问题是 get 参数不是发送到它自己的页面,而是在一个名为 filter 的子页面中......所以我们破解了它并找到了一种方法来检索它,如下所示:
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
现在我们有了中间,我们需要做的就是在模型中添加一个 where() 但它不起作用。
显然,这种方法不是正确的方法......所以我的问题不是如何使这段代码工作......而是如何实现它以使商家在访问控制器时只能看到他自己的东西。
我真正需要的是添加某种 where('external_mid', '=' $mid) ,一切都很好。
完整的代码现在看起来像这样:
<?php
namespace App\Nova;
use App\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Fields\BelongsTo;
use App\Nova\Filters\StatementDate;
use Laravel\Nova\Http\Requests\NovaRequest;
class Batch extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
//
public static function query(){
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
if (isset($matches['1'])&&$matches['1']!=''){
$model = \App\Batch::where('external_mid', '=', $matches['1']);
}else{
$model = \App\Batch::class;
}
return $model;
}
public static $model = $this->query();
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id','customer_name', 'external_mid', 'merchant_id', 'batch_reference', 'customer_batch_reference',
'batch_amt', 'settlement_date', 'fund_amt', 'payment_reference', 'payment_date'
];
/**
* Indicates if the resource should be globally searchable.
*
* @var bool
*/
public static $globallySearchable = false;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->hideFromIndex(),
Text::make('Customer','customer_name'),
Text::make('MID','external_mid'),
Text::make('Batch Ref #','batch_reference'),
Text::make('Batch ID','customer_batch_reference'),
Text::make('Batch Date','settlement_date')->sortable(),
Currency::make('Batch Amount','batch_amt'),
Text::make('Funding Reference','payment_reference')->hideFromIndex(),
Text::make('Funding Date','payment_date')->hideFromIndex(),
Currency::make('Funding Amount','fund_amt')->hideFromIndex(),
// **Relationships**
HasMany::make('Transactions'),
BelongsTo::make('Merchant')->hideFromIndex(),
// ***
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [
];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
解决方案
在 Laravel Nova 中,您可以通过添加index Query方法来修改任何 Resource 的结果查询。此方法允许您使用 Eloquent 以您定义的任何条件修改结果。
我了解您只需要使用具有默认定义的模型维护 $model 属性并修改 indexQuery 方法中的结果:
...
public static $model = \App\Batch::class;
public static function indexQuery(NovaRequest $request, $query)
{
// Using the same logic of the example above. I recommend to use the $request variable to access data instead of the $_SERVER global variable.
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
if (isset($matches['1'])&&$matches['1']!=''){
return $query->where('external_mid', '=', $matches['1']);
}else{
return $query;
}
}
...
关于 PHP 全局变量的使用,我建议你使用 laravel 默认的 request() 来查看你的 URL。您可以使用类似这样的方法从URL 中的中间$request->mid
值读取值。