简体   繁体   中英

How can I access only specific input fields using foreach loop in Laravel livewire?

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 class="form-column new-form">
                    <h1 class="form-title">Topics </h1>
                    <div class="form-column">
                        <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 class="form-row">
                        <div class="form-column">
                            <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 class="form-column">
                        </div>
                    </div>
                    <div class="form-column">
                        <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 class="form-column">
                        <label for="topicdocuments">any documents?</label>
                        <input type="file" name="topics[{{$index}}][document]" wire:model="topics.{{$index}}.document" multiple>
                    </div>
                    <button class="form-submit" wire:click.prevent="removetopic({{ $index }})">Remove topic&nbsp;&nbsp;<i class="bi bi-chevron-right"></i></button>


                </div>
                @endforeach
                <div class="form-row">
                    <button class="form-submit" wire:click.prevent="back">Course&nbsp;&nbsp;<i class="bi bi-chevron-left"></i></button>
                    <button type="submit" class="form-reset"><i class="bi bi-x-lg"></i>&nbsp;&nbsp;Reset</button>
                    <button type="submit" wire:click.prevent="addtopic" class="form-submit"><i class="bi bi-plus-lg"></i>&nbsp;&nbsp;Add This Topic</button>
                    <button class="form-submit" wire:click.prevent="test">Lessons&nbsp;&nbsp;<i class="bi bi-chevron-right"></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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM