Laravel 5.6 Tutorial For Beginners

Laravel 5.6 Tutorial For Beginners is today’s leading topic. Laravel is a free, open-source PHP web framework. It follows MVC(Model View Controller) Architecture. In this example, we create a simple stock system that allows you to create, update and delete operation(CRUD). You have followed below step to create CRUD application in Laravel 5.6. I have listed some of features of Laravel 5.6

1)Logging Improvements

2)Argon2 Password Hashing

3)UUID Methods

4)Single Server Task Scheduling

5)New Blade Directives

6)Dynamic Rate Limiting

7)Bootstrap

8)API Controller Generation

Laravel 5.6 Tutorial For Beginners

We are going to Configure Laravel Project.

#1: Install Laravel Project

Install Laravel 5.6 Project by the typing following command.

composer create-project --prefer-dist laravel/laravel 5.6tutorialbeginner

#2: Configure SQL Database

Now we can setup database credentials.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorialforbeginner
DB_USERNAME=root
DB_PASSWORD=

#3: Build Model and Migration File

 php artisan make:model Stocks -m

It will create the Stocks.php file and also create a create_stocks_table.php  migration file.

//// create_stocks_table

public function up()
    {
        Schema::create('stocks', function (Blueprint $table) {
            $table->increments('id');
            $table->string('stockname');
            $table->integer('stockprice');
            $table->text('description');
            $table->timestamps();
        });
    }

Now, migrate the table by the following command.

php artisan migrate

In the database, you can see the stocks table.

#4: Create a View File

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

<!-- stockcreate.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel 5.6 Tutorial For Beginners </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="https://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.6 Tutorial For Beginners</h2><br/>
      <form method="post" action="{{url('stockcreate')}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="StockName">Stock Name:</label>
            <input type="text" class="form-control" name="stockname">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="StockPrice">Stock Price:</label>
              <input type="text" class="form-control" name="stockprice">
            </div>
          </div>
          <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
            <label for="description">Description:</label>
            <textarea class="form-control" rows="5" name="description"></textarea>
            </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 StockController

It will build the controller file called StockController.php.

//StockController.php

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

#6: Define Route

We register all route in a web.php file.

//web.php

Route::get('stockcreate','StockController@create');
Route::post('stockcreate','StockController@store');
Route::get('stocks','StockController@index');
Route::get('/edit/stock/{id}','StockController@edit');
Route::post('/edit/stock/{id}','StockController@update');
Route::delete('/delete/stock/{id}','StockController@destroy');

laravel 5.6 tutorial

#7: Store data to the database

We need coding the store function in sequence to store the data in the database.

//StockController.php

public function store(Request $request)
    {
        $stock= new \App\Stocks;
        $stock->stockname=$request->get('stockname');
        $stock->stockprice=$request->get('stockprice');
        $stock->description=$request->get('description');
        $stock->save();
        return redirect('stocks')->with('success', 'Stock has been added');
    }

#8: Make an index page to list the data.

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

//StockController.php

public function index()
    {
        $stocks=\App\Stocks::all();
        return view('stockindex',compact('stocks'));
    }

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

<!-- stockindex.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>NYSE Stock</h2>
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Stock Name</th>
        <th>Stock Price</th>
        <th>Description</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>

      @foreach($stocks as $stock)
      <tr>
        <td>{{$stock['id']}}</td>
        <td>{{$stock['stockname']}}</td>
        <td>{{$stock['stockprice'].'$'}}</td>
        <td>{{$stock['description']}}</td>

        <td><a href="{{action('StockController@edit', $stock['id'])}}" class="btn btn-warning">Edit</a></td>
        <td>
          <form action="{{action('StockController@destroy', $stock['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/stocks

laravel 5.6 tutorial with example

#9: Build an edit view for updating the data.

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

//StockController.php

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

Now, make a stockedit.blade.php file inside resources/views.

<!-- stockedit.blade.php -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel 5.6 CRUD Tutorial With Example </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
  </head>
  <body>
    <div class="container">
      <h2>Edit Stock</h2><br  />
        <form method="post" action="{{action('StockController@update', $id)}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="stockname">Stock Name:</label>
            <input type="text" class="form-control" name="stockname" value="{{$stock->stockname}}">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="stockprice">Stock Price:</label>
              <input type="text" class="form-control" name="stockprice" value="{{$stock->stockprice}}">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="description">Description:</label>
              <textarea class="form-control" rows="5" name="description">{{ $stock->description }}</textarea>
            </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.

//StockController.php

public function update(Request $request, $id)
    {
        $stock= \App\Stocks::find($id);
        $stock->stockname=$request->get('stockname');
        $stock->stockprice=$request->get('stockprice');
        $stock->description=$request->get('description');
        $stock->save();
        return redirect('stocks')->with('success','Stock has been  updated');
    }

You can see below screenshot that we can update the price of Netflix 316$ to 310$.

laravel 5.6 example

#10: Remove the data

//StockController.php

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

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

laravel 5.6 crud example

Final Code of StockController.php looks like below

//StockController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StockController extends Controller
{
    public function create()
    {
        return view('stockcreate');
    }
    public function store(Request $request)
    {
        $stock= new \App\Stocks;
        $stock->stockname=$request->get('stockname');
        $stock->stockprice=$request->get('stockprice');
        $stock->description=$request->get('description');
        $stock->save();
        return redirect('stocks')->with('success', 'Stock has been added');
    }
    public function index()
    {
        $stocks=\App\Stocks::all();
        return view('stockindex',compact('stocks'));
    }
    public function edit($id)
    {
        $stock = \App\Stocks::find($id);
        return view('stockedit',compact('stock','id'));
    }
    public function update(Request $request, $id)
    {
        $stock= \App\Stocks::find($id);
        $stock->stockname=$request->get('stockname');
        $stock->stockprice=$request->get('stockprice');
        $stock->description=$request->get('description');
        $stock->save();
        return redirect('stocks')->with('success','Stock has been  updated');
    }
    public function destroy($id)
    {
        $stock = \App\Stocks::find($id);
        $stock->delete();
        return redirect('stocks')->with('success','Stock has been  deleted');
    }
}

Finally, Our Laravel 5.6 Tutorial For Beginners is over. Thanks for taking.

Post Your Thoughts

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