Php Laravel nova使资源仅显示用户的数据
我正在尝试做一些事情,似乎走出了框与拉威尔新星如何工作 我有一个超级管理员使用的批处理模型/资源。那些成批出口属于几个商人。我们决定向are门户添加一层连接,允许商家登录并查看那里的数据。因此,很明显,当商家访问batch Report页面时,他只需要查看与其自身帐户相关的数据 我们所做的就是在批处理页面中添加商户id,如下所示: nova/资源/批次?mid=0123456789 然后我们发现的问题是get参数并没有发送到页面本身,而是在一个名为filter的子页面中。。。因此,我们对其进行了黑客攻击,并找到了一种方法来检索它,如下所示:Php Laravel nova使资源仅显示用户的数据,php,laravel,laravel-nova,Php,Laravel,Laravel Nova,我正在尝试做一些事情,似乎走出了框与拉威尔新星如何工作 我有一个超级管理员使用的批处理模型/资源。那些成批出口属于几个商人。我们决定向are门户添加一层连接,允许商家登录并查看那里的数据。因此,很明显,当商家访问batch Report页面时,他只需要查看与其自身帐户相关的数据 我们所做的就是在批处理页面中添加商户id,如下所示: nova/资源/批次?mid=0123456789 然后我们发现的问题是get参数并没有发送到页面本身,而是在一个名为filter的子页面中。。。因此,我们对其进行了
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
现在我们有了mid,我们需要做的就是在模型中添加一个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中,您可以通过添加索引查询方法来修改任何资源的结果查询。此方法允许您使用Eloquent以定义的任何条件修改结果
我知道您只需要使用默认定义的模型维护$model属性,并在indexQuery方法中修改结果:
。。。
公共静态$model=\App\Batch::class;
公共静态函数indexQuery(NovaRequest$request$query)
{
//使用与上述示例相同的逻辑。我建议使用$request变量来访问数据,而不是$\u服务器全局变量。
preg_match('/mid\=([0-9]{10})/',$服务器['HTTP_REFERER'],$matches);
如果(设置($matches['1'])&&$matches['1']!=''){
返回$query->where('external_mid','=',$matches['1']);
}否则{
返回$query;
}
}
...
关于PHP全局变量的使用,我建议您使用laravel default request()查看URL。您可以使用类似这样的$request->mid
从URL中的中值读取值。谢谢,indexQuery()是这样的:-)$request->mid不起作用。。。谢谢你的帮助,里卡多!