Laravel nova 使资源只显示用户的数据

Laravel nova make resource show only the data of the user

我正在尝试做一些似乎与 laravel-nova 的工作方式开箱即用的事情...

我有一个超级管理员使用的批次 model/ressource。那些批次报告属于几个商家。我们决定为门户添加一个连接层,允许商家登录并查看那里的数据。那么显然,商户在访问批量报表页面时,只需要看到与自己账户相关的数据即可。

所以我们所做的就是在批处理页面中添加商户 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方法来修改任何资源的结果查询。此方法允许您使用 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。您可以使用类似 $request->mid 的内容从 URL.

中的 mid 值中读取值