How to Install Laravel and Create first View Page in Laravel ?

In this tutorial im going to describe how to install laravel project and create first view pages, so follow some easy steps define below.

First let’s go to install laravel project

composer create-project laravel/laravel learning-project "5.8.*"

lets go to .env folder and put database name and connect to database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=learning-project
DB_USERNAME=root
DB_PASSWORD=

Now migrate the table

php artisan migrate

Now Open your project and go to Resouces/view folder and create folllowing files

create.blade.php
dashboard.blade.php
home.blade.php
blog.blade.php

Go to create.blade.php and paste below code

<h1>this is create pages</h1>

Next go to dashboard.blade.php and paste below code

<h1>this is dashboard pages</h1>

Now create blog.blade.php file and paste below lines.

<h1>this is blog pages</h1>

Next go to Route/web.php and add below line

Go to Route/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Route::get('/dashboard', function (){
return view('dashboard');
});

Route::get('/create', function (){
return view('create');
});

Route::get('/blog', function (){
return view('blog');
});

And last one serve below code in your terminal.

php artisan serve

Copy below url and paste in your browser.

http://127.0.0.1:8000/blog
http://127.0.0.1:8000/create

Thank i hope its helpfull for you. 🙏🙏🙏Amit Kumar

Tagged : / /

Multi File upload with Image preview and checklist delete options

In this tutorial i’m going to describe how to upload multiple image with crud operation edit, update delete in laravel. Image upload is necessary of every project. In this project i’m going to learn how to upload with validation like images, mimes, max file upload and etc, with image preview when uploading image and adding features checklist delete.

So let’s go to download project and configure

Please follow some easy steps define below.

First let’s go to install laravel project

composer create-project laravel/laravel admin-dashboard "5.8.*"

After Installation setup database So go to the .env file and add the database credentials. lets go to .env folder and put database name and connect to database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=admin-dashboard
DB_USERNAME=root
DB_PASSWORD=

Now migrate the table

php artisan migrate

Next step: Add Resource Route Go to your route and paste below code 👇👇

use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);

Now lets go to create controller, model and migration file go to your terminal and paste below code

php artisan make:model ProductController -mcr

Lets go to your migration file productstable and paste below code. 👇

<?phpuse 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->bigIncrements('id');
$table->string('name');
$table->string('detail');
$table->string('image');
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}

Now migrate the table 👇👇

php artisan migrate

Next go to Your ProductController file and paste below code 👇👇

<?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'))
->with('i', (request()->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->validate([
'name' => 'required',
'name' => 'required',
'detail' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);

$input = $request->all();

if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}

Product::create($input);

return redirect()->route('products.index')
->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->validate([
'name' => 'required',
'detail' => 'required'
]);

$input = $request->all();

if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}

$product->update($input);

return redirect()->route('products.index')
->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->delete();

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


}

Next go to your Model

app/Models/Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

Now create some file as define below.

Go to your resources/view folder and create following files inside the products folder as define below file like

1) layout.blade.php
2) index.blade.php
3) create.blade.php
4) edit.blade.php
5) show.blade.php

Next go to resouce/view/products/layout file and paste below code

Go to your app.blade.php file and paste below code

<!DOCTYPE html>
<html>
<head>
<title>Multiple Image upload with crud by Laravel Amit</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>

Next go to products/index.blade.php and paste below code

@extends('products.layout')
@section('content')
<br><br>
<div class="container" style="margin-right: -50px;">
   <div class="row">
      <div class="col-lg-12 margin-tb">
         <div class="pull-left">
            <h2>Multiple Image upload with crud by Laravel Amit</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'))
   <div class="alert alert-success">
      <p>{{ $message }}</p>
   </div>
   @endif
   <table class="table table-bordered">
      <tr>
         <th width="50px"><input type="checkbox" id="master"></th>
         <th>No</th>
         <th>Image</th>
         <th>Name</th>
         <th>Email</th>
         <th>Details</th>
         <th width="280px">Action</th>
      </tr>
      @foreach ($products as $product)
      <tr>
         <td><input type="checkbox" class="sub_chk" data-id="{{$product->id}}"></td>
         <td>{{ ++$i }}</td>
         <td><img src="/image/{{ $product->image }}" width="100px"></td>
         <td>{{ $product->name }}</td>
         <td>{{ $product->email }}</td>
         <td>{{ $product->detail }}</td>
         <td>
            <form action="{{ route('products.destroy',$product->id) }}" method="POST">
               <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
               <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
               @csrf
               @method('DELETE')
               <button type="submit" class="btn btn-danger">Delete</button>
            </form>
         </td>
      </tr>
      @endforeach
   </table>
   {!! $products->links() !!}
</div>
@endsection
Next create files inside the products folder 👇👇
create.blade.php

Go to create.blade.php file and paste below code

@extends('products.layout')
@section('content')

<div class="container" style="margin-right: -87px;">
<div class="row">
<div class="col-lg-6">
<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>
<br><br>
@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" enctype="multipart/form-data">
@csrf

<div class="row">

<div class="col-md-12">
<div class="form-group">
<input type="file" name="image" placeholder="Choose image" id="image">
@error('image')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>

<!-- <div class="col-md-12 mb-2">
<img id="amit-image-before-upload" src="https://www.riobeauty.co.uk/images/product_image_not_found.gif"
alt="preview image" style="max-height: 250px;">
</div> -->
<!-- preview code testing -->
<div class="col-md-12 mb-2">
<img id="amit-image-before-upload" src="https://www.riobeauty.co.uk/images/product_image_not_found.gif"
alt="preview image" style="max-height: 250px;">
</div>
<!-- preview code test ending -->
<div class="col-lg-12 col-sm-6 col-md-6 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

</form>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function (e) {


$('#image').change(function(){

let reader = new FileReader();

reader.onload = (e) => {

$('#amit-image-before-upload').attr('src', e.target.result);
}

reader.readAsDataURL(this.files[0]);

});

});

</script>


@endsection

Next create files inside the products folder 👇👇

edit.blade.php

Copy below code and paste edit.blade.php file

@extends('products.layout')

@section('content')
<br><br><br>
<div class="container" style="margin-right: -87px;">
<div class="row">
<div class="col-lg-4 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->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.update',$product->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')

<div class="row">
<div class="col-lg-2 col-sm-2 col-md-2">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-lg-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Email:</strong>
<input type="text" name="name" value="{{ $product->email }}" class="form-control" placeholder="email">
</div>
</div>
<br>
<div class="col-lg-7 col-sm-6 col-md-6">
<div class="form-group">
<strong>Detail:</strong>
<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea>
</div>
</div>
<div class="col-lg-4 col-sm-3 col-md-3">
<div class="form-group">
<strong>Image:</strong>
<input type="file" name="image" class="form-control" placeholder="image">
<img src="/image/{{ $product->image }}" width="300px">
</div>
</div>
<div class="col-lg-4 col-sm-4 col-md-4 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

</form>
</div>
@endsection

Next create files inside the products folder 👇👇

show.blade.php

Copy below code and paste show.blade.php file 👇👇

@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>

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $product->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Email:</strong>
{{ $product->email }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $product->detail }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
<img src="/image/{{ $product->image }}" width="500px">
</div>
</div>
</div>
@endsection

Now go to your terminal paste below code

php artisan serve
http://127.0.0.1:8000/products

Now its showing like this

Now you can see before submit the data image preview image showing fine

These steps are implement on my own learning project and looks like this

Thanks i hope its helpfull for you…. 🙏🙏🙏 if you have any questions comment below. 🙋🙋

Tagged : / / /

How to Upload Multiple Image in Laravel

In this turoial im going to learn how to upload multiple image in laravel and store in database. Image upload is necessary of every project. In this project i’m going to learn how to upload with validation like images, mimes, max file upload and etc.

So let’s go to download project and configure with database. so copy below code and paste in your terminal.

First let’s go to install laravel project

composer create-project laravel/laravel admin-dashboard "5.8.*"

After Installation setup database So go to the .env file and add the database credentials. lets go to .env folder and put database name and connect to database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=admin-dashboard
DB_USERNAME=root
DB_PASSWORD=

Now migrate the table

php artisan migrate

Lets go to create migration file to store the image name go to your terminal and paste below code.

php artisan make:migration create_forms_table

Let’ go to migration table and add file name.

<?php

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

class CreateFormsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('filename');
$table->timestamps();
});
}

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

The next step will be to migrate this schema and make a table in the database.

php artisan migrate

Next go to create the controller and model file for this project copy below code and paste in your command. 👇👇

php artisan make:model Form

php artisan make:controller FormController

lets got to setup the model and controller file for our project.

Step 2: Define routes in the web.php file.

copy below code and paste in your route/web.php file 👇

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('form','FormController@create');
Route::post('form','FormController@store');

Go to your controller and paste below code in your controller 👇

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Form;

class FormController extends Controller
{
public function create()
{
return view('create');
}

public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image | mimes:jpeg,png,jpg,gif,svg|max:2048'
]);

if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images',$name);
$data[] =$name;
}
}

$form= new Form();
$form->filename=json_encode($data);

$form->save();

return back()->with('success', 'image has been successfully updated');
}


}

Now, let us make a create.blade.php file inside the Resources/views folder.

@extends('layouts.app')
@section('content')


<div class="container">
@if (count($errors) > 0)
<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

@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif

<h3 class="jumbotron">Laravel Multiple File Upload</h3>
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}

<div class="input-group control-group increment" >
<input type="file" name="filename[]" class="form-control">
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="control-group input-group" style="margin-top:10px">
<input type="file" name="filename[]" class="form-control">
<div class="input-group-btn">
<button class="btn btn-danger" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>

<button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>

</form>
</div>


<script type="text/javascript">


$(document).ready(function() {

$(".btn-success").click(function(){
var html = $(".clone").html();
$(".increment").after(html);
});

$("body").on("click",".btn-danger",function(){
$(this).parents(".control-group").remove();
});

});

</script>

@endsection

Now let’s go to your browser and paste below code 👇

http://127.0.0.1:8000/form

Now image has been successfully added in database as well. 👇

Thanks i hope its helpfull for you. 🙏🙏🙏Amit Kumar

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.

Tagged : / / / /

Laravel 8 Form Example Tutorial

In this tutorial i’m going to describe to create form and store data in database. Please follow some easy steps mentioned below.

How to Submit Form Data into Database in Laravel 8

  • Step 1 — Install Laravel 8 Application
  • Step 2 — Configuring Database using Env File
  • Step 3 — Create Model & Migration File For Add Blog Post Form
  • Step 4 — Create Routes
  • Step 5 — Creating Controller
  • Step 6 — Create Blade File For Add Blog Post Form
  • Step 7 — Start Development Server
  • Step 8 — Run Laravel 8 Form App On Browser

Step 1 — Install Laravel 8 Application

composer create-project laravel/laravel laravelform
  • Step 2 — Configuring Database using Env File

Now create model and migration table

php artisan make:model Post -m

Next go to post migration table and paste below code

<?php

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

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('description');
$table->timestamps();
});
}

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

Next go to Post model and paste below code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable=[
'title',
'description'
];
}

Now migrate the table run below command

php artisan migrate

Step 4 — Create Routes

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Route::get('add-blog-post-form', [PostController::class, 'index']);
Route::post('store-form', [PostController::class, 'store']);

Step 5 — Creating Controller

php artisan make:controller PostController -r

Go to your Post controller file and paste below code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;use App\Post;class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('add-blog-post-form');
}

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

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$post = new Post;

$post->title = $request->title;
$post->description = $request->description;

$post->save();

return redirect('add-blog-post-form')->with('status', 'Blog Post Form Data Has Been inserted');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Step 6 — Create Blade File For Form

add-blog-post-form.blade.php

Go to Resources/views/add-blog-post-form.blade.php and create this file

<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Form Example Tutorial laravel Amit</title>
<meta name="csrf-token" content="{{ csrf_token() }}">

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

</head>
<body>

<div class="container mt-4">

@if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif

<div class="card">
<div class="card-header text-center font-weight-bold">
Laravel 8 - Add Blog Post Form Example
</div>
<div class="card-body">
<form name="add-blog-post-form" id="add-blog-post-form" method="post" action="{{url('store-form')}}">
@csrf

<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" id="title" name="title" class="form-control" required="">
</div>

<div class="form-group">
<label for="exampleInputEmail1">Description</label>
<textarea name="description" class="form-control" required=""></textarea>
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>

Now refresh the Browser

php artisan serve

When you fire the above given url on browser, you will look like in the following image:

http://127.0.0.1:8000/add-blog-post-form

Thanks

Tagged : / / /

Target class [PostController] does not exist.

Whenever you got this types of error go to your Route/web.php file and checkout your Controller class is mention there or not.

Next Go to PostController file and copy your controller class

Now go to Routes/web.php and class this class

use App\Http\Controllers\PostController;
php artisan serve

Now its working properly

Thanks.

Tagged : / / / /

General error: 1364 Field ‘phone’ doesn’t have a default value

SQLSTATE[HY000]: General error: 1364 Field ‘phone’ doesn’t have a default value (SQL: insert into `products` (`name`, `email`, `address`, `image`, `updated_at`, `created_at`) values (amti, kuam@gmail.com, sdafasdf, 642917381.jpg, 2021–05–11 10:58:35, 2021–05–11 10:58:35))

When I’m submit form then got General error field doesn’t have default value. let’s go to solve this.

👉 1 step go to your database and put change none>to Null and save.

Lets go to phpmyadmin

👉 Click on Structure

Click on change and change all field to None to Null and save

Set Null and click on save button

👁👁 Now its error has been solved and your data has been stored successfully.

Thanks

Tagged : / / / / / /

CRUD Operation Detailed Explaination with Example.

Before performing CRUD operations we need to do some of the basic tasks. These includes:- 1. Installing Laravel 2. Creating Laravel Project 3. Making Model 4. Migrating After that we are good to go for the operation.

Explanation for the above operations is given in link below https://www.scmgalaxy.com/tutorials/crud-operation-prerequisites/

5. Create Operation

Create a folder Under resource =>view => Task =>File_name (say create.blade.php) and code the Html part in it.

We will now move to route(web.php) and create a route for the view . We will use Route::get(‘create’,’TaskController@index’)->name(‘create’);

We will now move to route(web.php) and create a route for the view to display. name(‘task‘) is given in the URL as http://127.0.0.1:8000/task. Control flow:- As the URL is hit the control will go the route(web.php), there we will write Route::get(‘create‘,’TaskController@create’)->name(‘create‘); Here, there will be a name match from the URL and if there is a match then there will be a function call named create and route will redirect to TaskController.php where it will find the function create and operate according to the instructions given in it. }

The TaskController.php will get to the create function definition Here this function will return a view create.blade.php, which has html codes, Output displayed on the browser.

After the create operation we need to store the data in the database for this we used <form action=”{{route(‘store’)}}” method=”POST”> which will call store function. for that, we need to call the store function when the user clicks submit after entering the data on the above page. The store function be like

In the store function we create object $var of Task make variables as $var->task which is assigned $request->task; (gets value from 1st field) & $var->detail which is assigned $request->detail; (gets value from 2nd field) and $var->save(); function will save it to the database.

Soon after the data is created and stored in the database, we can view it by performing Read operation.

6. Read operation

Create a file in resource =>view => Task (say, task.blade.php). and code the Html part in it.

We will now move to route(web.php) and create a route for the view to display. We will write the name(‘task‘) is given in the URL as http://127.0.0.1:8000/task. Control flow:- As the URL is hit the control will go the route(web.php), there it will write Route::get(‘task’,’TaskController@index’)->name(‘task’); Here, there will be a name match from the URL and if there is a match then there will be a function call named index and route will redirect to TaskController.php where it will find the function index and operate according to the instructions given in it. }

Here this function will return a view taskIndex.blade.php, which has HTML codes, which is to display on the browser with all the things compact.

7. Update operation

Create a file in resource =>view => Task => File_name (say, edit.blade.php). and code the Html part in it.

For update we need to give an edit button which calls the update function when clicked. for this we used <a href=”{{route(‘notes.edit’,[‘id’ => $note->id])}}”  type=”button” class=”btn btn-primary”>Edit</a> This will redirect to route (web.php) where we will write Route::get(‘edit’,’TaskController@edit’)->name(‘edit’); Here, there will be a name match from the URL and if there is match then there will be a function call named edit and route will redirect to TaskController.php where it will find the function edit and operate according to the instructions given in it. This will call edit function

This will find the corresponding id and return the edit page view with the details prefilled in it.

On click of submitting after changing the details which were prefilled, This will redirect to route (web.php) where we will write Route::post(‘update/{id}’,’TaskController@update’)->name(‘update’); Here, there will be a name match from the URL and if there is a match then there will be a function call named update and route will redirect to TaskController.php where it will find the function update and operate according to the instructions given in it. The update function will run.

In the Update function we create object $var1 of Task which finds and matches id and make variables as $var1->task which is assigned  $request->task; (gets value from 1st field) & $var1->detail which is assigned $request->detail; (gets value from 2nd field) and $var1->save(); function will save it to the database. and return redirect() will redirect the route to task.

7. Delete Operation

Implementing delete operation is easy and we need to Just provide a delete button in the html view which will help call the destroy function.

On click of Delete button This will redirect to route (web.php) where we will write Route::post(‘delete/{id}’,’TaskController@delete’)->name(‘delete’); Here, there will be a name match from the URL and if there is a match then there will be a function call named delete and route will redirect to TaskController.php where it will find the function delete and operate according to the instructions given in it. The update function will run.

In the Update function we create object $var2 of Task which finds and matches id and $var1->delete(); function will delete it from the database. and return redirect() will redirect the route to task.

Tagged : / / / / / / /