how to use same form for create and update in laravel | websolutioncode.com
how to use same form for create and update in laravel | websolutioncode.com

how to use same form for create and update in laravel

Laravel, a powerful PHP web application framework, provides a convenient and elegant way to handle same form for create and update in laravel form submissions. In many cases, you might find yourself needing to use the same form for both creating and updating records in your database. This guide will walk you through the process, using simple English and practical code examples.

Understanding the Basics

Before diving into the code, let’s understand the basics of same form for create and update in laravel. Typically, you have a model that represents a database table, a controller that handles the logic, and a view where users input data.

Model

Assume you have a Product model representing a “products” table in your database. The model might look like this:

// app/Models/Product.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['name', 'price', 'description'];
}

Controller

Next, you would have a controller to handle the business logic. In this example, let’s create a ProductController

// app/Http/Controllers/ProductController.php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    // Methods for handling create and update will go here
}

Views

Now, let’s set up the form in a Blade view. For simplicity, we’ll create a single form that can handle both create and update operations.

<!-- resources/views/products/form.blade.php -->

<form method="post" action="{{ isset($product) ? route('products.update', $product) : route('products.store') }}">
    @csrf
    @if(isset($product))
        @method('put')
    @endif

    <!-- Your form fields go here (e.g., name, price, description) -->

    <button type="submit">{{ isset($product) ? 'Update' : 'Create' }} Product</button>
</form>

Controller Implementation

Now, let’s implement the controller methods for creating and updating products.

// app/Http/Controllers/ProductController.php

use Illuminate\Support\Facades\Redirect;

class ProductController extends Controller
{
    public function create()
    {
        return view('products.form');
    }

    public function store(Request $request)
    {
        Product::create($request->all());
        return Redirect::route('products.index');
    }

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

    public function update(Request $request, Product $product)
    {
        $product->update($request->all());
        return Redirect::route('products.index');
    }
}

Routes

Don’t forget to define your routes in the web.php file:

// routes/web.php

use App\Http\Controllers\ProductController;

Route::get('/products/create', [ProductController::class, 'create'])->name('products.create');
Route::post('/products', [ProductController::class, 'store'])->name('products.store');
Route::get('/products/{product}/edit', [ProductController::class, 'edit'])->name('products.edit');
Route::put('/products/{product}', [ProductController::class, 'update'])->name('products.update');

Conclusion

By following this approach, you now have a single form that can be used for both creating and updating records in Laravel. The controller methods and routes are designed to handle both scenarios seamlessly. This not only simplifies your code but also makes your application more user-friendly. Feel free to customize the form fields and methods according to your specific needs.

Leave a Reply