In this tutorial you will learn about the Laravel 7 Crud with Image Upload From Scratch and its application with practical example.
In this Laravel 7 Crud with Image Upload Example tutorial, I’ll show you how to create basic CRUD application with Image upload in laravel. In this tutorial you learn to create simple CRUD application with image upload. In this step by step tutorial we will be creating simple crud application with image upload in laravel.
Laravel 7 Crud with Image Upload From Scratch
- Install Laravel Fresh Setup
- Setup Database Credentials
- Generate Model and Migration
- Create Resource Route & Controller
- Create the blade view
- Start Development Server
Step 1: Install Laravel Fresh Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Generate Model and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Product-m |
The above command will create a model name Product and a migration file for Product table. Now, go to database/migrations file and put the below here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('product_code'); $table->text('image'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
If you found any query builder error in command prompt go to => app\Providers\AppServiceProvider.php and put the below code here :
1 2 3 4 5 6 |
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); } |
And then run this below command :
1 |
php artisan migrate:fresh |
Now, add the fillable property inside Product.php file.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $fillable = [ 'title', 'product_code', 'image', 'description', ]; } |
4). Create Resource Route & Controller
In this step we will create a resource route and controller using following command:
1 |
php artisan make:controller ProductController --resource |
The above command will create a controller name ProductController with default methods like index, store, create, update, destroy, show, edit. Now we need to add the resource route. Go to routes/web.php put the below routes here :
1 2 3 4 5 6 7 |
<?php Route::get('/', function () { return view('welcome'); }); Route::resource('products', 'ProductController'); |
Next open controller, Go to app/HTTP/Controller/ProductController and put the below code here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
<?php namespace App\Http\Controllers; use App\Product; use Illuminate\Http\Request; use Redirect; use PDF; class ProductController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data['products'] = Product::orderBy('id','desc')->paginate(10); return view('product.list',$data); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('product.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'title' => 'required', 'product_code' => 'required', 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'description' => 'required', ]); if ($files = $request->file('image')) { $destinationPath = 'public/image/'; // upload path $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension(); $files->move($destinationPath, $profileImage); $insert['image'] = "$profileImage"; } $insert['title'] = $request->get('title'); $insert['product_code'] = $request->get('product_code'); $insert['description'] = $request->get('description'); Product::insert($request->all()); return Redirect::to('products') ->with('success','Greate! Product created successfully.'); } /** * Display the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function show(Request $request) { } /** * Show the form for editing the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function edit($id) { $where = array('id' => $id); $data['product_info'] = Product::where($where)->first(); return view('product.edit', $data); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Product $product * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $request->validate([ 'title' => 'required', 'product_code' => 'required', 'description' => 'required', ]); $update = ['title' => $request->title, 'description' => $request->description]; if ($files = $request->file('image')) { $destinationPath = 'public/image/'; // upload path $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension(); $files->move($destinationPath, $profileImage); $update['image'] = "$profileImage"; } $update['title'] = $request->get('title'); $update['product_code'] = $request->get('product_code'); $update['description'] = $request->get('description'); Product::where('id',$id)->update($update); return Redirect::to('products') ->with('success','Great! Product updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function destroy($id) { Product::where('id',$id)->delete(); return Redirect::to('products')->with('success','Product deleted successfully'); } } |
Step 5: Create the blade view
Create the product directory with inside following blade view file for CRUD operations:
- Layout.blade.php
- List.blade.php
- Create.blade.php
- Edit.blade.php
Layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Laravel Crud with Image Upload Example</title> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <style> body{ background-color: #25274d; } .container{ background: #ff9b00; padding: 4%; border-top-left-radius: 0.5rem; border-bottom-left-radius: 0.5rem; } </style> </head> <body> <div class="container"> <br><br><br> @yield('content') </div> </body> </html> |
List.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
@extends('product.layout') @section('content') <a href="{{ route('products.create') }}" class="btn btn-success mb-2">Add</a> <br> <div class="row"> <div class="col-12"> <table class="table table-bordered" id="laravel_crud"> <thead> <tr> <th>Id</th> <th>Title</th> <th>Product Code</th> <th>Description</th> <th>Created at</th> <td colspan="2">Action</td> </tr> </thead> <tbody> @foreach($products as $product) <tr> <td>{{ $product->id }}</td> <td>{{ $product->title }}</td> <td>{{ $product->product_code }}</td> <td>{{ $product->description }}</td> <td>{{ date('Y-m-d', strtotime($product->created_at)) }}</td> <td><a href="{{ route('products.edit',$product->id)}}" class="btn btn-primary">Edit</a></td> <td> <form action="{{ route('products.destroy', $product->id)}}" method="post"> {{ csrf_field() }} @method('DELETE') <button class="btn btn-danger" type="submit">Delete</button> </form> </td> </tr> @endforeach </tbody> </table> {!! $products->links() !!} </div> </div> @endsection |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
@extends('product.layout') @section('content') <h2 style="margin-top: 12px;" class="text-center">Add Product</a></h2> <br> <form action="{{ route('products.store') }}" method="POST" name="add_product"> {{ csrf_field() }} <div class="row"> <div class="col-md-12"> <div class="form-group"> <strong>Title</strong> <input type="text" name="title" class="form-control" placeholder="Enter Title"> <span class="text-danger">{{ $errors->first('title') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Product Code</strong> <input type="text" name="product_code" class="form-control" placeholder="Enter Product Code"> <span class="text-danger">{{ $errors->first('product_code') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Description</strong> <textarea class="form-control" col="4" name="description" placeholder="Enter Description"></textarea> <span class="text-danger">{{ $errors->first('description') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Product Image</strong> <input type="file" name="image" class="form-control" placeholder=""> <span class="text-danger">{{ $errors->first('image') }}</span> </div> </div> <div class="col-md-12"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> @endsection |
Edit.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
@extends('product.layout') @section('content') <h2 style="margin-top: 12px;" class="text-center">Edit Product</a></h2> <br> <form action="{{ route('products.update', $product_info->id) }}" method="POST" name="update_product"> {{ csrf_field() }} @method('PATCH') <div class="row"> <div class="col-md-12"> <div class="form-group"> <strong>Title</strong> <input type="text" name="title" class="form-control" placeholder="Enter Title" value="{{ $product_info->title }}"> <span class="text-danger">{{ $errors->first('title') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Product Code</strong> <input type="text" name="product_code" class="form-control" placeholder="Enter Product Code" value="{{ $product_info->product_code }}"> <span class="text-danger">{{ $errors->first('product_code') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Description</strong> <textarea class="form-control" col="4" name="description" placeholder="Enter Description" >{{ $product_info->description }}</textarea> <span class="text-danger">{{ $errors->first('description') }}</span> </div> </div> <div class="col-md-12"> <div class="form-group"> <strong>Product Image</strong> @if($product_info->image) <img id="original" src="{{ url('public/image/'.$product_info->image) }}" height="70" width="70"> @endif <input type="text" name="image" class="form-control" placeholder="" value="{{ $product_info->image }}"> <span class="text-danger">{{ $errors->first('image') }}</span> </div> </div> <div class="col-md-12"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> @endsection |
Step 6: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/products |