Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
InvestmentNovel

Latest Programming Tutorial Blog

Laravel 5.7 Crud Example Tutorial

/
/
/
342 Views

Laravel 5.7 Crud Example Tutorial is today’s leading topic. Laravel is recognized as one of the popular PHP frameworks. Bug fix releases 5.7 on September 4th, 2018. We create a simple Formula1 race system that allows you to create, update and delete operation(CRUD). I have listed some of the features of Laravel 5.7

#Laravel 5.7 Features & Updates

  1. Laravel Nova
  2. Email Verification
  3. Guest User Gates / Policies
  4. Symfony Dump Server
  5. Notification Localization
  6. Console Testing
  7. URL Generator & Callable Syntax
  8. Paginator Links
  9. Filesystem Read / Write Streams

If you want to know more information, then go to Laravel Documentation.

Laravel 5.7 Crud Example Tutorial

We are going to Configure Laravel Project.

#1: Install Laravel 5.7 Project

composer create-project --prefer-dist laravel/laravel laravel5.7crud

#2: Configure SQL Database

Now we can setup database credentials.

//.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.7crud
DB_USERNAME=root
DB_PASSWORD=

#3: Create Model and Migration File

php artisan make:model f1race -m

It will create the f1race.php file and also create a create_f1races_table.php  migration file.

public function up()
    {
        Schema::create('f1races', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('position');
            $table->string('drivername');
            $table->text('teamname');
            $table->timestamps();
        });
    }

Now, migrate table by the following command.

php artisan migrate

In the database, you can see the f1races table.

#4: Create a View File

You can create a file in resources/views/f1create.blade.php and put this following code in it.

<!-- f1racecreate.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel 5.7 Crud Example Tutorial </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h2>Laravel 5.7 Crud Example Tutorial</h2><br/>
      <form method="post" action="{{url('create')}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="position">Position:</label>
            <input type="text" class="form-control" name="position">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="drivername">Driver Name:</label>
              <input type="text" class="form-control" name="drivername">
            </div>
          </div>
          <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
            <label for="teamname">Team Name:</label>
            <input type="text" class="form-control" name="teamname">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Submit</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

#5: Create one controller

php artisan make:controller F1raceController

It will build the controller file called F1raceController.php.

//F1raceController.php

public function create()
    {
        return view('f1racecreate');
    }

#6: Define Route

We register all route in a web.php file

//web.php

Route::get('create','F1raceController@create');
Route::post('create','F1raceController@store');
Route::get('f1race','F1raceController@index');
Route::get('/edit/f1race/{id}','F1raceController@edit');
Route::post('/edit/f1race/{id}','F1raceController@update');
Route::delete('/delete/f1race/{id}','F1raceController@destroy');

Laravel 5.7 Crud Example

#7: Store data to the database

We need coding the store()  in sequence to store the data in the database.

//F1raceController.php

public function store(Request $request)
    {
        $f1race= new \App\f1race;
        $f1race->position=$request->get('position');
        $f1race->drivername=$request->get('drivername');
        $f1race->teamname=$request->get('teamname');
        $f1race->save();
        return redirect('f1race')->with('success', 'Data has been added');
    }

#8: Create an index page to list the data

For that, we want to forward the data to the f1raceindex.blade.php. So, in F1raceController.php file, we need to write the code to retrieve the data and return it to the f1race index view.

//F1raceController.php

public function index()
    {
        $f1races=\App\f1race::all();
        return view('f1raceindex',compact('f1races'));
    }

In resources/views produce individual blade file called f1raceindex.blade.php file and placed the subsequent code in it.

<!-- f1raceindex.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Index Page</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
  </head>
  <body>
    <div class="container">
    <br />
    @if (\Session::has('success'))
      <div class="alert alert-success">
        <p>{{ \Session::get('success') }}</p>
      </div><br />
     @endif
     <h2>Formula1 Race</h2>
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Position</th>
        <th>Driver Name</th>
        <th>Team Name</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>

      @foreach($f1races as $f1race)
      <tr>
        <td>{{$f1race['id']}}</td>
        <td>{{$f1race['position']}}</td>
        <td>{{$f1race['drivername']}}</td>
        <td>{{$f1race['teamname']}}</td>

        <td><a href="{{action('F1raceController@edit', $f1race['id'])}}" class="btn btn-warning">Edit</a></td>
        <td>
          <form action="{{action('F1raceController@destroy', $f1race['id'])}}" method="post">
            @csrf
            <input name="_method" type="hidden" value="DELETE">
            <button class="btn btn-danger" type="submit">Delete</button>
          </form>
        </td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
  </body>
</html>

So, when you type this URL: http://localhost:8000/f1race

Laravel 5.7 Crud Example

#9: Create an edit view for updating the data

Our next step will be the edit function in F1raceController.php file and set the following code in it.

//F1raceController.php

 public function edit($id)
    {
        $f1race = \App\f1race::find($id);
        return view('f1raceedit',compact('f1race','id'));
    }

Now, make an f1raceedit.blade.php file inside resources/views.

<!-- f1raceedit.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel 5.7 CRUD Tutorial With Example </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
  </head>
  <body>
    <div class="container">
      <h2>Edit Data</h2><br  />
        <form method="post" action="{{action('F1raceController@update', $id)}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="position">Position:</label>
            <input type="text" class="form-control" name="position" value="{{$f1race->position}}">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="drivername">Driver Name:</label>
              <input type="text" class="form-control" name="drivername" value="{{$f1race->drivername}}">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="teamname">Team Name:</label>
              <input type="text" class="form-control" name="teamname" value="{{$f1race->teamname}}">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success" style="margin-left:38px">Update</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

Next step would be to code update function.

//F1raceController.php

public function update(Request $request, $id)
    {
        $f1race= \App\f1race::find($id);
        $f1race->position=$request->get('position');
        $f1race->drivername=$request->get('drivername');
        $f1race->teamname=$request->get('teamname');
        $f1race->save();
        return redirect('f1race')->with('success', 'Data has been updated');
    }

You can see below screenshot that we can update the team name of Renault Sport Formula One Team to Renault.

Laravel 5.7 Tutorial For Beginners

#10: Remove the data

//StockController.php

public function destroy($id)
    {
        $f1race = \App\f1race::find($id);
        $f1race->delete();
        return redirect('f1race')->with('success','Data has been  deleted');
    }

You can see below screenshot that we can delete the data of Nico Hulkenberg.

Laravel 5.7 Crud

Finally, Our Laravel 5.7 Crud Example Tutorial is over. Thanks for taking.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This div height required for enabling the sticky sidebar