How To Create Laravel 11 CRUD App with Image Upload in 5 Minutes
Learn the essential skills and steps to become a full stack developer. Start your journey today with this comprehensive guide for beginners!
Last Update: 20 Oct 2024
Introduction
Laravel is known for its regular updates and improvements with each new release. This can be scary for some beginners. It goes like, "Oh no! Just got a hold of Laravel 9.x or 10.x, and then boom! There comes Laravel 11!! How will I cope with this?" Don't be afraid of those fears since they are basically baseless. The core concepts and syntax remain familiar, and all changes here are to help make your development experience smoother.
Actually, Laravel 11 comes with an even more streamlined, lightweight boilerplate. By default, it includes only what is absolutely necessary; therefore, resources are at a minimum consumption level. Of course, if you need more, all that is easily installable via Composer.
In this article, we will learn about Laravel 11 CRUD with image upload. I'm going to teach you step by step, using a clean and basic structure. You will see after this guide how quickly and effectively CRUD can be developed in only 5 minutes.
So let's get started!
Project Structure
here we basically used default bootstrap html layout to get a basic view in our app you can decorate your app as you want.
here we have focused only on the functional process so don't think of templating just make it in your own way!
Project Initialization and Configuration
I assume you have a local server and Composer installed on your device. Let's start with project initialization.
open your cmd and navigate in your local server's www folder and type this below code:
composer create-project laravel/laravel my-app
After running this command, Composer will give you a fully configured Laravel app. After the installation, go to the my-app folder
cd my-app
Congratulations! now you're inside your created app, now navigate the .env file and rename your app name and configure database configuration like this example:
Congratulations! your project initialization and configuration is complete
Let's create Route, Model, Migration, Controller
First, let's create a Resource route for Product. This will generate all the needed routes in the background. This way, we keep the codebase simple while showing just one route declaration.
Now lets create a model and migration in just one command by writing this on cmd:
php artisan make:model Product -m
Let's complete the migration by defining the schema. Open the generated migration file in your database/migrations. Then, write this demo code:
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->decimal('price', 8, 2);
$table->string('image')->nullable();
$table->timestamps();
});
}
Assign fillable properties in your Model to define which fields are going to be filled by the users. open app/Models navigate the Product model and write this:
Now Let's finish our Controller code, at first navigate app/Http/Controllers/Productcontroller and write index method:
public function index()
{
//fetching all the products from the database via model
$products = Product::all();
//pass all the data in the view
return view('products.index', compact('products'));
}
Let's make it functional with Controller
Now Let's finish our Controller code, at first navigate app/Http/Controllers/Productcontroller and write index() method:
public function index()
{
//fetching all the products from the database via model
$products = Product::all();
//pass all the data in the view
return view('products.index', compact('products'));
}
Now we will write our create() method code:
public function create()
{
return view('products.create');
}
Now we can make our store() method functional write this:
public function store(Request $request)
{
// validate the request data
$request->validate([
'name' => 'required',
'description' => 'required',
'price' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
// assigning filepath of the storage
$imagePath = null;
if ($request->hasFile('image')) {
$imagePath = $request->file('image')->store('images', 'public');
}
Product::create([
'name' => $request->name,
'description' => $request->description,
'price' => $request->price,
'image' => $imagePath,
]);
// redirecting to the index page
return redirect()->route('products.index')->with('success', 'Product created successfully.');
}
Now we will write our show() method code to show soecific selected product in the controller write this:
public function show(Product $product)
{
return view('products.show', compact('product'));
}
In your Product controller edit() method paste this to redirect your edit form:
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}