In this tutorial, I show you how to select, install, update, and delete a record from a MySQL database with Laravel 7.
Create a new table subjects using migration.
php artisan make:migration create_subjects_table
- Now, navigate to database/migration/ directory from the project root.
- Find a PHP file which ends with create_subjects_table and open it.
- Define the table structure in the up() method.
public function up()
{
Schema::create('subjects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->timestamps();
});
}
Run the migration –
php artisan migrate
Create Subjects Model.
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Subjects extends Model
{
protected $fillable = [
'name','description'
];
}
?>
Open routes/web.php file.
## View
Route::get('/subjects', 'SubjectsController@index')->name('subjects');
## Create
Route::get('/subjects/create', 'SubjectsController@create')->name('subjects.create');
Route::post('/subjects/store', 'SubjectsController@store')->name('subjects.store');
## Update
Route::get('/subjects/store/{id}', 'SubjectsController@edit')->name('subjects.edit');
Route::post('/subjects/update/{id}', 'SubjectsController@update')->name('subjects.update');
## Delete
Route::get('/subjects/delete/{id}', 'SubjectsController@destroy')->name('subjects.delete');
Create SubjectsController Controller.
php artisan make:controller SubjectsController
Open app/Http/Controllers/SubjectsController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesSession;
use IlluminateSupportFacadesValidator;
use AppSubjects;
class SubjectsController extends Controller {
public function index(){
$subjects = Subjects::select('id','name','description')->get();
return view('subjects.index')->with('subjects',$subjects);
}
public function create(){
return view('subjects.create');
}
public function store(Request $request){
$data = $request->except('_method','_token','submit');
$validator = Validator::make($request->all(), [
'name' => 'required|string|min:3',
'description' => 'required|string|min:3',
]);
if ($validator->fails()) {
return redirect()->Back()->withInput()->withErrors($validator);
}
if($record = Subjects::firstOrCreate($data)){
Session::flash('message', 'Added Successfully!');
Session::flash('alert-class', 'alert-success');
return redirect()->route('subjects');
}else{
Session::flash('message', 'Data not saved!');
Session::flash('alert-class', 'alert-danger');
}
return Back();
}
public function edit($id){
$subject = Subjects::find($id);
return view('subjects.edit')->with('subject',$subject);
}
public function update(Request $request,$id){
$data = $request->except('_method','_token','submit');
$validator = Validator::make($request->all(), [
'name' => 'required|string|min:3',
'description' => 'required|string|min:3',
]);
if ($validator->fails()) {
return redirect()->Back()->withInput()->withErrors($validator);
}
$subject = Subjects::find($id);
if($subject->update($data)){
Session::flash('message', 'Update successfully!');
Session::flash('alert-class', 'alert-success');
return redirect()->route('subjects');
}else{
Session::flash('message', 'Data not updated!');
Session::flash('alert-class', 'alert-danger');
}
return Back()->withInput();
}
// Delete
public function destroy($id){
Subjects::destroy($id);
Session::flash('message', 'Delete successfully!');
Session::flash('alert-class', 'alert-success');
return redirect()->route('subjects');
}
}
?>
Create a layout and subjects folder at resources/views/.
Create the following files in the folders –
layout
- app.blade.php
subject
- index.blade.php
- create.blade.php
- edit.blade.php
layout/app.blade.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CRUD (Create Read Update Delete) in a Laravel 7</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
<style type="text/css">
.errormsg{ color: red; }
.actionbutton{
width: 100%;
height: 55px;
}
</style>
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
subjects/index.blade.php
<!-- Extends template page-->
@extends('layout.app')
<!-- Specify content -->
@section('content')
<h3>Subjects List</h3>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<!-- Alert message (start) -->
@if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }}">
{{ Session::get('message') }}
</div>
@endif
<!-- Alert message (end) -->
<div class='actionbutton'>
<a class='btn btn-info float-right' href="{{route('subjects.create')}}">Add</a>
</div>
<table class="table" >
<thead>
<tr>
<th width='40%'>Name</th>
<th width='40%'>Description</th>
<th width='20%'>Actions</th>
</tr>
</thead>
<tbody>
@foreach($subjects as $subject)
<tr>
<td>{{ $subject->name }}</td>
<td>{{ $subject->description }}</td>
<td>
<!-- Edit -->
<a href="{{ route('subjects.edit',[$subject->id]) }}" class="btn btn-sm btn-info">Edit</a>
<!-- Delete -->
<a href="{{ route('subjects.delete',$subject->id) }}" class="btn btn-sm btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@stop
subjects/create.blade.php
<!-- Extends template page -->
@extends('layout.app')
<!-- Specify content -->
@section('content')
<h3>Add Subject</h3>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<!-- Alert message (start) -->
@if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }}">
{{ Session::get('message') }}
</div>
@endif
<!-- Alert message (end) -->
<div class="actionbutton">
<a class='btn btn-info float-right' href="{{route('subjects')}}">List</a>
</div>
<form action="{{route('subjects.store')}}" method="post" >
{{csrf_field()}}
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Name <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-12 col-xs-12" name="name" placeholder="Enter subject name" required="required" type="text">
@if ($errors->has('name'))
<span class="errormsg">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="description">Description</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea name='description' id='description' class='form-control' placeholder="Enter description"></textarea>
@if ($errors->has('description'))
<span class="errormsg">{{ $errors->first('description') }}</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input type="submit" name="submit" value='Submit' class='btn btn-success'>
</div>
</div>
</form>
</div>
</div>
@stop
subjects/edit.blade.php
<!-- Extends template page -->
@extends('layout.app')
<!-- Specify content -->
@section('content')
<h3>Edit Subject</h3>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<!-- Alert message (start) -->
@if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }}">
{{ Session::get('message') }}
</div>
@endif
<!-- Alert message (end) -->
<div class="actionbutton">
<a class='btn btn-info float-right' href="{{route('subjects')}}">List</a>
</div>
<form action="{{route('subjects.update',[$subject->id])}}" method="post" >
{{csrf_field()}}
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="description">Name <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-12 col-xs-12" name="name" placeholder="Enter subject name" required="required" type="text" value="{{old('name',$subject->name)}}">
@if ($errors->has('name'))
<span class="errormsg">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Description
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea name='description' id='description' class='form-control' placeholder="Enter description">{{old('description',$subject->description)}}</textarea>
@if ($errors->has('description'))
<span class="errormsg">{{ $errors->first('description') }}</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input type="submit" name="submit" value='Submit' class='btn btn-success'>
</div>
</div>
</form>
</div>
</div>
@stop