Step 01: Adding product add button just above of table element in list of products
<h2 class="float-start">All Products</h2> <div class="float-end"> <a class="btn btn-sm btn-primary" href="{{ route('products.create') }}"> Add Product</a> </div>
Step 02: In ProductController, adding create() method
public function create( ) { $meta_title = 'Add Product'; return view( 'products.create', compact( 'meta_title' ) ); }
Step 03: Adding create.blade.php under resources/view/products folder and putting following snippet inside blade section directive extending app layout
<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="float-start">Add Product</h2> <div class="float-end"> <a class="btn btn-sm btn-primary" href="{{ route('products.index') }}"> Back to products</a> </div> <div class="clearfix"></div> <div class="card "> <div class="card-body"> <form method="post" action="{{ route('products.store') }}" id="updatefrm" method="post"> @csrf <div class="form-group mb-2"> <label for="title">Name</label> <br> <input name="name" class="form-control" placeholder="product name" title="Product Name" type="text" id="ProductName" value="{{ old('name') }}" /> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group mb-2"> <label for="title">Cost ($)</label> <br> <input name="cost" class="form-control" placeholder="Cost" title="Cost" type="text" id="cost" value="{{ old('cost') }}" /> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> <input class="btn btn-sm btn-primary" class="form-control" type="submit" name="action" value="Add"> </form> </div> </div> </div> </div> </div> </div>
Step 04: Creating ProductRequest.php using artisan command for validation of form fields
php artisan make:request ProductRequest
Step 05: Adding validation rules like below in ProductRequest.php making sure authorize function returns true
namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class ProductRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required', 'cost' => 'required', ]; } }
Step 06: Changing Product.php model like below adding fillable with saving fields and mutator for saving slug with name
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; class Product extends Model { use HasFactory; protected $fillable = [ 'name', 'user_id', 'description', 'slug', 'cost', 'status', 'sequence' ]; function setNameAttribute($value) { $this->attributes['name'] = $value; $this->attributes['slug'] = Str::slug($value); } }
Step 07: Back in ProductController adding store() method like below and redirecting to products list page after addition
//at the top use App\Http\Requests\ProductRequest; use App\Models\Product; public function store( ProductRequest $request ) { Product::create([ 'name' => $request->name, 'user_id' => auth()->user()->id, 'description' => $request->description ?? '', 'cost' => $request->cost, 'status' => !empty($request->status) ? 1 : 0, 'sequence' => ( Product::latest('sequence')->first()->id + 1 ) // fetching last sequence value from products table and adding one to it ]); return redirect()->route('products.index'); }
Now if we fillup the create form and submit it, product information with all its fields will be saved to products table in the database.