Question : laravel where has
Answered by : alberto-peripolli
use Illuminate\Database\Eloquent\Builder;
// Retrieve posts with at least one comment containing words like foo%...
$posts = App\Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'foo%');
})->get();
// Retrieve posts with at least ten comments containing words like foo%...
$posts = App\Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'foo%');
}, '>=', 10)->get();
Source : https://laravel.com/docs/7.x/eloquent-relationships | Last Update : Thu, 14 May 20
Question : laravel with has
Answered by : tiago-frana
public function index()
{ $countryName = 'Brazil'; $users = User::with('country') ->whereHas('country', function (Builder $query) use($countryName) { $query->where('name', 'like', "%{$countryName}%"); }) ->get();
}
Source : https://www.itsolutionstuff.com/post/laravel-eloquent-wherehas-condition-exampleexample.html | Last Update : Sat, 23 Jul 22
Question : whereHas site:https://laravel.com/docs/
Answered by : tiago-frana
use Illuminate\Database\Eloquent\Builder;
// Retrieve posts with at least one comment containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'code%');
})->get();
// Retrieve posts with at least ten comments containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'code%');
}, '>=', 10)->get();
Source : https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence | Last Update : Thu, 24 Dec 20
Question : laravel how to query belongsTo relationship
Answered by : fragile-flatworm-0c0no1b5zwnz
$movies = Movie::whereHas('director', function($q) { $q->where('name', 'great');
})->get();
Source : https://stackoverflow.com/questions/23970762/eloquent-where-condition-based-on-a-belongs-to-relationship | Last Update : Mon, 19 Oct 20