Laravel Model
Laravel is a popular PHP framework that makes it easy to build web applications using a modern and expressive syntax. One of the most powerful features of Laravel is its elegant and intuitive approach to working with databases through an Object-Relational Mapping (ORM) system called Eloquent. Eloquent allows developers to interact with databases using PHP objects instead of raw SQL queries, which simplifies the process of creating, reading, updating, and deleting records in a database.
At the heart of Eloquent is the Model, which represents a database table as an object-oriented entity. A Model is a PHP class that extends the Illuminate\Database\Eloquent\Model class and defines the structure and behavior of a database table. Each Model contains properties that represent the table's columns, and methods that define relationships between tables or perform data manipulation operations.
Creating a Model is easy. You can generate a new Model using the Artisan command-line tool:
php artisan make:model Product
This creates a new Product.php file in the app/Models directory, which you can open and customize to define the Model's properties and methods. By default, Eloquent assumes that the corresponding database table is named after the plural of the Model's class name (e.g., products for the Product Model). However, you can customize the table name by setting the $table property in the Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_products';
}
Defining the Model's properties is also straightforward. You can define each column as a public property of the Model, and Eloquent will automatically map it to the corresponding database column:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_products';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'description', 'price'];
public $name;
public $description;
public $price;
}
In the example above, we defined three public properties: $name, $description, and $price. We also specified that these properties can be mass assigned by the create method or the update method. By default, all properties are protected from mass assignment to prevent accidental manipulation of sensitive data.
Once you've defined your Model, you can use it to interact with the database. For example, to create a new record, you can call the create method on the Model and pass an array of attributes:
$product = Product::create([
'name' => 'Laptop',
'description' => 'A powerful laptop for developers',
'price' => 999
]);
This creates a new record in the products table with the specified attributes. Similarly, you can retrieve records using a variety of methods provided by Eloquent, such as find, first, where, or all:
// Find a record by its primary key
$product = Product::find(1);
// Find the first record that matches a condition
$product = Product::where('name', 'Laptop')->first();
// Find all records that match a condition
$products = Product::where('price', '>', 1000)->get();
Eloquent also provides powerful tools for defining relationships between tables, such as one-to-one, one-to-many, many-to-many, or polymorphic relationships. These relationships allow you to easily query related records and perform complex data manipulation operations
Comments
Post a Comment