Laravel背包按钮,可重定向到其他模型的创建视图 [英] Laravel Backpack button that redirects to other Model's create view
问题描述
所以我有三个模型,产品,销售和代码.每个产品都属于一个代码模型(类似于用户),每个销售都有一个用于销售产品的代码字段和一个用于购买产品的代码字段.在产品的列表视图中,我想要一个名为出售"的按钮,该按钮将我重定向到带有传递的产品代码(基本上是所有者)的销售创建视图,以便我可以创建带有产品及其代码已插入的销售.
我遵循了文档说明( https://backpackforlaravel.com/docs/3.4/crud-按钮)来创建自定义按钮,但是当我进入新功能内的部分时(在指南中为适度",对我而言为出售"),我无法重定向至我运行时创建的销售模型(通过在销售的CRUD中单击创建"获得的模型)
php artisan背包:克鲁德
我该怎么做?
N.B .:我已经建立了模型的所有相对CRUD.
按钮的代码:sell.blade.php
@if($ crud-> hasAccess('update'))< a href =" {{url($ crud-> route.'/'.$ entry-> getKey().'/sell')}}"class ="btn btn-sm btn-link"> i class ="las la-dollar-sign">/iVendi/a.@万一
ProductCrudController
sell
方法
公共功能sell($ id){//添加带有产品ID和所有者的重定向到Sale的create视图}
销售模式
类别Sale扩展了模型{//使用CrudTrait;保护$ guarded = [];公共功能代码(){返回$ this-> belongsToMany('App \ Models \ Code','code','code');}公共功能product(){返回$ this-> belongsTo('App \ Models \ Products');}}
产品型号
class产品扩展了模型{//使用CrudTrait;保护$ guarded = [];公共功能productCode(){返回$ this-> belongsTo('App \ Models \ Code','code','code');}公共职能sales(){返回$ this-> hasMany('App \ Models \ Sale');}}
代码模型
class代码扩展了模型{//使用CrudTrait;保护$ guarded = [];protected $ primaryKey ='代码';protected $ keyType ='字符串';公共功能产品(){返回$ this-> hasMany('App \ Models \ Product','code','code');}}
您可以在您开始使用产品的Crud Controller中的方法中进行重定向,但是我实际上会这样做直接在按钮中.
在 sell.blade.php
中,我会做类似的事情:
@if($ crud-&>; hasAccess('update'))< a href =" {{url('admin/sales/create/?code ='.$ entry-> getKey()}}" class ="btn btn-sm btn-link"><; i class ="las la-dollar-sign"//i Vendi/a.@万一
请注意,假设销售业务负责人的路径为 admin/sales/
,如果不是这种情况,请进行修改.
然后,在设置字段的Sale crud控制器中,如果显示类似以下内容,则从GET参数设置字段的默认值:
$ this-> crud-> addField(['名称'=>'代码','标签'=>'代码','type'=>'文本','默认'=>request()-> input('code',''),]);
So I have three models, Product, Sale and Code. Each Product belongs to a Code model (it's similar to a User) and each Sale has a field for the Code who is selling the Product and one for the Code who is buying it. In the Product's list view I want to have a button called 'Sell' that redirects me to the Sale's create view with the Product's Code (basically the owner) passed in it so I can create a Sale with the Product and its Code already inserted.
I followed the documentation (https://backpackforlaravel.com/docs/3.4/crud-buttons) for creating a custom button, but when I get to the part inside the new function (in the guide it's 'moderate', for me it's 'sell') I can't redirect to the create view of the Sale model (the one I get by clicking Create in the Sale's CRUD) created when I ran
php artisan backpack:crud Sale
How can I do this?
N.B.: I already built all the model's relative CRUDs.
Button's code: sell.blade.php
@if($crud->hasAccess('update'))
<a href="{{ url($crud->route.'/'.$entry->getKey().'/sell') }} " class="btn btn-sm btn-link"><i class="las la-dollar-sign"></i>Vendi</a>
@endif
sell
method in the ProductCrudController
public function sell($id) {
// add redirect to Sale's create view with the product's id and owner
}
Sale model
class Sale extends Model {
//
use CrudTrait;
protected $guarded = [];
public function code() {
return $this->belongsToMany('App\Models\Code', 'code', 'code');
}
public function product() {
return $this->belongsTo('App\Models\Products');
}
}
Product model
class Product extends Model {
//
use CrudTrait;
protected $guarded = [];
public function productCode() {
return $this->belongsTo('App\Models\Code', 'code', 'code');
}
public function sales() {
return $this->hasMany('App\Models\Sale');
}
}
Code model
class Code extends Model {
//
use CrudTrait;
protected $guarded = [];
protected $primaryKey = 'code';
protected $keyType = 'string';
public function products() {
return $this->hasMany('App\Models\Product', 'code', 'code');
}
}
You could do the redirect in a method in the product's crud controller as you've started to, but I would actually do it directly in the button.
In sell.blade.php
I'd do something like:
@if($crud->hasAccess('update'))
<a href="{{ url('admin/sales/create/?code='.$entry->getKey() }} " class="btn btn-sm btn-link"><i class="las la-dollar-sign"></i>Vendi</a>
@endif
Note that assumes the path to your sale crud controller is admin/sales/
, modify that if thats not the case.
Then, in your Sale crud controller where you set up your fields, set the field's default from the GET parameter if present something like:
$this->crud->addField([
'name' => 'code',
'label' => 'Code',
'type' => 'text',
'default' => request()->input('code', ''),
]);
这篇关于Laravel背包按钮,可重定向到其他模型的创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!