Laravel 8 Crud with Image Upload Tutorial

Siddharth Shukla
3 min readMar 28, 2021

--

Laravel 8 Crud with Image Upload Tutorial

Laravel 8 crud example with image upload. In this tutorial, I will show you how to create an ajax crud app with an image file upload.

This Laravel 8 crud example with image file upload tutorial guide you step by step.

Step 1 — Install the laravel 8 App
Step 2 — Connecting App to Database
Step 3 — Create Migration And Model
Step 4 — Creating a Resource Controller
Step 5 — Add Routes
Step 6 — Create Blade View
Step 7 — Run Development Server
Step 8 — Run CRUD with Image Upload App On Browser

Step 1 — Install the laravel 8 App

We need to run a command to create Laravel 8 projects.

composer create-project --prefer-dist laravel/laravel laravel-8-crud-with-image-uploadcd laravel-8-crud-with-image-upload

Step 2 — Connecting App to Database

Next step, we will set the database credentials in the application. Let’s open your project .env file and set the database credentials here.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Laravel8CrudImage
DB_USERNAME=root
DB_PASSWORD=root@123

Generate barcode in laravel: https://realprogrammer.in/how-to-generate-barcode-in-laravel-8/

Step 3 — Create Migration And Model

In this step, we will execute the following command on the terminal to create posts table migration and create Post Modal using bellow command:

php artisan nake:modal Post -m

Navigate database/migrations/ and open create_posts_table.php file. Then update the following code into this file:

<?phpuse Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('image');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}

Next, Open the App directory and open the Post.php file and then update the following code to into Post.php file as follow:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'description','image'
];
}

Step 4 — Creating a Post Controller

In this step, create a new controller name PostController. So use the below command and create a new controller that name is PostController.

php artisan make:controller PostController --resource

Next, navigate to app/Http/Controllers and open the PostController.php file. Then update the following methods for add posts, edit a post and delete the post into this controller file:

In Post Controller, you need to create some methods as follow:

Index()
Store()
Edit()

Update()
Destroy()

Index() method

Using the index() method, I will show you the post list. So update the below code in your post controller index()

method:

public function index()
{
$data['posts'] = Post::orderBy('id','desc')->paginate(5);

return view('posts.index', $data);
}

Store() Method

Using the Store() method, I will save and update the post into a database table. So update the below code in our post controller Store() method:

public function store(Request $request)
{
$request->validate([
'title' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
'description' => 'required',
]);
$path = $request->file('image')->store('public/images');
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
$post->image = $path;
$post->save();

return redirect()->route('posts.index')
->with('success','Post has been created successfully.');
}

Edit() Method

Using the Edit() method, I will edit the post details, So update the below code with our edit method:

public function edit(Post $post)
{
return view('posts.edit',compact('post'));
}

Destroy() Method

Using the delete method, we can delete a post from the post list and also the database table. So update the below code with our destroy() method:

public function destroy(Post $post)
{
$post->delete();

return redirect()->route('posts.index')
->with('success','Post has been deleted successfully');
}

Update() Method

Using the Update method, we can Update a post from the post list and also the database table. So update the below code with your Update() method:

public function update(Request $request, $id)
{
$request->validate([
'title' => 'required',
'description' => 'required',
]);

$post = Post::find($id);
if($request->hasFile('image')){
$request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
]);
$path = $request->file('image')->store('public/images');
$post->image = $path;
}
$post->title = $request->title;
$post->description = $request->description;
$post->save();

return redirect()->route('posts.index')
->with('success','Post updated successfully');
}

Read More: https://realprogrammer.in/laravel-8-crud-with-image-upload-tutorial/

--

--

Siddharth Shukla

I'm a Full Stack Engineer based in India ☀️, working at Kreativ Street. I'm a coding passion focused on Python, specifically Laravel & Django.