[英]Laravel Backpack. 1-1 (one-to-one) relation
I have 2 Eloquent models:我有 2 个 Eloquent 模型:
/**
* Entities/Products.php
*/
use CrudTrait;
protected $fillable = [
'name', 'macronutrients_id',
];
public function macronutrients()
{
return $this->hasOne(Macronutrients::class);
}
/**
* Entities/Macronutrients.php
*/
use CrudTrait;
protected $fillable = [
'proteins', 'fats', 'carbons', 'calories', 'product_id'
];
public function product()
{
return $this->belongsTo(Product::class);
}
I don't know how I can show table (or something like list of options) with all macronutrients on product's edit page via Laravel Backpack CRUD?我不知道如何通过 Laravel Backpack CRUD 在产品的编辑页面上显示包含所有常量营养素的表格(或类似选项列表)?
In other words, I want to make something like this:换句话说,我想做这样的事情:
on page http://example.com/admin/product/2/edit :在页面http://example.com/admin/product/2/edit 上:
* [text] Name
* Macronutrients:
[number] proteins
[number] fats
[number] carbons
[number] calories
where [text], [number] is input fields.其中 [text], [number] 是输入字段。
I resolved this with some custom logic.我用一些自定义逻辑解决了这个问题。 As a result:
因此:
Screenshot of my /admin/product/1/edit我的 /admin/product/1/edit 的屏幕截图
First of all, I created custom field:首先,我创建了自定义字段:
<!-- /resources/views/vendor/backpack/crud/fields/product_macronutrients.blade.php -->
<!-- product_macronutrients -->
@php($macronutrients = isset($entry) ? $entry->macronutrients : false)
<div @include('crud::inc.field_wrapper_attributes') >
@include('crud::inc.field_translatable_icon')
<div class="array-container form-group">
<table class="table table-bordered table-striped m-b-0">
<thead>
<tr>
<th class="text-center">{{-- <i class="fa fa-trash"></i>--}} </th>
@foreach( $field['columns'] as $column )
<th style="font-weight: 300!important;">
// l10n strings (productscatalog::labels.proteins, productscatalog::labels.fats and so on)
@lang("productscatalog::labels.$column")
</th>
@endforeach
</tr>
</thead>
<tbody ui-sortable="sortableOptions" class="table-striped">
<tr class="array-row">
<td>
<p><b>@lang("productscatalog::labels.macrontr")</b></p>
</td>
@foreach( $field['columns'] as $column)
<td>
<input
class="form-control input-sm"
type="text"
name="{{ $column }}"
value="{{ old($column) ? old($column) : $macronutrients ? $macronutrients->$column : '' }}"
@include('crud::inc.field_attributes')
/>
</td>
@endforeach
</tr>
</tbody>
</table>
</div>
</div>
And ProductCrudController
:和
ProductCrudController
:
public function setup()
{
// other stuff...
$this->crud->addField([
'label' => 'Macronutrients',
'type' => 'product_macronutrients',
'name' => '',
'columns' => [
'proteins',
'fats',
'carbons',
'calories',
],
]);
}
public function store(StoreRequest $request)
{
$redirect_location = parent::storeCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function update(UpdateRequest $request)
{
$redirect_location = parent::updateCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function destroy($id)
{
$this->destroyMacronutrients($id);
$return = parent::destroy($id);
return $return;
}
protected function storeOrUpdateMacronutrients(Request $request, Product $product)
{
$macronutrients = Macronutrients::firstOrNew(['id' => $product->id]);
$macronutrients->proteins = $request->input('proteins');
$macronutrients->fats = $request->input('fats');
$macronutrients->carbons = $request->input('carbons');
$macronutrients->calories = $request->input('calories');
$macronutrients->save();
}
protected function destroyMacronutrients($productId)
{
$macronutrients = Macronutrients::findOrFail($productId);
$macronutrients->delete();
}
Hope it helps.希望能帮助到你。
$this->crud->addColumn([
// 1-n relationship
'label' => "Country name", // Table column heading
'type' => "select",
'name' => 'country_name', // the column that contains the ID of that connected entity;
'entity' => 'country', // the method that defines the relationship in your Model
'attribute' => "country_name", // foreign key attribute that is shown to user
'model' => "App\Models\Country",
]);
this is an example for 1-n relationship in laravel backpack这是 Laravel 背包中 1-n 关系的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.