gpt4 book ai didi

Laravel Backpack - 从 crud Controller 获取当前记录

转载 作者:行者123 更新时间:2023-12-02 04:35:22 27 4
gpt4 key购买 nike

在我的 crud Controller 中,我试图获取当前正在编辑的人的姓名。

所以

http://192.168.10.10/admin/people/93/edit

在 people crud Controller 中

public function setup() {
dd(\App\Models\People::get()->first()->name)
}

这会返回第一个人,而不是当前正在编辑的人。

如何返回当前人(本例中id为93)

最佳答案

好的,既然您使用了背包,请查看 CrudController 以了解该方法的外观:

public function edit($id)
{
$this->crud->hasAccessOrFail('update');

$this->data['entry'] = $this->crud->getEntry($id);
$this->data['crud'] = $this->crud;
$this->data['fields'] = $this->crud->getUpdateFields($id);

$this->data['id'] = $id;

return view('crud::edit', $this->data);
}

所以现在您可以覆盖编辑功能并更改任何您想要的。如果愿意,您甚至可以创建自定义编辑页面。

另一方面,设置通常用于添加类似的东西

$this->crud->addClause(...);

或者您甚至可以获得整个构造函数并将其放入设置方法中,因为设置调用如下所示:

public function __construct()
{
// call the setup function inside this closure to also have the request there
// this way, developers can use things stored in session (auth variables, etc)
$this->middleware(function ($request, $next) {
$this->setup();

return $next($request);
});
}

所以你可以做类似\Auth::user()->id;

这样工作也是正常的。如果你只使用纯 laravel,你将只能访问你相应设置的路由中的当前 id。

关于Laravel Backpack - 从 crud Controller 获取当前记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43864889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com