Logo

dev-resources.site

for different kinds of informations.

Laravel Eloquent ORM in Bangla Part-3 (Models Retrieving)

Published at
1/8/2025
Categories
laravel
eloquent
database
mysql
Author
sontus
Categories
4 categories in total
laravel
open
eloquent
open
database
open
mysql
open
Author
6 person written this
sontus
open
Laravel Eloquent ORM in Bangla Part-3 (Models Retrieving)

Image description

Laravel Eloquent āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ Models Retrieving āĻāĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§‡ āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ āĻĨā§‡āĻ•ā§‡ āĻĄā§‡āĻŸāĻž āĻĢā§‡āĻš āĻ•āĻ°āĻž āĻ–ā§āĻŦāĻ‡ āĻ¸āĻšāĻœāĨ¤ Eloquent ORM āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¸āĻžāĻĨā§‡ āĻ‡āĻ¨ā§āĻŸāĻžāĻ°ā§‡āĻ•ā§āĻŸ āĻ•āĻ°āĻžāĻ° āĻœāĻ¨ā§āĻ¯ āĻ¸ā§āĻŦāĻŋāĻ§āĻžāĻœāĻ¨āĻ• āĻāĻŦāĻ‚ āĻ°āĻŋāĻĄā§‡āĻŦāĻ˛ āĻŽā§‡āĻĨāĻĄ āĻ¸āĻ°āĻŦāĻ°āĻžāĻš āĻ•āĻ°ā§‡āĨ¤

āĻ¨āĻŋāĻšā§‡ Models Retrieving āĻāĻ° āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āĻ¨ āĻ§āĻžāĻĒ āĻāĻŦāĻ‚ āĻ•ā§ŒāĻļāĻ˛ āĻĻā§‡āĻ–āĻžāĻ¨ā§‹ āĻšāĻ˛ā§‹:

ā§§. āĻ¸āĻŽāĻ¸ā§āĻ¤ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻž (Retrieving All Records)

all() āĻŽā§‡āĻĨāĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻŸā§‡āĻŦāĻŋāĻ˛ā§‡āĻ° āĻ¸āĻŽāĻ¸ā§āĻ¤ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻĢā§‡āĻš āĻ•āĻ°āĻž āĻ¯āĻžāĻ¯āĻŧāĨ¤

use App\Models\Post;

$posts = Post::all();

Enter fullscreen mode Exit fullscreen mode
  • āĻāĻŸāĻŋ āĻāĻ•āĻŸāĻŋ Collection āĻ°āĻŋāĻŸāĻžāĻ°ā§āĻ¨ āĻ•āĻ°ā§‡āĨ¤
  • āĻ†āĻĒāĻ¨āĻŋ foreach āĻŦāĻž āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ Collection āĻŽā§‡āĻĨāĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻĄā§‡āĻŸāĻž āĻ…ā§āĻ¯āĻžāĻ•ā§āĻ¸ā§‡āĻ¸ āĻ•āĻ°āĻ¤ā§‡ āĻĒāĻžāĻ°ā§‡āĻ¨:
foreach ($posts as $post) {
    echo $post->title;
}

Enter fullscreen mode Exit fullscreen mode

ā§¨. āĻāĻ•āĻ• āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻž (Retrieving a Single Record)

i) find() āĻŽā§‡āĻĨāĻĄ:

āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ āĻŸā§‡āĻŦāĻŋāĻ˛ āĻĨā§‡āĻ•ā§‡ āĻĒā§āĻ°āĻžāĻ‡āĻŽāĻžāĻ°āĻŋ āĻ•ā§€ āĻĻāĻŋāĻ¯āĻŧā§‡ āĻāĻ•āĻ• āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻĢā§‡āĻš āĻ•āĻ°ā§‡āĨ¤

$post = Post::find(1);

if ($post) {
    echo $post->title;
}

Enter fullscreen mode Exit fullscreen mode

ii) findOrFail() āĻŽā§‡āĻĨāĻĄ:

āĻ¯āĻĻāĻŋ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ¨āĻž āĻĒāĻžāĻ“āĻ¯āĻŧāĻž āĻ¯āĻžāĻ¯āĻŧ, āĻ¤āĻžāĻšāĻ˛ā§‡ āĻāĻŸāĻŋ 404 HTTP Exception āĻ›ā§āĻĄāĻŧā§‡ āĻĻā§‡āĻ¯āĻŧāĨ¤

$post = Post::findOrFail(1);

Enter fullscreen mode Exit fullscreen mode

iii) first() āĻŽā§‡āĻĨāĻĄ:

āĻĒā§āĻ°āĻĨāĻŽ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°ā§‡āĨ¤

$post = Post::where('status', 'published')->first();

Enter fullscreen mode Exit fullscreen mode

iv) firstOrFail() āĻŽā§‡āĻĨāĻĄ:

āĻĒā§āĻ°āĻĨāĻŽ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ¨āĻž āĻĒā§‡āĻ˛ā§‡ 404 HTTP Exception āĻĻā§‡āĻ¯āĻŧāĨ¤

$post = Post::where('status', 'published')->firstOrFail();

Enter fullscreen mode Exit fullscreen mode

ā§Š. āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻž (Query Constraints)

āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ āĻĨā§‡āĻ•ā§‡ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻĢāĻŋāĻ˛ā§āĻŸāĻžāĻ° āĻ•āĻ°āĻžāĻ° āĻœāĻ¨ā§āĻ¯ where āĻāĻŦāĻ‚ āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻ•āĻ¨ā§āĻĄāĻŋāĻļāĻ¨ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻšāĻ¯āĻŧāĨ¤

i) where āĻŽā§‡āĻĨāĻĄ:

$posts = Post::where('status', 'published')->get();

Enter fullscreen mode Exit fullscreen mode

ii) āĻāĻ•āĻžāĻ§āĻŋāĻ• āĻ•āĻ¨ā§āĻĄāĻŋāĻļāĻ¨:

$posts = Post::where('status', 'published')
             ->where('user_id', 1)
             ->get();

Enter fullscreen mode Exit fullscreen mode

iii) orWhere āĻŽā§‡āĻĨāĻĄ:

$posts = Post::where('status', 'published')
             ->orWhere('status', 'draft')
             ->get();

Enter fullscreen mode Exit fullscreen mode

ā§Ē. āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ•āĻ˛āĻžāĻŽ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻž

āĻĄāĻŋāĻĢāĻ˛ā§āĻŸāĻ­āĻžāĻŦā§‡, Eloquent āĻ¸āĻŽāĻ¸ā§āĻ¤ āĻ•āĻ˛āĻžāĻŽ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°ā§‡āĨ¤ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ•āĻ˛āĻžāĻŽ āĻĒā§‡āĻ¤ā§‡ select() āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻšāĻ¯āĻŧāĨ¤

$posts = Post::select('title', 'content')->get();

Enter fullscreen mode Exit fullscreen mode

ā§Ģ. āĻĒā§‡āĻœāĻŋāĻ¨ā§‡āĻļāĻ¨ (Paginate Records)

paginate() āĻŽā§‡āĻĨāĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻĒā§‡āĻœāĻŋāĻ¨ā§‡āĻŸ āĻ•āĻ°āĻž āĻ¯āĻžāĻ¯āĻŧāĨ¤

$posts = Post::paginate(10);

Enter fullscreen mode Exit fullscreen mode

āĻĒā§‡āĻœāĻŋāĻ¨ā§‡āĻļāĻ¨ā§‡āĻ° āĻ˛āĻŋāĻ™ā§āĻ• āĻĻā§‡āĻ–āĻžāĻ¨ā§‹āĻ° āĻœāĻ¨ā§āĻ¯ Blade āĻŸā§‡āĻŽāĻĒā§āĻ˛ā§‡āĻŸā§‡:

{{ $posts->links() }}

Enter fullscreen mode Exit fullscreen mode

ā§Ŧ. āĻšāĻžāĻ™ā§āĻ•āĻŋāĻ‚ (Chunking)

āĻ…āĻ¨ā§‡āĻ• āĻĄā§‡āĻŸāĻž āĻĨāĻžāĻ•āĻ˛ā§‡ āĻāĻŸāĻŋ āĻŽā§‡āĻŽā§‹āĻ°āĻŋ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻŽāĻžāĻ¨ā§‹āĻ° āĻœāĻ¨ā§āĻ¯ āĻ‰āĻĒāĻ¯ā§‹āĻ—ā§€āĨ¤

Post::chunk(100, function ($posts) {
    foreach ($posts as $post) {
        echo $post->title;
    }
});

Enter fullscreen mode Exit fullscreen mode

ā§­. āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻ…āĻ°ā§āĻĄāĻžāĻ° āĻ•āĻ°āĻž (Ordering Results)

āĻĄāĻžāĻŸāĻžāĻŦā§‡āĻ¸ā§‡āĻ° āĻ°ā§‡āĻ•āĻ°ā§āĻĄāĻ—ā§āĻ˛ā§‹āĻ•ā§‡ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ•ā§āĻ°āĻŽā§‡ āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻ¤ā§‡ orderBy() āĻŽā§‡āĻĨāĻĄ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻšāĻ¯āĻŧāĨ¤

$posts = Post::orderBy('created_at', 'desc')->get();

Enter fullscreen mode Exit fullscreen mode

ā§Ž. āĻ˛āĻŋāĻŽāĻŋāĻŸ āĻāĻŦāĻ‚ āĻ…āĻĢāĻ¸ā§‡āĻŸ

āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻ• āĻ°ā§‡āĻ•āĻ°ā§āĻĄ āĻĒā§‡āĻ¤ā§‡ take() āĻŦāĻž limit() āĻāĻŦāĻ‚ skip() āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻšāĻ¯āĻŧāĨ¤

$posts = Post::take(5)->get(); // āĻĒā§āĻ°āĻĨāĻŽ ā§ĢāĻŸāĻŋ āĻ°ā§‡āĻ•āĻ°ā§āĻĄ
$posts = Post::skip(10)->take(5)->get(); // ā§§ā§ĻāĻŸāĻŋ āĻ¸ā§āĻ•āĻŋāĻĒ āĻ•āĻ°ā§‡ āĻĒāĻ°ā§‡āĻ° ā§ĢāĻŸāĻŋ

Enter fullscreen mode Exit fullscreen mode

ā§¯. āĻ…ā§āĻ¯āĻžāĻ—ā§āĻ°āĻŋāĻ—ā§‡āĻŸ āĻŽā§‡āĻĨāĻĄāĻ¸ (Aggregates)

i) āĻ•āĻžāĻ‰āĻ¨ā§āĻŸ:

$count = Post::count();

Enter fullscreen mode Exit fullscreen mode

ii) āĻŽā§āĻ¯āĻžāĻ•ā§āĻ¸:

$maxViews = Post::max('views');

Enter fullscreen mode Exit fullscreen mode

iii) āĻŽāĻŋāĻ¨:

$minViews = Post::min('views');

Enter fullscreen mode Exit fullscreen mode

iv) āĻāĻ­āĻžāĻ°ā§‡āĻœ:

$avgViews = Post::avg('views');

Enter fullscreen mode Exit fullscreen mode

v) āĻŸā§‹āĻŸāĻžāĻ˛:

$totalViews = Post::sum('views');

Enter fullscreen mode Exit fullscreen mode

ā§§ā§Ļ. āĻ•āĻžāĻ¸ā§āĻŸāĻŽ āĻ°āĻŋāĻ˛ā§‡āĻļāĻ¨āĻļāĻŋāĻĒ āĻ°ā§‡āĻŸā§āĻ°āĻŋāĻ­āĻžāĻ˛

Eloquent āĻ¸āĻŽā§āĻĒāĻ°ā§āĻ• āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻ…āĻ¨ā§āĻ¯āĻžāĻ¨ā§āĻ¯ āĻŽāĻĄā§‡āĻ˛ā§‡āĻ° āĻĄā§‡āĻŸāĻž āĻ°āĻŋāĻŸā§āĻ°āĻŋāĻ­ āĻ•āĻ°āĻž āĻ¸āĻŽā§āĻ­āĻŦāĨ¤

i) āĻ˛ā§‹āĻĄ āĻ°āĻŋāĻ˛ā§‡āĻļāĻ¨āĻļāĻŋāĻĒ (Eager Loading):

$posts = Post::with('comments')->get();

Enter fullscreen mode Exit fullscreen mode

ii) āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ āĻ°āĻŋāĻ˛ā§‡āĻļāĻ¨āĻļāĻŋāĻĒ:

$posts = Post::with(['comments', 'user'])->get();

Enter fullscreen mode Exit fullscreen mode

ā§§ā§§. āĻ° āĻāĻŦāĻ‚ āĻ° āĻĢāĻžāĻ°ā§āĻ¸ā§āĻŸ āĻŽā§‡āĻĨāĻĄ (Raw Queries)

Laravel-āĻāĻ° DB āĻĢā§āĻ¯āĻžāĻ¸ā§‡āĻĄ āĻĻāĻŋāĻ¯āĻŧā§‡ āĻ•āĻžāĻ¸ā§āĻŸāĻŽ SQL āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°āĻž āĻ¯āĻžāĻ¯āĻŧāĨ¤

use Illuminate\Support\Facades\DB;

$posts = DB::select('SELECT * FROM posts WHERE status = ?', ['published']);

Enter fullscreen mode Exit fullscreen mode
laravel Article's
30 articles in total
Favicon
Serve a Laravel project on Web, Desktop and Mobile with Tauri
Favicon
Host Header Injection in Laravel: Risks and Prevention
Favicon
Laravel 11.30: A Leap Forward in Testing, Model IDs, and Authorization
Favicon
How to Effectively Manage Laravel Request Validation?
Favicon
[Boost]
Favicon
Building a Quick CSV Export Command in Laravel
Favicon
Deploy laravel application using vercel : Amazing
Favicon
How to Image Upload with CKeditor in Laravel 11 Tutorial
Favicon
How to Install and Use Trix Editor in Laravel 11
Favicon
Testing Temporary URLs in Laravel Storage
Favicon
API Vulnerabilities in Laravel: Identify & Secure Your Endpoints
Favicon
Enforcing Strong Passwords in Laravel
Favicon
Beyond MVC: Redefining Backend Development with DataForge
Favicon
From Product Manager to Independent Developer: A Six-Month Transformation Guide
Favicon
"PHP is dead⚰ī¸" .. what's next? Is Laravel worth it? 😎
Favicon
LTS as a Business: How an Old Project Can Become the Foundation for a New Business Model
Favicon
How to Fix the "PHP Not Found" Error on macOS After Installing XAMPP
Favicon
Sending logs to Telegram. Module for Laravel
Favicon
Need someone to contribute in writing test code for my open source project
Favicon
6 Steps to Master PHPUnit Testing with Ease!
Favicon
How to Create a Reusable Laravel Admin Panel for Multiple Projects
Favicon
Day 6: Building APIs with Laravel Sanctum
Favicon
Fix Insufficient Logging & Monitoring in Laravel Easily
Favicon
🎉 Simplify Laravel CRUD Operations with Ease! 🚀
Favicon
Laravel IQ - Level 1 - Part 2
Favicon
Different ways to use where() in Laravel
Favicon
Laravel IQ - Level 1 - Part 1
Favicon
Leveraging Social Media to Attract Top PHP Developers
Favicon
Laravel Eloquent ORM in Bangla Part-3 (Models Retrieving)
Favicon
How to Build a Generic CRUD Controller in Laravel for Multiple Resources

Featured ones: