TASKS 03: Eloquent ORM & Basic CRUD Operations - RadLeoOFC/laravel-admin-panel GitHub Wiki

1. Mapping Models to Tables in Eloquent

Eloquent ORM in Laravel is used to interact with database tables using models. It simplifies database operations by providing an object-oriented approach.

Model-to-Table Mapping

  • By default, Eloquent maps the model name to a table with its pluralized lowercase form. For example:
    • Model: Product
    • Table: products
  • If the table name differs from the default naming convention, it can be explicitly defined in the model:

protected $table = 'custom_table_name';

If the table name doesn't follow this convention (e.g., the table is named shop_items instead of products), you can explicitly define which table the model should use. To do this, add the $table property to your model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'shop_items'; // Specify the exact table name
}

Now, the Product model will interact with the shop_items table.

Primary Key

  • A primary key is a unique identifier for a record in a table. The default primary key column in Eloquent is id. However, if the table uses a different column, we can specify it in the model using the $primaryKey property.

protected $primaryKey = 'custom_id';

To specify a custom primary key column (e.g., product_id):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $primaryKey = 'product_id'; // Specify the name of the primary key column
}

Now the model will use product_id as the primary key.

Mass Assignment Protection

  • Mass assignment is a way to create or update multiple fields in a table at once. For example, here's how to create a product by passing it all the data (title, price and description) at once:
Product::create([
    'name' => 'Laptop',
    'price' => 1500,
    'description' => 'High-performance laptop'
]);
  • Without protection, an attacker could attempt to assign values to hidden or sensitive fields in your table (e.g., is_admin, password, user_id, etc.). To prevent this, Laravel requires you to explicitly specify which fields are allowed for mass assignment. To safeguard against mass assignment vulnerabilities, Eloquent uses the $fillable property:

protected $fillable = ['name', 'price', 'description'];

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['name', 'price', 'description']; // Allow only these fields
}

Now, if someone tries to mass-assign anything outside of these fields, Laravel will block the operation.

Product model

Timestamps

In Laravel, two special fields are automatically added to database tables:

`created_at` — stores the date and time when a record is created.
`updated_at` — stores the date and time when a record is last updated.

Eloquent automatically manages created_at and updated_at columns. To disable this functionality:

public $timestamps = false;

If these fields are not needed in a table, the functionality can be disabled by setting the $timestamps property in the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public $timestamps = false; // Disables timestamps
}

When timestamps are enabled, they are automatically updated whenever a record is created or modified.

An example of a complete model:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'products'; // Table associated with the model
    protected $primaryKey = 'id';  // Primary key
    public $timestamps = true;    // Use timestamps
    protected $fillable = ['name', 'price', 'description']; // Fields allowed for mass assignment
}

Summary:

  • Eloquent automatically links a model to a table, but this can be customized if needed.
  • Use $table to specify a different table name.
  • Use $primaryKey if a non-standard primary key is used.
  • Use $fillable to protect against mass assignment vulnerabilities.
  • Disable $timestamps if timestamps are not required.

These features simplify database operations and help ensure the code remains secure!



2. Resourceful Routing and Simplified CRUD Operations

Laravel’s resourceful routing is a feature designed to handle standard CRUD operations with minimal configuration. It generates all necessary routes for creating, reading, updating, and deleting resources using a single statement.

Resourceful Route Definition

To define a resourceful route for the ProductController:

Route::resource('products', ProductController::class);

Product routes

Generated Routes

The Route::resource statement creates the following routes automatically:

HTTP Method URI Controller Method Purpose
GET /products index Display a list of products.
GET /products/create create Show a form to add a product.
POST /products store Save a new product.
GET /products/{id} show Display a specific product.
GET /products/{id}/edit edit Show a form to edit a product.
PUT/PATCH /products/{id} update Update a product in the database.
DELETE /products/{id} destroy Delete a product.

This feature eliminates the need for repetitive route definitions.

Examples of Controller Methods

index(): Retrieve and display all products

public function index() {
    $products = Product::all();
    return view('products.index', compact('products'));
}

Product controller function index()


create() and store():** Show a form and save a product**

public function create() {
    return view('products.create');
}

public function store(Request $request) {
    $request->validate([
        'name' => 'required',
        'price' => 'required|numeric',
        'description' => 'nullable|string',
    ]);
    Product::create($request->all());
    return redirect()->route('products.index')->with('success', 'Product created successfully!');
}

Product Controller functions create() and store()


edit() and update(): Edit and update a product

public function edit(Product $product) {
    return view('products.edit', compact('product'));
}

public function update(Request $request, Product $product) {
    $request->validate([
        'name' => 'required',
        'price' => 'required|numeric',
        'description' => 'nullable|string',
    ]);
    $product->update($request->all());
    return redirect()->route('products.index')->with('success', 'Product updated successfully!');
}

destroy(): Delete a product

public function destroy(Product $product) {
    $product->delete();
    return redirect()->route('products.index')->with('success', 'Product deleted successfully!');
}

public function destroy()



3. Screenshots/CLI Output

CLI Commands

1. Generating a model and controller:

php artisan make:model Product

php artisan make:controller ProductController

php artisan controller

2. Running migrations:

php artisan migrate

Running migrations

3. Starting the Laravel application:

php artisan serve

Starting the Laravel application img

The Laravel application


Screenshots of CRUD Operations


Product List (Index Page)

  • resources/views/products/index.blade.php

  • A page displaying all products:

index blade html file

main page of the project products

Main page products


Product Creation Form (Create Page)

  • resources/views/products/create.blade.php

  • A page to create a new product:

create blade html file

products input name, price and description

Product creation


Edit Product Form (Edit Page)

-_ resources/views/products/edit.blade.php_

  • A page to edit an existing product:

edit blade html file

Edit product form


Delete products

Delete products



Conclusion

The use of Eloquent ORM simplifies database interactions by providing an object-oriented interface for working with tables. Resourceful routing eliminates the need for repetitive route definitions, streamlining the implementation of CRUD functionality. Together, these tools enable rapid and maintainable development in Laravel.