Jason Thomas

Jason Thomas

1567996446

Laravel 6 tutorial – Build Your First CRUD App with Example

Originally published at https://w3path.com

We will create CRUD (create/read/update/delete) for product management. We will validate crud application forms with server-side validation. In this product crud management, we will use the resource controller and route.

Contents

  • Install Laravel Fresh Project
  • Generate Model and Migration
  • Create Resource Route & Controller
  • Create the blade view
  • Start Development Server
  • Conclusion

Install Laravel Fresh Project

We need to install Laravel 6 fresh application using below command, Open your command prompt and run the below command :

composer create-project --prefer-dist laravel/laravel Blog

After successfully install Laravel 6 Application, Go to your project root directory and find the .env file and setup database credential, If you don’t find the .env file then rename the .env.example file to .env

Configuration in .env

In this step, we will set database credential in .env file. Let’s open .env file.

 
DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

Generate Model and Migration

Now we will create table named Products and its migration file. use the below command:

php artisan make:model Product-m

This command will create a model named Products and also create one migration file for Product table. After successfully run the command go to database/migrations file and put the below here :

 
<?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(‘title’);
$table->string(‘product_code’);
$table->text(‘description’);
$table->timestamps();
});
}

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

}

Next, migrate the table using the below command.

php artisan migrate

If you found any query builder error in command prompt go to => app\Providers\AppServiceProvider.php and put the below code here :

 use Illuminate\Support\Facades\Schema;
 
public function boot()
{
    Schema::defaultStringLength(191);
}

And then run this below command :

 php artisan migrate:fresh

Now, add the fillable property inside Book.php file.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
‘title’,
‘product_code’,
‘description’,
];
}

Create Resource Route & Controller

Create the ProductController using the below command.

php artisan make:controller ProductController  --resource

This command will create a contoller name ProductController and also inside by default seven methods like index, store, create, update, destroy, show, edit.

Next, We need to add the resource route. Go to routes/web.php put the below routes here :

<?php

Route::get(‘/’, function () {
return view(‘welcome’);
});

Route::resource(‘products’, ‘ProductController’);

Next open controller, Go to app/HTTP/Controller/ProductController and put the below code here :

<?php
   
namespace App\Http\Controllers;
   
use App\Product;
use Illuminate\Http\Request;
use Redirect;
use PDF;
   
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     /
    public function index()
    {
        $data[‘products’] = Product::orderBy(‘id’,‘desc’)->paginate(10);
   
        return view(‘product.list’,$data);
    }
    
    /**
     
Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     /
    public function create()
    {
        return view(‘product.create’);
    }
   
    /**
     
Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     /
    public function store(Request $request)
    {
        $request->validate([
            ‘title’ => ‘required’,
            ‘product_code’ => ‘required’,
            ‘description’ => ‘required’,
        ]);
   
        Product::create($request->all());
    
        return Redirect::to(‘products’)
       ->with(‘success’,‘Greate! Product created successfully.’);
    }
    
    /**
     
Display the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     /
    public function show(Request $request)
    {
         
    }
    
    /**
     
Show the form for editing the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     /
    public function edit($id)
    {  
        $where = array(‘id’ => $id);
        $data[‘product_info’] = Product::where($where)->first();
 
        return view(‘product.edit’, $data);
    }
   
    /**
     
Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     /
    public function update(Request $request, $id)
    {
        $request->validate([
            ‘title’ => ‘required’,
            ‘product_code’ => ‘required’,
            ‘description’ => ‘required’,
        ]);
         
        $update = [‘title’ => $request->title, ‘description’ => $request->description];
        Product::where(‘id’,$id)->update($update);
   
        return Redirect::to(‘products’)
       ->with(‘success’,‘Great! Product updated successfully’);
    }
   
    /**
     
Remove the specified resource from storage.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Product::where(‘id’,$id)->delete();
   
        return Redirect::to(‘products’)->with(‘success’,‘Product deleted successfully’);
    }
     
}

Create the blade view

Next, We need to create some blade view files, Go to app/resources/views/ and create one folder name product. Inside the product folder create the following blade files.

  1. Layout.blade.php
  2. List.blade.php
  3. Create.blade.php
  4. Edit.blade.php
Layout.blade.php


<!DOCTYPE html>
<html>
  <head>
    <meta charset=“UTF-8”>
    <meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
    <meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
    <meta name=“csrf-token” content=“{{ csrf_token() }}”>
    <title>Laravel CRUD Tutorial With Example - Tutsmake.com</title>
    <link href=“//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css” rel=“stylesheet” id=“bootstrap-css”>
    <style>
    body{
    background-color: #25274d;
    }
    .container{
    background: #ff9b00;
    padding: 4%;
    border-top-left-radius: 0.5rem;
    border-bottom-left-radius: 0.5rem;
    }
    </style>
  </head>
  <body>
    <div class=“container”>
      <br><br><br>
      @yield(‘content’)
    </div>
  </body>
</html>
List.blade.php  
@extends(‘product.layout’)
   
@section(‘content’)
  <a href=“{{ route(‘products.create’) }}” class=“btn btn-success mb-2”>Add</a>
  <br>
   <div class=“row”>
        <div class=“col-12”>
          
          <table class=“table table-bordered” id=“laravel_crud”>
           <thead>
              <tr>
                 <th>Id</th>
                 <th>Title</th>
                 <th>Product Code</th>
                 <th>Description</th>
                 <th>Created at</th>
                 <td colspan=“2”>Action</td>
              </tr>
           </thead>
           <tbody>
              @foreach($products as $product)
              <tr>
                 <td>{{ $product->id }}</td>
                 <td>{{ $product->title }}</td>
                 <td>{{ $product->product_code }}</td>
                 <td>{{ $product->description }}</td>
                 <td>{{ date(‘Y-m-d’, strtotime($product->created_at)) }}</td>
                 <td><a href=“{{ route(‘products.edit’,$product->id)}}” class=“btn btn-primary”>Edit</a></td>
                 <td>
                 <form action=“{{ route(‘products.destroy’, $product->id)}}” method=“post”>
                  {{ csrf_field() }}
                  @method(‘DELETE’)
                  <button class=“btn btn-danger” type=“submit”>Delete</button>
                </form>
                </td>
              </tr>
              @endforeach
           </tbody>
          </table>
          {!! $products->links() !!}
       </div>
   </div>
 @endsection  
Create.blade.php
@extends(‘product.layout’)
 
@section(‘content’)
<h2 style=“margin-top: 12px;” class=“text-center”>Add Product</a></h2>
<br>
 
<form action=“{{ route(‘products.store’) }}” method=“POST” name=“add_product”>
{{ csrf_field() }}
 
<div class=“row”>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Title</strong>
            <input type=“text” name=“title” class=“form-control” placeholder=“Enter Title”>
            <span class=“text-danger”>{{ $errors->first(‘title’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Product Code</strong>
            <input type=“text” name=“product_code” class=“form-control” placeholder=“Enter Product Code”>
            <span class=“text-danger”>{{ $errors->first(‘product_code’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Description</strong>
            <textarea class=“form-control” col=“4” name=“description” placeholder=“Enter Description”></textarea>
            <span class=“text-danger”>{{ $errors->first(‘description’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <button type=“submit” class=“btn btn-primary”>Submit</button>
    </div>
</div>
 
</form>
@endsection
Edit.blade.php
@extends(‘product.layout’)
 
@section(‘content’)
<h2 style=“margin-top: 12px;” class=“text-center”>Edit Product</a></h2>
<br>
 
<form action=“{{ route(‘products.update’, $product_info->id) }}” method=“POST” name=“update_product”>
{{ csrf_field() }}
@method(‘PATCH’)
 
<div class=“row”>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Title</strong>
            <input type=“text” name=“title” class=“form-control” placeholder=“Enter Title” value=“{{ $product_info->title }}”>
            <span class=“text-danger”>{{ $errors->first(‘title’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Product Code</strong>
            <input type=“text” name=“product_code” class=“form-control” placeholder=“Enter Product Code” value=“{{ $product_info->product_code }}”>
            <span class=“text-danger”>{{ $errors->first(‘product_code’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <div class=“form-group”>
            <strong>Description</strong>
            <textarea class=“form-control” col=“4” name=“description” placeholder=“Enter Description” >{{ $product_info->description }}</textarea>
            <span class=“text-danger”>{{ $errors->first(‘description’) }}</span>
        </div>
    </div>
    <div class=“col-md-12”>
        <button type=“submit” class=“btn btn-primary”>Submit</button>
    </div>
</div>
 
</form>
@endsection

Start Development Server

In this step, we will use the PHP artisan serve command. It will start your server locally

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now we are ready to run our example open the below URL in the browser.

http://localhost:8000/products

Conclusion

In this article, We have successfully created Laravel 6 CRUD Application (Create, Read, Update, Delete) with example. Our examples run quickly.

Live Demo

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

Laravel 5.8 Tutorial for Beginners

Upgrading Laravel To 6.0 From 5.8

Laravel 6 Release New Features and Upgrade

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial


#laravel #php #web-development #rest #api

What is GEEK

Buddha Community

Laravel 6 tutorial – Build Your First CRUD App with Example

Laravel AJAX CRUD Example Tutorial

Hello Guys,

Today I will show you how to create laravel AJAX CRUD example tutorial. In this tutorial we are implements ajax crud operation in laravel. Also perform insert, update, delete operation using ajax in laravel 6 and also you can use this ajax crud operation in laravel 6, laravel 7. In ajax crud operation we display records in datatable.

Read More : Laravel AJAX CRUD Example Tutorial

https://www.techsolutionstuff.com/post/laravel-ajax-crud-example-tutorial


Read Also : Read Also : Laravel 6 CRUD Tutorial with Example

https://techsolutionstuff.com/post/laravel-6-crud-tutorial-with-example

#laravel ajax crud example tutorial #ajax crud example in laravel #laravel crud example #laravel crud example with ajax #laravel #php

Laravel 6 CRUD Tutorial with Example

In this example, I will show you to how to make simple laravel CRUD(insert, update, delete or listing) operations with example.

Insert Update Delete module is primary requirement for each project,you will understand how to use route, controller, blade files, model and migration for crud operation in laravel 6.

We just need to follow below step and you will get basic CRUD using controller, model, route, bootstrap 4 and blade. If you follow below step then definatly you will get proper output.

Read More : Laravel 6 CRUD Tutorial with Example

https://www.techsolutionstuff.com/post/laravel-6-crud-tutorial-with-example

#laravel 6 crud tutorial with example #laravel 6 tutorial #crud tutorial #laravel #php #jquery

I am Developer

1617089618

Laravel 8 Tutorial for Beginners

Hello everyone! I just updated this tutorial for Laravel 8. In this tutorial, we’ll go through the basics of the Laravel framework by building a simple blogging system. Note that this tutorial is only for beginners who are interested in web development but don’t know where to start. Check it out if you are interested: Laravel Tutorial For Beginners

Laravel is a very powerful framework that follows the MVC structure. It is designed for web developers who need a simple, elegant yet powerful toolkit to build a fully-featured website.

Recommended:-Laravel Try Catch

#laravel 8 tutorial #laravel 8 tutorial crud #laravel 8 tutorial point #laravel 8 auth tutorial #laravel 8 project example #laravel 8 tutorial for beginners

Laravel 8 CRUD Operation Example

Hello Friend,

As you know Laravel 8 already officially released and today I will show you how to create CRUD operation in laravel 8, I have already perform many CRUD operations in my previous post like CRUD operation in ajax, CRUD operation in laravel 6 etc. So, today I will give you laravel 8 CRUD application example.

Laravel 8 CRUD Operation Example

https://websolutionstuff.com/post/laravel-8-crud-operation-example

#laravel #php #laravel 8 crud operation example #crud operation #laravel 8 crud tutorial #crud operation in laravel 8

Fredy  Larson

Fredy Larson

1595059664

How long does it take to develop/build an app?

With more of us using smartphones, the popularity of mobile applications has exploded. In the digital era, the number of people looking for products and services online is growing rapidly. Smartphone owners look for mobile applications that give them quick access to companies’ products and services. As a result, mobile apps provide customers with a lot of benefits in just one device.

Likewise, companies use mobile apps to increase customer loyalty and improve their services. Mobile Developers are in high demand as companies use apps not only to create brand awareness but also to gather information. For that reason, mobile apps are used as tools to collect valuable data from customers to help companies improve their offer.

There are many types of mobile applications, each with its own advantages. For example, native apps perform better, while web apps don’t need to be customized for the platform or operating system (OS). Likewise, hybrid apps provide users with comfortable user experience. However, you may be wondering how long it takes to develop an app.

To give you an idea of how long the app development process takes, here’s a short guide.

App Idea & Research

app-idea-research

_Average time spent: two to five weeks _

This is the initial stage and a crucial step in setting the project in the right direction. In this stage, you brainstorm ideas and select the best one. Apart from that, you’ll need to do some research to see if your idea is viable. Remember that coming up with an idea is easy; the hard part is to make it a reality.

All your ideas may seem viable, but you still have to run some tests to keep it as real as possible. For that reason, when Web Developers are building a web app, they analyze the available ideas to see which one is the best match for the targeted audience.

Targeting the right audience is crucial when you are developing an app. It saves time when shaping the app in the right direction as you have a clear set of objectives. Likewise, analyzing how the app affects the market is essential. During the research process, App Developers must gather information about potential competitors and threats. This helps the app owners develop strategies to tackle difficulties that come up after the launch.

The research process can take several weeks, but it determines how successful your app can be. For that reason, you must take your time to know all the weaknesses and strengths of the competitors, possible app strategies, and targeted audience.

The outcomes of this stage are app prototypes and the minimum feasible product.

#android app #frontend #ios app #minimum viable product (mvp) #mobile app development #web development #android app development #app development #app development for ios and android #app development process #ios and android app development #ios app development #stages in app development