Step by step ... does this work ?
public function updatedTemplateId()
{
dd($this->template_id);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, I've got a little problem with Livewire, I'm trying to pass parameters to a component like this :
<livewire:create-screen :templates="$templates" :contents="$contents" :template_id="null"/>
Here's my component :
<div>
<div class="mt-4 col-span-3">
<label for="template" class="block text-sm font-medium text-gray-700 mt-1">Template</label>
<select wire:model="template_id" required id="template" name="template" class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<option value="">--</option>
@foreach ($templates as $unTemplate)
<option value="{{ $unTemplate->id }}">{{ $unTemplate->name }}</option>
@endforeach
</select>
</div>
{{ $contents }}
</div>
When I initially render the component, the $contents variable is returned correctly, but when I change the select's option, it returns an empty array. What's strange is that $templates always returns what it should return, never an empty array, only $contents is not working...
Here's the PHP :
<?php
namespace App\Http\Livewire;
use App\Models\Template;
use Livewire\Component;
class CreateScreen extends Component
{
public $templates;
public $template_id;
public $template;
public $contents;
public function mount($contents)
{
$this->contents = $contents;
}
public function updatedTemplateId()
{
$this->template = Template::find($this->template_id);
}
public function render()
{
return view('livewire.create-screen', [
'template' => $this->template,
]);
}
}
And here's the query corresponding to $contents :
$first = VideoContent::selectRaw('id, "video" as type, video as filename, original_filename, null as text, name')
->where('customer_id','=',$customer->id);
$second = ImageContent::selectRaw('id, "image" as type, image as filename, original_filename, null as text, name')
->where('customer_id','=',$customer->id);
$contents = TextContent::selectRaw('id, "text" as type, null as filename, null as original_filename, content as text, name')
->where('customer_id','=',$customer->id)
->union($first)
->union($second)
->orderBy('id')
->get();
Thanks for your help and have a great wednesday.
@KLassiux try this and see what happens
<?php
namespace App\Http\Livewire;
use App\Models\Template;
use Livewire\Component;
class CreateScreen extends Component
{
public $templates;
public $template_id;
public $template;
public function contentsQuery() {
$first = VideoContent::selectRaw('id, "video" as type, video as filename, original_filename, null as text, name')
->where('customer_id','=',1);
$second = ImageContent::selectRaw('id, "image" as type, image as filename, original_filename, null as text, name')
->where('customer_id','=',1);
$contents = TextContent::selectRaw('id, "text" as type, null as filename, null as original_filename, content as text, name')
->where('customer_id','=',1)
->union($first)
->union($second)
->orderBy('id')
->get();
return $contents;
}
public function updatedTemplateId()
{
dd($this->contentsQuery());
$this->template = Template::find($this->template_id);
}
public function render()
{
return view('livewire.create-screen', [
'template' => $this->template,
]);
}
}
Please or to participate in this conversation.