mercredi 4 avril 2018

Is there a way to map a raw query to a Model

I have a complex raw SQL query that I don't want to retro-convert to query builder. Is there a way to map the result of the query to a model?

In short, I have this kind of query:

SELECT users.* FROM users
WHERE exists (super_long_raw_query)
AND many_more_raw_where_clauses
ORDER BY something_complex
etc;

It only returns columns from one Model (from a table corresponding to a model).

It would be terrible and not readable to convert it to a "Laravel Query Builder" query:

User::where(/* ... */)
    ->whereExists(function() { /* super complex callback */ } )
    ->etc(/* ... */)

The raw query is easier (in this specific case) to read.

Is there a way to map a raw query to a model? (assuming it only returns the columns of a specific model)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire