Laravel: Simple CRUD Operation
Submitted by nurhodelta_17 on Tuesday, November 14, 2017 - 16:17.
Getting Started
First, we're going to create a new project and I'm gonna name it crud and add it to localhost with the name crud.dev. If you have no idea on how to do this, please refer to my tutorial Installing Laravel - PHP Framework.Installing Laravel Collective
Next, we're gonna install Laravel Collective to handle our forms. You may visit the official website if you wanted to read the documentations using this link. 1. Open command prompt, navigate to our newly created project and type: composer require "laravelcollective/html":"^5.5.0" Note: Change the value of 5.5.0 depending on the laravel version that you are using. 2. After finishing the installation, open app.php located in config folder. Find 'providers' array and add the ff:
Collective\Html\HtmlServiceProvider::class,
Find 'aliases' array and add the ff:
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
Setting up our Database
1. Open your phpMyAdmin and create a new database. In my case, I've created a database named crud. 2. In our project, open .env file and update the ff lines depending on your setting.
DB_DATABASE=crud
DB_USERNAME=root
DB_PASSWORD=
Creating our Controller and Model
Next, we're going to create our controller and model to handle our table. 1. In command prompt, navigate to your project and type: php artisan make:controller MemberController This will create our controller in the form of MemberController.php located in app/Http/Controllers folder. 2. Open MemberController.php and edit it with the ff codes:- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Member;
- class MemberController extends Controller
- {
- public function index(){
- return view('show');
- }
- public function getMembers(){
- $members = Member::all();
- return view('show')->with('members', $members);
- }
- public function save(Request $request){
- $member = new Member;
- $member->firstname = $request->input('firstname');
- $member->lastname = $request->input('lastname');
- $member->save();
- return redirect('/');
- }
- public function update(Request $request, $id){
- $member = Member::find($id);
- $input = $request->all();
- $member->fill($input)->save();
- return redirect('/');
- }
- public function delete($id)
- {
- $members = Member::find($id);
- $members->delete();
- return redirect('/');
- }
- }
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Member extends Model
- {
- protected $fillable = ['firstname', 'lastname'];
- }
- <?php
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreateMembersTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('members', function (Blueprint $table) {
- $table->increments('id');
- $table->string('firstname');
- $table->string('lastname');
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('members');
- }
- }

Creating our Routes
In routes folder, open web.php and edit it with the ff codes:- <?php
- Route::get('/', 'MemberController@index');
- Route::get('/', 'MemberController@getMembers');
- Route::post('/save', 'MemberController@save');
- Route::patch('/update/{id}', ['as' => 'member.update', 'uses' => 'MemberController@update']);
- Route::delete('/delete/{id}', ['as' => 'member.delete', 'uses' => 'MemberController@delete']);
Creating our Views
In resources/views folder, create the ff files: app.blade.php- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
- <link rel="stylesheet" href="/css/app.css">
- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
- </head>
- <body>
- <div class="container">
- @yield('content')
- </div>
- @include('modal')
- </body>
- </html>
- @extends('app')
- @section('content')
- <div class="row">
- <div class="col-md-10 col-md-offset-1">
- <h2>Members Table
- </h2>
- </div>
- </div>
- <div class="row">
- <div class="col-md-10 col-md-offset-1">
- <table class="table table-bordered table-responsive table-striped">
- <thead>
- </thead>
- <tbody>
- @foreach($members as $member)
- <tr>
- @include('action')
- </td>
- </tr>
- @endforeach
- </tbody>
- </table>
- </div>
- </div>
- @endsection
- <!-- Add Modal -->
- <div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- </div>
- <div class="modal-body">
- {!! Form::open(['url' => 'save']) !!}
- <div class="form-group">
- <div class="row">
- <div class="col-md-2" style="margin-top:7px;">
- {!! Form::label('firstname', 'Firstname') !!}
- </div>
- <div class="col-md-10">
- {!! Form::text('firstname', '', ['class' => 'form-control', 'placeholder' => 'Input Firstname', 'required']) !!}
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="row">
- <div class="col-md-2" style="margin-top:7px;">
- {!! Form::label('lastname', 'Lastname') !!}
- </div>
- <div class="col-md-10">
- {!! Form::text('lastname', '', ['class' => 'form-control', 'placeholder' => 'Input Lastname', 'required']) !!}
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- {!! Form::close() !!}
- </div>
- </div>
- </div>
- </div>
- <!-- Edit Modal -->
- <div class="modal fade" id="edit{{$member->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- </div>
- <div class="modal-body">
- {!! Form::model($members, [ 'method' => 'patch','route' => ['member.update', $member->id] ]) !!}
- <div class="form-group">
- <div class="row">
- <div class="col-md-2" style="margin-top:7px;">
- {!! Form::label('firstname', 'Firstname') !!}
- </div>
- <div class="col-md-10">
- {!! Form::text('firstname', $member->firstname, ['class' => 'form-control']) !!}
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="row">
- <div class="col-md-2" style="margin-top:7px;">
- {!! Form::label('lastname', 'Lastname') !!}
- </div>
- <div class="col-md-10">
- {!! Form::text('lastname', $member->lastname, ['class' => 'form-control']) !!}
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- {!! Form::close() !!}
- </div>
- </div>
- </div>
- </div>
- <!-- Delete Modal -->
- <div class="modal fade" id="delete{{$member->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- </div>
- <div class="modal-body">
- {!! Form::model($members, [ 'method' => 'delete','route' => ['member.delete', $member->id] ]) !!}
- </div>
- <div class="modal-footer">
- {!! Form::close() !!}
- </div>
- </div>
- </div>
- </div>
Running our Project
In your web browser, type the name that you added in localhost for your project in my case, crud.dev. You should now be able to CRUD. That ends this tutorial. Happy Coding :)Comments
Add new comment
- Add new comment
- 7177 views