dev-resources.site
for different kinds of informations.
Laravel Eloquent ORM in Bangla Part-4 (Retrieving Single Models / Aggregates)
Laravel Eloquent-āĻ Retrieving Single Models / Aggregates āĻĢāĻŋāĻāĻžāĻ°āĻāĻŋ āĻĄāĻžāĻāĻžāĻŦā§āĻ¸ āĻĨā§āĻā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻāĻāĻŋ āĻ°ā§āĻāĻ°ā§āĻĄ āĻŦāĻž āĻ ā§āĻ¯āĻžāĻā§āĻ°āĻŋāĻā§āĻ āĻā§āĻ¯āĻžāĻ˛ā§ (āĻ¯ā§āĻŽāĻ¨: āĻāĻžāĻāĻ¨ā§āĻ, āĻŽā§āĻ¯āĻžāĻā§āĻ¸, āĻŽāĻŋāĻ¨, āĻāĻāĻžāĻ°ā§āĻ) āĻ°āĻŋāĻā§āĻ°āĻŋāĻ āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻ¯āĻŧāĨ¤ āĻāĻāĻŋ āĻ¸āĻžāĻ§āĻžāĻ°āĻŖāĻ¤ āĻ¤āĻāĻ¨ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻ¯āĻŧ āĻ¯āĻāĻ¨ āĻāĻĒāĻ¨āĻžāĻā§ āĻāĻāĻāĻŋ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻĄā§āĻāĻž āĻŦāĻž āĻĄā§āĻāĻž āĻ¸ā§āĻā§āĻ° āĻ¸āĻāĻā§āĻˇāĻŋāĻĒā§āĻ¤ āĻĒāĻ°āĻŋāĻ¸āĻāĻā§āĻ¯āĻžāĻ¨ āĻĒā§āĻ°āĻ¯āĻŧā§āĻāĻ¨āĨ¤
Retrieving Single Models
ā§§. find()
āĻŽā§āĻĨāĻĄ
āĻĒā§āĻ°āĻžāĻāĻŽāĻžāĻ°āĻŋ āĻā§-āĻāĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻ°ā§āĻāĻ°ā§āĻĄ āĻ°āĻŋāĻā§āĻ°āĻŋāĻ āĻāĻ°āĻ¤ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšāĻ¯āĻŧāĨ¤
use App\Models\Post;
// āĻĒā§āĻ°āĻžāĻāĻŽāĻžāĻ°āĻŋ āĻā§ ā§§-āĻāĻ° āĻ°ā§āĻāĻ°ā§āĻĄ
$post = Post::find(1);
if ($post) {
echo $post->title;
}
ā§¨. findOrFail()
āĻŽā§āĻĨāĻĄ
āĻ°ā§āĻāĻ°ā§āĻĄ āĻ¨āĻž āĻĒā§āĻ˛ā§ 404 HTTP Exception āĻā§āĻĄāĻŧā§ āĻĻā§āĻ¯āĻŧāĨ¤
$post = Post::findOrFail(1);
ā§Š. first()
āĻŽā§āĻĨāĻĄ
āĻĒā§āĻ°āĻĨāĻŽ āĻ°ā§āĻāĻ°ā§āĻĄ āĻ°āĻŋāĻā§āĻ°āĻŋāĻ āĻāĻ°āĻ¤ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšāĻ¯āĻŧāĨ¤
$post = Post::where('status', 'published')->first();
if ($post) {
echo $post->title;
}
ā§Ē. firstOrFail()
āĻŽā§āĻĨāĻĄ
āĻĒā§āĻ°āĻĨāĻŽ āĻ°ā§āĻāĻ°ā§āĻĄ āĻ¨āĻž āĻĒā§āĻ˛ā§ 404 HTTP Exception āĻā§āĻĄāĻŧā§ āĻĻā§āĻ¯āĻŧāĨ¤
$post = Post::where('status', 'published')->firstOrFail();
ā§Ģ. value()
āĻŽā§āĻĨāĻĄ
āĻā§āĻ¨ā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻāĻāĻ āĻā§āĻ¯āĻžāĻ˛ā§ āĻ°āĻŋāĻā§āĻ°āĻŋāĻ āĻāĻ°āĻ¤ā§ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻ¯āĻŧāĨ¤
$title = Post::where('id', 1)->value('title');
ā§Ŧ. pluck()
āĻŽā§āĻĨāĻĄ
āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻāĻāĻžāĻ§āĻŋāĻ āĻā§āĻ¯āĻžāĻ˛ā§ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°āĻ¤ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšāĻ¯āĻŧāĨ¤
$titles = Post::where('status', 'published')->pluck('title');
Retrieving Aggregates
Eloquent āĻāĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ āĻ ā§āĻ¯āĻžāĻā§āĻ°āĻŋāĻā§āĻ āĻŽā§āĻĨāĻĄāĻā§āĻ˛ā§ āĻ¸āĻ°āĻžāĻ¸āĻ°āĻŋ āĻĄāĻžāĻāĻžāĻŦā§āĻ¸ āĻĨā§āĻā§ āĻĄā§āĻāĻž āĻāĻŖāĻ¨āĻž āĻāĻ°āĻ¤ā§ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻ¯āĻŧāĨ¤ āĻ¨āĻŋāĻā§ āĻāĻŋāĻā§ āĻā§āĻ°ā§āĻ¤ā§āĻŦāĻĒā§āĻ°ā§āĻŖ āĻŽā§āĻĨāĻĄ āĻĻā§āĻāĻžāĻ¨ā§ āĻšāĻ˛ā§:
ā§§. count()
āĻĄāĻžāĻāĻžāĻŦā§āĻ¸ā§ āĻ°ā§āĻāĻ°ā§āĻĄā§āĻ° āĻ¸āĻāĻā§āĻ¯āĻž āĻāĻŖāĻ¨āĻž āĻāĻ°ā§āĨ¤
$postCount = Post::count();
ā§¨. max()
āĻā§āĻ¨ā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻ¸āĻ°ā§āĻŦā§āĻā§āĻ āĻŽāĻžāĻ¨ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
$maxViews = Post::max('views');
ā§Š. min()
āĻā§āĻ¨ā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻ¸āĻ°ā§āĻŦāĻ¨āĻŋāĻŽā§āĻ¨ āĻŽāĻžāĻ¨ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
$minViews = Post::min('views');
ā§Ē. avg()
āĻā§āĻ¨ā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻāĻĄāĻŧ āĻŽāĻžāĻ¨ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
$averageViews = Post::avg('views');
ā§Ģ. sum()
āĻā§āĻ¨ā§ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ˛āĻžāĻŽā§āĻ° āĻŽā§āĻ āĻ¯ā§āĻāĻĢāĻ˛ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
$totalViews = Post::sum('views');
Combination of Single Models and Aggregates
āĻāĻĒāĻ¨āĻŋ āĻāĻāĻ¸āĻžāĻĨā§ āĻ
ā§āĻ¯āĻžāĻā§āĻ°āĻŋāĻā§āĻ āĻāĻŦāĻ āĻ°ā§āĻāĻ°ā§āĻĄ āĻĢāĻŋāĻ˛ā§āĻāĻžāĻ°āĻŋāĻ āĻŽā§āĻĨāĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°ā§āĻ¨āĨ¤
$totalPublishedPosts = Post::where('status', 'published')->count();
$maxPublishedViews = Post::where('status', 'published')->max('views');
āĻāĻĻāĻžāĻšāĻ°āĻŖ: Single Record āĻāĻŦāĻ Aggregates āĻāĻāĻ¤ā§āĻ°ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°
use App\Models\Post;
// āĻĒā§āĻ°āĻĨāĻŽ Published āĻĒā§āĻ¸ā§āĻ āĻ°āĻŋāĻā§āĻ°āĻŋāĻ āĻāĻ°ā§āĻ¨
$post = Post::where('status', 'published')->first();
// āĻ¯āĻĻāĻŋ āĻĒā§āĻ¸ā§āĻ āĻĒāĻžāĻāĻ¯āĻŧāĻž āĻ¯āĻžāĻ¯āĻŧ
if ($post) {
echo "Title: " . $post->title;
}
// Published āĻĒā§āĻ¸ā§āĻā§āĻ° āĻŽā§āĻ āĻ¸āĻāĻā§āĻ¯āĻž
$totalPublishedPosts = Post::where('status', 'published')->count();
// Published āĻĒā§āĻ¸ā§āĻāĻā§āĻ˛ā§āĻ° āĻ¸āĻ°ā§āĻŦā§āĻā§āĻ āĻāĻŋāĻ
$maxPublishedViews = Post::where('status', 'published')->max('views');
echo "Total Published Posts: $totalPublishedPosts";
echo "Maximum Views for Published Posts: $maxPublishedViews";
Featured ones: