
Step 1: Install Laravel 8 App
First, create a new Laravel 8 project by running the following command in the terminal:
composer create-project --prefer-dist laravel/laravel YourProject
Step 2: Configure the MySQL Database
Create a new database in your MySQL database (use phpMyAdmin or MySQL command line). Then, update the .env
file located in the root of your Laravel project with your database credentials:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravelblog #your_database_name DB_USERNAME=root #your_database_username DB_PASSWORD=admin #your_database_password
Step 3: Create a Model and Migration File
Generate the model and migration file by running:
php artisan make:model Student -m
This will create a migration file inside database/migrations/
and a model inside app/Models/Student.php
.
In the migration file (2020_10_19_134839_create_students_table.php
), add the necessary columns:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStudentsTable extends Migration
{
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->text('address');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('students');
}
}
Run the migration:
php artisan migrate
Step 4: Add Fillable Property to the Model
In your app/Models/Student.php
, add the fillable
property:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
use HasFactory;
protected $fillable = [
'first_name', 'last_name', 'address',
];
}
Step 5: Define Routes
In the routes/web.php
, define routes to handle CRUD operations:
use App\Http\Controllers\StudentController;
Route::get('create', 'App\Http\Controllers\StudentController@create')->name('student.create');
Route::post('store', 'App\Http\Controllers\StudentController@store')->name('student.store');
Route::get('list', 'App\Http\Controllers\StudentController@index')->name('student.list');
Route::post('update', 'App\Http\Controllers\StudentController@update')->name('student.update');
Route::get('show', 'App\Http\Controllers\StudentController@show')->name('student.show');
Route::get('edit', 'App\Http\Controllers\StudentController@edit')->name('student.edit');
Route::get('destroy{id}', 'App\Http\Controllers\StudentController@destroy')->name('student.destroy');
Step 6: Create Controller
Generate the controller using:
bashphp artisan make:controller StudentController --resource
This will create a StudentController.php
inside app/Http/Controllers
. The controller should include the following methods for each CRUD operation:
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function index()
{
$students = Student::all();
return view('student.list', compact('students'));
}
public function create()
{
return view('student.create');
}
public function store(Request $request)
{
$request->validate([
'txtFirstName' => 'required',
'txtLastName' => 'required',
'txtAddress' => 'required'
]);
$student = new Student([
'first_name' => $request->get('txtFirstName'),
'last_name' => $request->get('txtLastName'),
'address' => $request->get('txtAddress')
]);
$student->save();
return redirect()->route('student.create')->with('success', 'Student has been added');
}
public function show(Student $student)
{
return view('student.view', compact('student'));
}
public function edit(Student $student)
{
return view('student.edit', compact('student'));
}
public function update(Request $request, $id)
{
$request->validate([
'txtFirstName' => 'required',
'txtLastName' => 'required',
'txtAddress' => 'required'
]);
$student = Student::find($id);
$student->first_name = $request->get('txtFirstName');
$student->last_name = $request->get('txtLastName');
$student->address = $request->get('txtAddress');
$student->update();
return redirect()->route('student.list')->with('success', 'Student updated successfully');
}
public function destroy(Student $student)
{
$student->delete();
return redirect()->route('student.list')->with('success', 'Student deleted successfully');
}
}
Step 7: Create Blade Views
-
resources/views/layout/app.blade.php
– Layout for the app:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Laravel 8 CRUD (Create Read Update Delete) Tutorial For Beginners</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
-
resources/views/student/create.blade.php
– Create new student:
@extends('layout.app')
@section('content')
<h2>Add New Student</h2>
<form action="{{ route('student.store') }}" method="POST">
@csrf
<div class="form-group">
<label for="txtFirstName">First Name:</label>
<input type="text" class="form-control" name="txtFirstName" placeholder="Enter First Name">
</div>
<div class="form-group">
<label for="txtLastName">Last Name:</label>
<input type="text" class="form-control" name="txtLastName" placeholder="Enter Last Name">
</div>
<div class="form-group">
<label for="txtAddress">Address:</label>
<textarea class="form-control" name="txtAddress" rows="3" placeholder="Enter Address"></textarea>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
@endsection
-
resources/views/student/list.blade.php
– Display all students:
@extends('layout.app')
@section('content')
<h2>Laravel 8 CRUD Example</h2>
<a class="btn btn-success" href="{{ route('student.create') }}">Add New Student</a>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->first_name }}</td>
<td>{{ $student->last_name }}</td>
<td>{{ $student->address }}</td>
<td>
<a href="{{ route('student.edit', $student->id) }}" class="btn btn-primary btn-sm">Edit</a>
<form action="{{ route('student.destroy', $student->id) }}" method="POST" style="display:inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
-
resources/views/student/edit.blade.php
– Edit student:
@extends('layout.app')
@section('content')
<h2>Edit Student</h2>
<form method="POST" action="{{ route('student.update', $student->id) }}">
@method('PATCH')
@csrf
<div class="form-group">
<label for="txtFirstName">First Name:</label>
<input type="text" class="form-control" name="txtFirstName" value="{{ $student->first_name }}">
</div>
<div class="form-group">
<label for="txtLastName">Last Name:</label>
<input type="text" class="form-control" name="txtLastName" value="{{ $student->last_name }}">
</div>
<div class="form-group">
<label for="txtAddress">Address:</label>
<textarea class="form-control" name="txtAddress">{{ $student->address }}</textarea>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
@endsection
-
resources/views/student/view.blade.php
– View student details:
@extends('layout.app')
@section('content')
<h2>Student Details</h2>
<table class="table table-bordered">
<tr>
<th>First Name:</th>
<td>{{ $student->first_name }}</td>
</tr>
<tr>
<th>Last Name:</th>
<td>{{ $student->last_name }}</td>
</tr>
<tr>
<th>Address:</th>
<td>{{ $student->address }}</td>
</tr>
</table>
@endsection
Step 8: Run the Application
Now, you can run the application by executing the following command:
php artisan serve
Visit the application at http://localhost:8000/create
to create, view, edit, or delete students.
first of all thanxs for this nice and quick tutorial. But unfortunately I wasn´t able to coomplete it and see the result. I guess that you probably made some little errors and typos while doing this. Like instruction reagrding PostController vs-StudentController and the other things is this:
Step 7: Create Blade Files
Here in this step, we create our view files. First, create two folders one is “layout” and another is “posts”. In the layout, folder creates an app.blade.php file, and inside the posts folder create another blade file. If you don’t know to integrate our blade files in Laravel then please check first here how to integrate the Laravel bootstrap admin panel. Now create below blade files for your crud app:
layout and posts folder doe go where.
--> Would be very happy if you could check this and reply&correct these.
Thanxs :)