Laravel 6 CRUD Application Tutorial

Laravel 6 CRUD Application Tutorial

In this tutorial, you will learn very basic CRUD operation with Laravel new version 6. I am going to show you step by step from scratch so, I will better to understand if you are new in Laravel.

Insert Update Delete module is primary requirement for each project, so in this tutorial i will give you step by step instruction for creating crud (Create Read Update Delete) Application in Laravel 6. you will understand how to use resource route, controller, blade files, model and migration for crud operation in Laravel 6.

Laravel 6 is just released by tomorrow, Laravel 6 gives several new features and LTS support. So if you are new to laravel then this tutorial will help you create insert update delete application in Laravel 6.

You just need to follow few step and you will get basic crud stuff using controller, model, route, bootstrap 4 and blade..

In this tutorial, you will learn very basic crud operation with Laravel new version 6. I am going to show you step by step from scratch so, i will better to understand if you are new in Laravel.

Step 1 : Install Laravel 6

first of all we need to get fresh Laravel 6 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Database Configuration

In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel 6. So let's open .env file and fill all details like as bellow:

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
Step 3: Create Migration

we are going to create crud application for product. so we have to create migration for "products" table using Laravel 6 php artisan command, so first fire bellow command:

php artisan make:migration create_products_table --create=products

After this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create products table.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('detail');
$table->timestamps();
});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('products');
}

}

Now you have to run this migration by following command:

php artisan migrate
Step 4: Add Resource Route

Here, we need to add resource route for product crud application. so open your "routes/web.php" file and add following route.

routes/web.php

Route::resource('products','ProductController');
Step 5: Add Controller and Model

In this step, now we should create new controller as ProductController. So run bellow command and create new controller. bellow controller for create resource controller.

php artisan make:controller ProductController --resource --model=Product

After bellow command you will find new file in this path "app/Http/Controllers/ProductController.php".

In this controller will create seven methods by default as bellow methods:

1) index()

2) create()

3) store()

4) show()

5) edit()

6) update()

7) destroy()

So, let's copy bellow code and put on ProductController.php file.

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::latest()->paginate(5);

    return view('products.index',compact('products'))
        -&gt;with('i', (request()-&gt;input('page', 1) - 1) * 5);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('products.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $request-&gt;validate([
        'name' =&gt; 'required',
        'detail' =&gt; 'required',
    ]);

    Product::create($request-&gt;all());

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product created successfully.');
}

/**
 * Display the specified resource.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function show(Product $product)
{
    return view('products.show',compact('product'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function edit(Product $product)
{
    return view('products.edit',compact('product'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, Product $product)
{
    $request-&gt;validate([
        'name' =&gt; 'required',
        'detail' =&gt; 'required',
    ]);

    $product-&gt;update($request-&gt;all());

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product updated successfully');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function destroy(Product $product)
{
    $product-&gt;delete();

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product deleted successfully');
}

}

Ok, so after run bellow command you will find "app/Product.php" and put bellow content in Product.php file:

app/Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
protected $fillable = [
'name', 'detail'
];
}

Step 6: Add Blade Files

In last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder "products" then create blade files of crud app. So finally you have to create following bellow blade file:

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

So let's just create following file and put bellow code.

resources/views/products/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 6 CRUD Application - ItSolutionStuff.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>

<div class="container">
@yield('content')
</div>

</body>
</html>

resources/views/products/index.blade.php

@extends('products.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 6 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a>
</div>
</div>
</div>

@if ($message = Session::get('success'))
    &lt;div class="alert alert-success"&gt;
        &lt;p&gt;{{ $message }}&lt;/p&gt;
    &lt;/div&gt;
@endif

&lt;table class="table table-bordered"&gt;
    &lt;tr&gt;
        &lt;th&gt;No&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Details&lt;/th&gt;
        &lt;th width="280px"&gt;Action&lt;/th&gt;
    &lt;/tr&gt;
    @foreach ($products as $product)
    &lt;tr&gt;
        &lt;td&gt;{{ ++$i }}&lt;/td&gt;
        &lt;td&gt;{{ $product-&gt;name }}&lt;/td&gt;
        &lt;td&gt;{{ $product-&gt;detail }}&lt;/td&gt;
        &lt;td&gt;
            &lt;form action="{{ route('products.destroy',$product-&gt;id) }}" method="POST"&gt;

                &lt;a class="btn btn-info" href="{{ route('products.show',$product-&gt;id) }}"&gt;Show&lt;/a&gt;

                &lt;a class="btn btn-primary" href="{{ route('products.edit',$product-&gt;id) }}"&gt;Edit&lt;/a&gt;

                @csrf
                @method('DELETE')
  
                &lt;button type="submit" class="btn btn-danger"&gt;Delete&lt;/button&gt;
            &lt;/form&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    @endforeach
&lt;/table&gt;

{!! $products-&gt;links() !!}

@endsection

resources/views/products/create.blade.php

@extends('products.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>

@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('products.store') }}" method="POST">
@csrf

 &lt;div class="row"&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Name:&lt;/strong&gt;
            &lt;input type="text" name="name" class="form-control" placeholder="Name"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Detail:&lt;/strong&gt;
            &lt;textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"&gt;&lt;/textarea&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12 text-center"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;

</form>
@endsection

resources/views/products/edit.blade.php

@extends('products.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>

@if ($errors-&gt;any())
    &lt;div class="alert alert-danger"&gt;
        &lt;strong&gt;Whoops!&lt;/strong&gt; There were some problems with your input.&lt;br&gt;&lt;br&gt;
        &lt;ul&gt;
            @foreach ($errors-&gt;all() as $error)
                &lt;li&gt;{{ $error }}&lt;/li&gt;
            @endforeach
        &lt;/ul&gt;
    &lt;/div&gt;
@endif

&lt;form action="{{ route('products.update',$product-&gt;id) }}" method="POST"&gt;
    @csrf
    @method('PUT')

     &lt;div class="row"&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;strong&gt;Name:&lt;/strong&gt;
                &lt;input type="text" name="name" value="{{ $product-&gt;name }}" class="form-control" placeholder="Name"&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;strong&gt;Detail:&lt;/strong&gt;
                &lt;textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"&gt;{{ $product-&gt;detail }}&lt;/textarea&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12 text-center"&gt;
          &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;

&lt;/form&gt;

@endsection

resources/views/products/show.blade.php

@extends('products.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>

&lt;div class="row"&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Name:&lt;/strong&gt;
            {{ $product-&gt;name }}
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Details:&lt;/strong&gt;
            {{ $product-&gt;detail }}
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

@endsection

Now we are ready to run our crud application example with Laravel 6 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/products

You can download code from GitHub

I hope it can help you....

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Top Laravel Development Companies Reviews

Top Laravel Development Companies Reviews

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here...

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here have passed the stringent assessments carried out by TopDevelopers in order to get approved as top Laravel service providers. Get in touch with the most sought-after Laravel developers in the market for your assistance.

Here is the List of Best Laravel Development Companies.