Hey Artisan
In this tutorial i am going to discuss about aravel 8 crud operation example. In this tutorial i will create a simple crud app in laravel 8 from scratch. If you are new to laravel then you are a right place. i will discuss laravel 8 crud from scratch.
You know that laravel 8 was released yesterday. That's why i am going to show you Laravel 8 CRUD operations tutorial you will learn how to fetch data using get request, how delete data using delete request, how to edit or update data using put or patch request and how to save or insert data using post request.
In this laravel 8 crud example i will create a customer model and i will generate crud using this customer model. In this laravel crud tutorial i also use route model binding to see single data or update to hiding the id of customer and i will use customer slug rather id to show single data.You just have to follow some few steps and you will get basic crud stuff using controller, model, route, bootstrap 4 and blade..
I will also validate the form before submit and i will show error message of form request data. It is going to be a very simple crud operation of Laravel 7 application for beginners. CRUD is a commonn operation of every application. So let's learn laravel crud operation from scratch.
Table of Contents
Recommended : Laravel 7.x Rest API CRUD Example with JWT Auth
Step 1 : Download Laravel
After installing composer in your environment just run below command to download fresh laravel app. As we are going to start laravel crud from step by step. So download it via command.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Connect Database
In this we need to connect database. Cause without database we can't insert update view or delete our data. So connect it like below.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=give_your_db_name
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create Model and Migration
Now in this step we need to create our model and migration to get table to create laravel 7 crud application. run below command to create model and migration file.
php artisan make:model Customer -m
Recommended : Laravel 7.x Ajax CRUD Example with Sweet Alert
database/migrations/create_customers_table.php
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->string('email')->unique();
$table->timestamps();
});
}
Now run this below command to migrate this table.
php artisan migrate
Now open contact model and paste this below code.
app/Customer.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
// protected $guarded = []; or you can use
protected $fillable = [
'name',
'slug',
'email'
];
public function getRouteKeyName()
{
return 'slug';
}
}
Step 4: Create Routes
Now time to create our routes to make laravel crud applications. Now open "routes/web.php" and paste below code.
routes/web.php
Route::get('/customer', 'TestController@add_customer_form')->name('customer.add');
Route::post('/customer', 'TestController@submit_customer_data')->name('customer.save');
Route::get('/customer/list', 'TestController@fetch_all_customer')->name('customer.list');
Route::get('/customer/edit/{customer}', 'TestController@edit_customer_form')->name('customer.edit');
Route::patch('/customer/edit/{customer}', 'TestController@edit_customer_form_submit')->name('customer.update');
Route::get('/customer/{customer}', 'TestController@view_single_customer')->name('customer.view');
Route::delete('/customer/{customer}', 'TestController@delete_customer')->name('customer.delete');
Step 5: Create Controller
In this step we need to create our TestController to create all the method to complete laravel crud operation. So run below command to create TestController
php artisan make:controller TestController
Now in this step open TestController and paste this below code in it.
app/Http/Controllers/TestController.php
namespace App\Http\Controllers;
use App\Customer;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function add_customer_form()
{
if( \View::exists('customer.create') ){
return view('customer.create');
}
}
public function submit_customer_data(Request $request)
{
$rules = [
'name' => 'required|min:6',
'email' => 'required|email|unique:customers'
];
$errorMessage = [
'required' => 'Enter your :attribute first.'
];
$this->validate($request, $rules, $errorMessage);
Customer::create([
'name' => $request->name,
'slug' => \Str::slug($request->name),
'email' => strtolower($request->email)
]);
$this->meesage('message','Customer created successfully!');
return redirect()->back();
}
public function fetch_all_customer()
{
$customers = Customer::toBase()->get();
return view('customer.index',compact('customers'));
}
public function edit_customer_form(Customer $customer)
{
return view('customer.edit',compact('customer'));
}
public function edit_customer_form_submit(Request $request, Customer $customer)
{
$rules = [
'name' => 'required|min:6',
'email' => 'required|email|unique:customers'
];
$errorMessage = [
'required' => 'Enter your :attribute first.'
];
$this->validate($request, $rules, $errorMessage);
$customer->update([
'name' => $request->name,
'email' => strtolower($request->email)
]);
$this->meesage('message','Customer updated successfully!');
return redirect()->back();
}
public function view_single_customer(Customer $customer)
{
return view('customer.view',compact('customer'));
}
public function delete_customer(Customer $customer)
{
$customer->delete();
$this->meesage('message','Customer deleted successfully!');
return redirect()->back();
}
public function meesage(string $name = null, string $message = null)
{
return session()->flash($name,$message);
}
}
Step 6: Create Blade File
Recommended : Laravel 7.x Livewire CRUD Tutorial
We are set to and we are in the final step to complete this laravel 7 crud tutorial example. Just we need to create some blade file to see our data and form to submit data.
resources/views/layouts/app.blade.php
resources/views/customer/create.blade.php
resources/views/customer/edit.blade.php
resources/views/customer/index.blade.php
resources/views/customer/view.blade.php
Recommended : Laravel 7.x Vue js CRUD Example With Vue Router
Now our laravel 7 crud example tutorial is over. Now you can check it by visiting the following url.
Recommended : Building a REST API with Laravel Microservices Lumen
Hope it can help you and you have leart something new from this laravel crud tutorial. Now you know how to update data delete data view data and insert data in laravel.
#laravel-8 #laravel-8x #crud #example #tutorial