Persisting Eloquent Builder Instance in Laravel Livewire

Viewed 472

there is an instance of \Illuminate\Database\Eloquent\Builder that I would like to persist when instantiate (during the mount lifecycle hook) so that I can use it for queries. The problem is I would not be able to set it as public property since it will throw a Livewire\Exceptions\PublicPropertyTypeNotAllowedException (as follows). I would not be able to save it in session as well since it is a PDO instance and PDO instances can't be serialized or unserialized. enter image description here

Any idea guys?

Edit:

Controller:

public function index() {
    $tableQuery = User::query();

    return view('table', compact('tableQuery'));
    }

table.blade.php:

<livewire:table
   :tableQuery=$tableQuery
>

App\Http\Livewire\Table:

class Table extends Component{
    private $tableQuery;
    
    public function mount($tableQuery){
        $this->tableQuery = $tableQuery;
    }
    
    public function render(){
        return view('livewire.table');
    }
}
1 Answers

you can use Model inside livewire

class Table extends Component{
    public $tableQuery;
    
    public function mount(){
        $this->tableQuery =  User::query();;
     }
    
    public function render(){
        return view('livewire.table');
    }
}
Related