Reputation: 1892
I have a rather strange need from Eloquent. I need to represent the following query using the Eloquent way of doing things. The values come directly from user input so I don't know how best to proceed.
SELECT
GROUP_CONCAT(Table1.table2ID) AS table2ID,
GROUP_CONCAT(Table1.table3ID) AS table3ID
FROM
Table1
HAVING table2ID LIKE '%1%' AND (table3ID LIKE '%123%' OR '%456%')
AND table2ID LIKE '%2%' AND (table3ID LIKE '%789%' OR '%012%')
NOTE: This is an extremely reduced version of query to reduce confusion, but this is the part I am having the issue with.
$query->having(...)
doesn't support the closure method that $query->where
does.
Upvotes: 1
Views: 468
Reputation: 6253
If your query is too complicated for eloquent, think that the point of eloquent is doing more readable and easy the code. There is no need to overcomplicate yourself, create a scope where you can add a raw query to your Model, like this:
use Illuminate\Database\Eloquent\Model;
class MiModel extends Model
{
public function scopeHasWhatIneed($query)
{
return $query->select(DB::raw("your complex query here"));
}
}
Now you can play with your model, with understable code:
$mi_model->hasWhatIneed()->where(.....)->orderby...
Ah, notice that with raw queries you need to protect yourself from sql injection.
Upvotes: 2