Merhabalar arkadaşlar array bir dizim var başka bir ilişkili tablodan veriye ulaşıyorum ama bir türlü döngü ile döndüremedim null dönüyor yardımcı olur musunuz? konusu geçen sorun FormController'in içindeki $input->option'nun içindeki $option->value 'dir. normalde ilişki kurduğum DynamicInputtun altında dd($inputs)
FormController::
public function form()
{
$forms = DynamicForm::get();
foreach ($forms as $form)
{
$groups = DynamicGroup::where('form_id', $form->id)->get();
$form = new Form($form->title, $form->name, $form->description, $form->cardColor);
$form->setTitle($form->title);
$form->setDescription($form->description);
foreach ($groups as $group)
{
$inputs = DynamicInput::with('options')->get();
dd($inputs) //Bunu yaptığımda options tablosundaki verilere ulaşabiliyorum ama aşağıdaki foreac'te hiç veri //alamıyorum
$group = new Group($group->title, $group->description );
foreach ($inputs as $input)
{
$input = new Input($input->label, $input->name, $input->type, $input->placeHolder,
$input->description, $input->prefix, $input->suffix, $input->value,
$input->colSize, $input->max, $input->min, $input->maxLength, $input->readOnly,
$input->disabled, $input->required, $input->multiple, $input->checked, $input->step,
$input->rows,$input->option);
$group->addInput($input);
$input->setLabel($input->label);
$input->setName($input->name);
$input->setDescription($input->description);
$input->setPlaceHolder($input->placeHolder);
$input->setType($input->type);
$input->setName($input->name);
$input->setRequired($input->required);
$input->setValue($input->value);
$input->setColSize($input->colSize);
$input->setDisabled($input->disabled);
$input->setMultiple($input->multiple);
$input->setChecked($input->checked);
$input->setStep($input->step);
$input->setRows($input->rows);
foreach ($input->option as $option) //Sorun burada
{
$input->addOption($option->value); //$option->value'ye ulaşmaya çalışıyorum
}
}
$form->addGroup($group);
}
return $form;
}
Dynamic_options_table
public function up()
{
Schema::create('dynamic_options', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('input_id');
$table->foreign('input_id')->references('id')->on('dynamic_inputs')->onDelete('cascade');
$table->string('value');
$table->timestamps();
});
}
dynamic_inputs_table
public function up()
{
Schema::create('dynamic_inputs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('group_id');
$table->string('label');
$table->string('name');
$table->string('description')->nullable();
$table->string('options')->nullable();
$table->string('type');
$table->string('placeHolder')->nullable();
$table->string('prefix')->nullable();
$table->string('suffix')->nullable();
$table->string('value')->nullable();
$table->integer('colSize')->nullable();
$table->integer('max')->nullable();
$table->integer('min')->nullable();
$table->integer('maxLength')->nullable();
$table->boolean('required')->default(false);
$table->boolean('readOnly')->default(false);
$table->boolean('disabled')->default(false);
$table->boolean('multiple')->default(false);
$table->boolean('checked')->default(false);
$table->float('step')->default(0.01);
$table->integer('rows')->default(3);
$table->foreign('group_id')->references('id')->on('dynamic_groups')
->onDelete('cascade');
$table->timestamps();
});
}
DynamicInput model
class DynamicInput extends Model
{
protected $fillable = ['group_id','title', 'description','name','type','options','place_holder','prefix','suffix','value',
'col_size','max','min','max_length','required','read_only','disabled','multiple','checked','step','rows'];
protected $casts = [
'required' => 'boolean',
'read_only' => 'boolean',
'disabled' => 'boolean',
'multiple' => 'boolean',
'checked' => 'boolean',
];
use HasFactory;
public function group()
{
return $this->hasMany(DynamicGroup::class);
}
public function options():BelongsTo
{
return $this->belongsTo(DynamicOption::class,'id');
}
}
DynamicOption modelim
class DynamicOption extends Model
{
protected $fillable = ['input_id','value'];
use HasFactory;
public function inputs():HasMany
{
return $this->hasMany(DynamicInput::class);
}
}
Bu da Input.php clasım class çok uzun sadece ilgili yerleri paylaşacağım
**class Input**
{
public ?Group $group = null;
public string $label;
public string $name;
public string $type;
public string $description;
public array $option = [];
public string $placeHolder;
public string $prefix;
public string $suffix;
public $value = null;
public int $colSize=12;
public $max = null;
public $min = null;
public $maxLength = null;
public bool $readOnly = false;
public bool $disabled = false;
public bool $required = false;
public bool $multiple = false;
public bool $checked = false;
public float $step = 0.01;
public int $rows = 3;
public function __construct(string $label, string $name, string $type, string $placeHolder =null, string $description =null, string $prefix = null, string $suffix = null) {
$this->setLabel($label);
$this->setName($name);
$this->setType($type);
$this->setPlaceHolder($placeHolder ?? '');
$this->setDescription($description ?? '');
$this->setPrefix($prefix ?? '');
$this->setSuffix($suffix ?? '');
}
public function addOption(array $option): self
{
$this->option[] = $option;
return $this;
}
public function getLabel()
{
$this->label;
}
public function setOptions(array $option): self
{
$this->option[] = $option;
return $this;
}
input.blade.php
@if ($input->type == 'select' && $input->option == true)
<div class="row">
<div class="col-md-{{ $input->colSize }}">
<div class="form-group">
<label>{{ $input->label }}</label>
<select name="{{ $input->name }}" class="form-control select2bs4" style="width: 100%;">
@foreach ($input->option as $option)
<option value="{{ $option->value }}">{{ $option->value }}</option>
@endforeach
</select>
<small id="{{ $input->name }}Help" class="form-text text-muted">{{ $input->description }}</small>
</div>
</div>
</div>
@endif