Home > Software design >  How can I access only specific input fields using foreach loop in Laravel livewire?
How can I access only specific input fields using foreach loop in Laravel livewire?

Time:07-19

I am a newbie here and using livewire, I'm doing multiple submissions such that user can add many topics and their respective fields. Here is my form:

@foreach ($topics as $index => $topic )
                <div >
                    <h1 >Topics </h1>
                    <div >
                        <label for="topicname">topic name</label>
                        <input type="url" name="topics[{{$index}}][name]" wire:model="topics.{{$index}}.name" id="topicname" placeholder="Enter topic name">
                    </div>
                    <div >
                        <div >
                            <label for="coursestatus">status</label>
                            <select name="topics[{{$index}}][status]" id="status" wire:model="topics.{{$index}}.status">
                                <option value="enabled" selected>Enabled</option>
                                <option value="disabled">Disabled</option>
                            </select>
                        </div>
                        <div >
                        </div>
                    </div>
                    <div >
                        <label for="topicdescription">topic description</label>
                        <textarea name="topics[{{$index}}][description]" wire:model="topics.{{$index}}.description" id="topicdescription" cols="30" rows="10"></textarea>
                    </div>
                    <div >
                        <label for="topicdocuments">any documents?</label>
                        <input type="file" name="topics[{{$index}}][document]" wire:model="topics.{{$index}}.document" multiple>
                    </div>
                    <button  wire:click.prevent="removetopic({{ $index }})">Remove topic&nbsp;&nbsp;<i ></i></button>


                </div>
                @endforeach
                <div >
                    <button  wire:click.prevent="back">Course&nbsp;&nbsp;<i ></i></button>
                    <button type="submit" ><i ></i>&nbsp;&nbsp;Reset</button>
                    <button type="submit" wire:click.prevent="addtopic" ><i ></i>&nbsp;&nbsp;Add This Topic</button>
                    <button  wire:click.prevent="test">Lessons&nbsp;&nbsp;<i ></i></button>
                </div>

I can fetch full array of the form using test function in my livewire component:

<?php
namespace App\Http\Livewire; 
use Livewire\Component; 
use Illuminate\Support\Facades\DB; 

class Courseupload extends Component
{

public $topics = []

public function mount()
{
    $this->topics = [[
        'name'=>'', 'descrption'=> '', 'status'=> '', 'document'=> ''
    ]];
}


public function addtopic()  
{
    $this->topics[] = ['name'=> '', 'status'=> '', 'description'=>'', 'document'=>''];

}

public function test()
{
   dd($this->topics); 
}
}

Example array Array of multiple forms

How can I fetch only specific fields like 'name', 'description' etc. from every form generated?

Thanks.

CodePudding user response:

You could convert your array to a collection and then use the pluck method to get the single field you want. If you want multiple fields then you can map over each of the elements and use the Arr::only method to return the fields you want.

For example:

$collection = collect([
    ['name' => 'First nane', 'status' => 'First status', 'description' => 'First description', 'document' => 'First document'],
    ['name' => 'Second name', 'status' => 'Second status', 'description' => 'Second description', 'document' => 'Second document']
]);

$singleField = $collection->pluck('name');

$multipleFields = $collection->map(function ($iter) {
    return Arr::only($iter, ['name', 'status']);
});

dd($singleField, $multipleFields);

How the above would differ for you, is you would be using your $this->topics property directly:

$collection = collect($this->topics);
  • Related