Hey Artisan
In this tutorial i am going to discuss about laravel crud. In this tutorial i will create a simple crud app in laravel 7 from scratch. If you are new to laravel then you are a right place. i will discuss laravel 7 crud step by step.
So in this laravel 7 crud example tutorial you will learn how to create route and how to create controller. How we can migrate database and how we can fetch data from database. you will learn how to create, update, delete and view in laravel 7 from scratch.
If you know laravel crud then you can make laravel app with your own code. in this laravel 7 tutorial i will discuss for beginners developer who are going to start laravel recent. So if you are very new to laravel then in this tutorial i will guide you how to create insert update delete application in laravel 7.
We also validate html input form data using laravel validation helper method. So let's start our laravel 7 crud tutorial. Hope you enjoy it.
Table of Contents
Step 1 : Download Laravel
After installing composer in your environment just run below command to download laravel.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Connect Database
In this we have to connect database. Cause without database we can't save 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 we need to create our model and migration to get table to create laravel 7 crud app. run below command to create model and migration file.
php artisan make:model Contact -m
database/migrations/create_contacts_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}
Now run this below command to migrate this table.
php artisan migrate
Now open contact model and paste this below code.
app/Contact.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $guarded = [];
}
Step 4: Create Routes
Now time to create our routes to make laravel crud. Now open "routes/web.php" and paste below code.
routes/web.php
Route::get('user', 'UserController@index')->name('user.index');;
Route::get('user/create', 'UserController@create')->name('user.create');
Route::post('user/store', 'UserController@store')->name('user.store');
Route::get('user/edit/{id}', 'UserController@edit')->name('user.edit');
Route::patch('user/update/{id}', 'UserController@update')->name('user.update');
Route::delete('user/delete/{id}', 'UserController@destroy')->name('user.destroy');
Step 5: Create Controller
In this step we need to create our UserController to create all the method. So run below command to create UserController
php artisan make:controller UserController
now open UserController and paste this below code in it.
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
class UserController extends Controller
{
public function index()
{
if(\View::exists('user.index')){
$user = \App\Contact::latest()->get();
return view('user.index',compact('user'));
}
}
public function create()
{
if(\View::exists('user.index')){
return view('user.create');
}
}
public function store(UserRequest $request)
{
\App\Contact::create($request->all());
$this->message('message','User created successfully');
return redirect()->back();
}
public function edit($id)
{
if(\View::exists('user.edit')){
$user = \App\Contact::findOrFail($id);
return view('user.edit',compact('user'));
}
}
public function update(UserRequest $request, $id)
{
$user = \App\Contact::findOrFail($id);
$user->name = $request->name;
$user->email = $request->email;
$user->save();
$this->message('message','User updated successfully');
return redirect()->route('user.index');
}
public function destroy($id)
{
$user = \App\Contact::findOrFail($id);
$user->delete();
$this->message('message','User deleted successfully');
return redirect()->back();
}
public function message(string $name = null, string $message = null)
{
return session()->flash($name,$message);
}
}
Step 6: Create Blade File
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 html form to submit data.
resources/views/layouts/app.blade.php
resources/views/user/create.blade.php
resources/views/user/edit.blade.php
resources/views/user/index.blade.php
resources/views/user/message.blade.php
Now you can open bellow URL on your browser:
http://localhost:8000/user
Read also : Laravel Vue Js Form Submit With Validation Using V-form Package
Now our laravel 7 crud example tutorial is over. Hope it can help you and you have leart something from this laravel crud tutorial.