![](/img/trans.png)
[英]How to show Laravel Nova(Version 4.0) action in Nova Resource Index?
[英]Laravel nova make resource show only the data of the user
我正在嘗試做一些 go 開箱即用的事情,以及 laravel-nova 的工作原理......
我有一個超級管理員使用的批處理模型/資源。 這些批次重新報告屬於幾個商家。 我們決定為門戶添加一層連接,允許商家登錄並查看那里的數據。 所以很明顯,當商家訪問批量報告頁面時,他只需要看到與自己賬戶相關的數據。
所以我們所做的就是在批處理頁面中添加商家 ID,如下所示:nova/resources/batch?mid=0123456789
然后我們發現的問題是 get 參數不是發送到它自己的頁面,而是在一個名為 filter 的子頁面中......所以我們破解了它並找到了一種方法來檢索它,如下所示:
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
現在我們有了中間,我們需要做的就是向 model 添加一個 where() ,但它不起作用。
顯然,這種方法不是正確的方法......所以我的問題不是如何使這段代碼工作......而是如何實現它以使商家在訪問 controller 時只能看到他自己的東西。
我真正需要的是添加某種 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 維護 $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 默認請求()來查看您的 URL。 您可以使用類似$request->mid
的方法從 URL 中的中間值讀取值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.