KINGSLEY OKPARA
KINGSLEY OKPARA

Reputation: 559

query builder equivalent of Model::query();

Good day, please, for some reasons I have to use query builder instead of Eloquent, the reason been that the name of my model is KidsCafe and if I use Eloquent, it will search for a table with myapp.kidscaves, but the name of the table is kidscafes this is throwing errors, so I have to use query builder in my interaction with the db.

Now, I have to implement search functionality, so how do I implement something like this using query builder

$kids = KidsCafe::query();

My question is how do I implement query(); using query builder, and also is there another way I can use eloquent without it

thank you in advance

Upvotes: 1

Views: 74

Answers (2)

Gonras Karols
Gonras Karols

Reputation: 1190

You can also use the setTable() function do dynamically use another table's name, something like this -

$kidscafes = new KidsCafe;
$kidscafes->setTable('kidscafes');

Upvotes: 0

Ali Ali
Ali Ali

Reputation: 1864

You can manually specify a table name by defining a table property on your model:

class KidsCafe extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'kidscafes';
}

Upvotes: 2

Related Questions