Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Assigning alias to a model

Let's say I have a model class inside MyApp\Models namespace. Now when I want to filter my data on some specific column, I have to do someting like the following:

MyApp\Models\Customer::find("MyApp\Models\Customer.name = 'Some cool name'");

Is there some way to avoid typing "MyApp\Models\Customer.name" and just use some table alias, so that I could just put "c.name" instead?

Only name must work:

MyApp\Models\Customer::find("name = 'Some cool name'");

Yeah, it does. But what if the column is ambiguous, in case the table is joined with another table. The following will not work because I would have to specify "MyApp\Models\Customer.id = 123" instead of "id = 123":

    MyApp\Models\Customer::query()
        ->join('MyApp\Models\Company',  'c.id = MyApp\Models\Customer.company_id', 'c')
        ->where('id = 123')
        ->execute();


33.6k
Accepted
answer
edited May '15

You can use the query builder instead:

$robots = $this->modelsManager->createBuilder()
    ->from(['r' => 'Some\Robots'])
    ->where('r.name = "Ultron"')
    ->getQuery()
    ->execute();

That was what I was looking for. Thanks :)