You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-10-03
* Time: 08:25
*/
namespace App\Forms\Fields;
use App\Components\CustomModelFormBuilder;
use App\Models\CustomModelFields;
use Kris\LaravelFormBuilder\Fields\FormField;
class Relation extends FormField
{
protected function getTemplate()
{
// At first it tries to load config variable,
// and if fails falls back to loading view
// resources/views/laravel-form-builder/custom-model/relation.blade.php
return 'laravel-form-builder.custom-model.relation';
}
public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
{
$belongs_field_id = $this->getOptions()["field_id"];
$belongs_field = CustomModelFields::with(["subCustomModel" => function ($query) {
$query->with(["fields" => function ($query_builder) {
$query_builder->orderBy("myindex");
}]);
}])->find($belongs_field_id);
foreach ($belongs_field->subCustomModel->fields as $field) {
if (!$field->name) {
continue;
}
$field->original_name = $field->name;
$field->name = $belongs_field->name . "[" . $field->name . "]" . "[]";
}
$options["belongs_field"] = $belongs_field;
$sub_form = (new CustomModelFormBuilder($belongs_field->subCustomModel))->generateFrom();
$options["sub_form"] = $sub_form;
return parent::render($options, $showLabel, $showField, $showError);
}
}