Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0
  1. Ask youself what you actually want.
  2. Imagine the query.
  3. Write the code.
somethingElseModel::with('email')->where('stuff', $stuff)->get();
Last updated 2 years ago.
0

No, there's no such thing. Luckily you can do it by yourself with no effort:

/**
 * query scope withWhereHas
 * 
 * @return void
 */
public function scopeWithWhereHas($query, $relation, $field, $operator, $value)
{
	$query->with([$relation => function ($q) use ($field, $operator, $value) {
		$q->where($field, $operator, $value);
	}])->whereHas($relation, function ($q) use ($field, $operator, $value) {
		$q->where($field, $operator, $value);
	});
}

// then for example:
$something = Something::withWhereHas('someRelation', 'someField', 'like', '%someValue')->get();
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.