Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CyberList's avatar

Livewire : Wire:model.live="id" get random value

Hello, I am trying to integrate a wire.model.live id to search for a post by its ID but each time the page is reloaded it is replaced by a strange value like "4r9lXPKmzRQxL7DphgXf" for exemple.

Here is my code :

#[Validate('numeric')]
	public $id = "";
	
	public function render()
	{
		$videos = Video::query()
			->when($this->validate(), function ($query) {
				dump($this->validate());
				return $query->searchAdmin($this->validate());
			})
			->with('category')
			->orderBy('date', 'desc')
			->orderBy($this->sortBy, $this->sortDir)
			->paginate($this->perPage);

		return view('livewire.table-video', [
			'videos' => $videos,
			'types' => VideoType::toLabel(),
			'categories' => Category::pluck('title', 'id'),
		]);
	}
<input type="text" name="id" wire:model.live="id">

Can u help me with this ? thx :)

0 likes
16 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue with Livewire's wire:model.live directive. The wire:model.live directive is not a standard Livewire directive. Instead, Livewire provides wire:model for two-way data binding and wire:model.lazy for updating the data when the input loses focus.

If you want to search for a post by its ID as the user types, you should use wire:model combined with the updated lifecycle hook to react to changes. Here's how you can modify your code:

First, change wire:model.live to wire:model in your input field:

<input type="text" name="id" wire:model="id">

Next, in your Livewire component, you can use the updated method to react to changes in the $id property. This method is called every time a property that is bound to the view using wire:model is updated:

public function updated($propertyName)
{
    if ($propertyName === 'id' && is_numeric($this->id)) {
        // Perform your search logic here
        // For example, you can call a method that updates the list of videos
        $this->searchVideosById();
    }
}

public function searchVideosById()
{
    // Your search logic here
    // This is just an example, you'll need to implement the actual logic
    $this->videos = Video::where('id', $this->id)->get();
}

Make sure to remove the dump($this->validate()); call from your render method, as it's not a good practice to have debugging code in your production code.

Also, ensure that your validation rules are correctly defined. The #[Validate('numeric')] attribute syntax is not standard PHP or Livewire syntax. Livewire uses a different approach for validation. Here's an example of how you might validate the $id property:

protected $rules = [
    'id' => 'numeric',
    // ... other rules
];

public function updatedId()
{
    $this->validateOnly('id');
    // Perform the search or other actions after validation
}

Remember to import the necessary classes at the top of your Livewire component file:

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Video;
// ... other necessary imports

With these changes, your Livewire component should update the list of videos as the user types an ID into the input field, assuming that the searchVideosById method contains the correct logic to perform the search.

tykus's avatar

Don't use $id as a public property; it conflicts with the internet Livewire component ID that uniquely identifies the component - see the snapshot for wire.id property.

CyberList's avatar

That's what I thought but how do I pass the ID of a post via $this->validate() to retrieve it in a database query ?

CyberList's avatar

@tykus Ok I did that

#[Validate('numeric')]
public $postid = "";


public function render()
	{
		$videos = Video::query()
			->when($this->validate(), function ($query) {
				return $query->searchAdmin($this->validate());
			})
			->with('category')
			->orderBy($this->sortBy, $this->sortDir)
			->orderBy('date', 'desc')
			->paginate($this->perPage);

		return view('livewire.table-video', [
			'videos' => $videos,
			'types' => VideoType::toLabel(),
			'categories' => Category::pluck('title', 'id'),
		]);
	}
	<x-form.input wire:model.live="postid" class="w-full" name="postid" placeholder="ID"/>
public function scopeSearchAdmin(Builder $query, $value)
	{
		$query->searchpublic($value);

		if ($value['postid']) {
			$query = $query->where('id', $value['postid']);
		}


	}

It works perfectly when I type in an ID. But when I type for ex "4+" and so the numeric validation rule is not good, I get this error: Call to a member function with() on null

I have no idea why ?

CyberList's avatar

@tykus


class TableVideo extends Component
{
	use WithPagination, Searchable, Sortable, Auth;

	#[Url]
	#[Validate('regex:/[a-zA-Z0-9\s]+/|nullable')]
	public $q = "";
	#[Url]
	#[Validate('date')]
	public $datestart = "";
	#[Url]
	#[Validate('date')]
	public $dateend = "";
	#[Url]
	#[Validate('exists:categories,id')]
	#[Validate('numeric')]
	public $cat = '';
	#[Url]
	#[Validate('numeric')]
	public $videoid = "";
	#[Validate('numeric')]
	public $postid = "";
	#[Url]
	#[Validate('alpha')]
	public $source = '';
	#[Url]
	#[Validate('exists:videos,type')]
	#[Validate('string')]
	public $type = '';
	#[Url]
	#[Validate('boolean')]
	public $public = '';
	#[Url]
	#[Validate('boolean')]
	public $exclusif = '';
	#[Url]
	#[Validate('boolean')]
	public $orthographe = '';
	#[Validate('boolean')]
	public $badurlcloud = false;
	#[Url]
	#[Validate('numeric')]
	public $perPage = 20;

	public function render()
	{
		$videos = Video::query()
			->when($this->validate(), function ($query) {
				return $query->searchAdmin($this->validate());
			})
			->with('category')
			->orderBy($this->sortBy, $this->sortDir)
			->orderBy('date', 'desc')
			->paginate($this->perPage);

		return view('livewire.table-video', [
			'videos' => $videos,
			'types' => VideoType::toLabel(),
			'categories' => Category::pluck('title', 'id'),
		]);
	}
}



tykus's avatar

@CyberList same.

Can you isolate where the error is occurring; my initial thought was your query (where you eager-load), but that is actually highly unlikely? Is there any clue in the stacktrace?

CyberList's avatar

@tykus I have this

$request:Illuminate\Http\Request
POST http://clpress.test/livewire/update

The query in the model is

	if ($value['postid']) {
			$query = $query->where('id', $value['postid']);
		}
tykus's avatar

@CyberList there is not stacktrace there - I would be expecting to see some classes and functions (filenames and line numbers) where the application has been before arriving at the problem line/statement.

CyberList's avatar

@tykus I have nothing more then that :

/**
public / index.php: 51 require
**/

$response = $kernel->handle(

    $request = Request::capture()

)->send();

I can send you my code in private if you need?

CyberList's avatar
CyberList
OP
Best Answer
Level 8

I found the problem. The problem came from the fact that I was using $this->validate(); in the render() method.

So I used it in updated(). It's not super clean in my opinion but it works.

public function render()
	{

		$videos = Video::query()
			->when($this->validated, function ($query) {
				return $query->search($this->validated);
			});

		return view('livewire.table-video', [
			'videos' => $videos,
		]);
	}


	public function updated($prop)
	{
		$this->validated = $this->validate();
	}
1 like

Please or to participate in this conversation.