You don't need to pass in $quality in the foreach since it's being created inside the Livewire component. You can just do @livewire('quality-form').
Converting a Laravel form to Livewire
I want to submit an input to the database with Livewire but the problem is that in my ProfileController I am using $qualities to retrieve a collection to the view: profile.index, and in the Livewire component I use $quality to submit a form, when I rendered the component in the view I did this: @livewire('quality-form', ['quality' => $quality])
livewire.quality-form
<form
wire:submit.prevent="submitForm"
action="{{ route('quality.store') }}"
method="POST"
>
@csrf
<input
wire:model.lazy="quality"
type="text"
name="quality"
value="{{ old('quality') ?? $quality->quality }}"
placeholder="New quality"
>
@error('quality')
...
@enderror <!-- quality end -->
<div>
<button
type="submit">
Add
</button>
</div>
</form> <!-- quality form end -->
QualityForm.php
class QualityForm extends Component
{
public $quality;
protected $rules = [
'quality' => 'required|min:6',
];
public function submitForm()
{
$qualities = Quality::create([
'user_id' => Auth::user()->id,
'quality' => $this->quality
]);
$this->validate();
$this->reset();
}
public function render()
{
return view('livewire.quality-form');
}
}
In the view I have a @foreach in this way:
@foreach ($profile as $profile)
<div>
<div>
<img src="will use this space later for the profile table" alt="profile picture">
@livewire('quality-form', ['quality' => $quality])
</div>
</div> <!-- profile picture & qualities end -->
...
@endforeach
Is the $quality variable here getting in the way of the foreach given that in fact, I do not have a $quality variable anywhere on my ProfileController? How could I use the $quality variable from the Livewire class?
ProfileController
public function index()
{
$qualities = Auth::user()->qualities;
return view('profile.index', compact('qualities'));
}
The other instance I was using $quality was on QualityController store method and that worked
public function store(Quality $quality)
{
$quality = request()->validate([
'quality' => ['required', 'string'],
]);
$quality['user_id'] = auth()->id();
$qualityObject = new Quality($quality);
$qualityObject->save();
return redirect('/profile')->with('new_quality', 'Keep it up!');
}
Please or to participate in this conversation.