简体   繁体   中英

pass laravel query to crud Trait

I'm using Crud Traits From My controllers To Save Time How Can I pass query To From Controller To Trait

i tried this code but I get an error Constant expression contains invalid operations

/**
 * The entities to eager load on every query.
 *
 * @var array
 */
protected $withEntities= [
    'shipment'=> Shipment::query()->find($request['shipment_id']),
    
];

So I can use it in traits like this

trait HasCrudActions
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View
     */
    public function index()
    {
        return view("{$this->viewPath}.index")->with($this->withEntities);
    }
}

So you can have a method in the controller

protected function withEntities($request)
{
    return [
        'shipment' => Shipment::query()->find($request->input('shipment_id')),
    ];
}

Then in trait you can use the method

trait HasCrudActions
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View
     */
    public function index(Request $request)
    {
        return view("{$this->viewPath}.index")
            ->with($this->withEntities($request));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM