Difficulty: Beginner → Intermediate
Interview Level: Junior → Senior
Eloquent is Laravel's ORM — Object-Relational Mapper.
It allows developers to work with database records using PHP models rather than writing every database operation manually in SQL.
For example:
$product = Product::find(10);
Instead of manually writing:
SELECT * FROM products WHERE id = 10;
Eloquent represents the database table through a model:
class Product extends Model { }
Then:
$product->name
can access a database attribute.
Creating records
$product = Product::create([ 'name' => 'Laptop', 'price' => 75000, ]);
Updating
$product->update([ 'price' => 70000, ]);
Deleting
$product->delete();
Relationships
Suppose:
User ↓ Orders
The User model might contain:
public function orders() { return $this->hasMany(Order::class); }
Then:
$user->orders;
can retrieve the user's related orders.
This is one of Eloquent's most powerful features.
0 comments:
Post a Comment
Thanks