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
Question : many to many relationship laravel
Answered by : brainy-beetle-xps8yb0v5p9r
use App\Models\User;
$user = User::find(1);
$user->roles()->attach($roleId);
Source : https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships | Last Update : Sat, 20 Feb 21
Question : Laravel many to many
Answered by : steveroland-nde-tsapi
users id - integer name - string
roles id - integer name - string
role_user user_id - integer role_id - integer
Source : https://laravel.com/docs/9.x/eloquent-relationships#many-to-many | Last Update : Fri, 22 Jul 22