Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

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 : / / / /

CRUD Operations in Laravel using ajax

Step 1 – Install Laravel 5.8

First we want to Download Laravel 5.8 version for developing Crud application using Ajax. For this you have to open your terminal or command prompt and write below command. It will download Laravel 5.8 in your define directory.

composer create-project laravel/laravel=5.8 ajax-crud –prefer-dist

Step 2 – Laravel 5.8 Database Connection

Once Laravel 5.8 has been downloaded then after we have to make Database connection. For this first you have to open config/database.php and define Mysql Database configuration.

After this you have to open .env file and in that file also you have to define Mysql Database configuration, which you can find below.

Step 3 – Create Table

For Create Crud Application, first we have to create Mysql table. For create Mysql table from Laravel 5.8 application we have to write following artisan command in your command prompt.

php artisan make:migration create_ajax_cruds_table –create=ajax_cruds

Above command will command create migration file in database/migrations folder. In that file we have to define table column which you want to define in Mysql table. Below you can find source code of migration file.

Lastly for create Mysql table in your Database, you have to write following command in your terminal or command prompt. It will create ajax_cruds table in your define Database.

php artisan migrate

Step 4 – Create Model in Laravel 5.8

Now we want to make Model in Laravel 5.8, For this you have to write following command in your terminal or command prompt. It will create model file here app/AjaxCrud.php.

php artisan make:model AjaxCrud -m

Under this app/AjaxCrud.php file, you have to define table column for database operation which source code you can find below.

Step 5 – Create Blades Files

For display output data in browser, in Laravel 5.8 we have to create blade file in resources/views folder. Under this folder we have create ajax_index.blade.php file. In this file you can find HTML code and jQuery code for display data in jQuery Datatable.

Step 6 – Set Resource Route

Step 7 – Create Controller in Laravel 5.8

Lastly, We need to set resource route for ajax crud application. For this we have to open routes/web.php file.

Controller mostly used for handle HTTP request. Create new controller in Laravel 5.8 framework, we have to write following command.

php artisan make:controller AjaxCrudController –resource

This command will create AjaxCrudController.php file in app/Http/Controllers folder. In this controller file you can find all in build required method for do Crud operation. Under this controller for use AjaxCrud model, first we have to define use App\AjaxCrud; this statement at the header of the AjaxCrudController.php file.

index() – This is the root method of this controller. This method will received Ajax request for load data in jQuery DataTables plugin. If this method received ajax request then it will load data in jQuery DataTables plugin. In this ajax block you can find yajra datatables package code.

store() – For Insert Data into Mysql table using ajax in Laravel 5.8, here we have use store() method of AjaxCrudController.php. This method will received Ajax request for insert or add data. Under this method first it has validate for data. If there is any validation fails then it will send response to ajax request in JSON format. But there is no any validation fails then it will continue execution of code and first it will upload profile image of user and then after it will insert data into mysql table. And lastly it will send json response to ajax request.

edit() – This method is used for fetch single row of data from mysql table, and send data to ajax request in json formate which will be display under Bootstrap modal form.

update() – This method has received ajax request for update existing mysql table data. In this method first it check user has select profile image or not. If User is select image then in this method it will validate form data with selected file is image or not. But Suppose user has not select image then it will only validate textbox data only. If there is any validation error occur then it will send data to ajax request in json formate. After successfully validate form data then it will update data.

destroy() – For delete or remove mysql data, ajax request will be send to destroy() method. It will delete or remove mysql data in Laravel 5.8 by using ajax.

For run Laravel 5.8 application, you can write following command.

php artisan serve

For see Laravel 5.8 application in your browser, you have to write following url.

http://localhost:8000/ajax-crud
Tagged : / / / /

Crud Operation in Laravel

In this tutorial I’m going to learn how to create Crud operation in Laravel. Please follow some easy steps define below.

Step 1 — Installing Laravel 8

Open terminal in xampp/htdocs and paste below code

composer create-project laravel/laravel example-app

Step 2 — Setting up a MySQL Database

👇 Step 2 — Database Configuration

Setup database with your installed laravel 8 project . lets go to .env folder and put database name and connect to database.

👇 Next go to .env folder and put database name

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud-operation
DB_USERNAME=root
DB_PASSWORD=

Next run this command

php artisan migrate

Step 3 — Creating a Database Migration

Run this command

php artisan make:migration create_products_table --create=products

Copy below code and simply paste in your 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');
}
}

Step 4 — Adding a Resource Route

<?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::resource('products','ProductController');

Step 5 — Adding a Laravel 8 Controller and Model

run this command

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

Go to your product controller 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',
'detail' => 'required',
]);

Product::create($request->all());

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',
]);

$product->update($request->all());

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');
}
}

Lets Go to Model and paste below code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

Step 6 — Adding your Larevl 8 Blade Views

Go to Resource folder and create view file

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

resources/views/products/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 6 CRUD Application - 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>

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 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>No</th>
<th>Name</th>
<th>Details</th>
<th width="280px">Action</th>
</tr>
@foreach ($products as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name }}</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() !!}

@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

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

</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->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">
@csrf
@method('PUT')

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<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-xs-12 col-sm-12 col-md-12">
<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-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

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

<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>Details:</strong>
{{ $product->detail }}
</div>
</div>
</div>
@endsection

Lets run php artisan serve

http://localhost:8000/products

I hope its helpful for you.

Tagged : / / /

How to Add Data in DataTable in Laravel Crud

In this tutorial I’m going to describe how to add data in DataTable in laravel 8 we will create single page companies ajax crud application (SPA) using dataTables js, modal and jQuery in laravel 8.

Laravel 8 Ajax CRUD Tutorial using Datatable JS
Step 1 – Download Laravel 8 App
Step 2 – Database Configuration
Step 3 – Installing Yajra Datatables
Step 4 – Make Model & Migration
Step 5 – Make Routes
Step 6 – Create AJAX CRUD Datatables Controller
Step 7 – Create Blade Views File
companies.blade.php
company-action.blade.php
Step 8 – Run Development Server

Step 1 — Download Laravel 8 App

1st step Install laravel project and new setup, lets open the terminal C:\xampp\htdocs and paste below code

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

Step 2 — Database Configuration

Setup database with your installed laravel 8 project . 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=laraveldatatable
DB_USERNAME=root
DB_PASSWORD=

Next run this command

php artisan migrate

Step 3 — Installing Yajra Datatables

composer require yajra/laravel-datatables-oracle

Then configure datatables package. So go to config directory and open app.php file. And add the following service providers into app.php file:

Go to -> config/app.php and put in providers section

'providers' => [

Yajra\Datatables\DatatablesServiceProvider::class,
],

'aliases' => [

'Datatables' => Yajra\Datatables\Facades\Datatables::class,
]

Then publish laravel datatables vendor package by using below command:

php artisan vendor:publish

Step 4 — Make Model & Migration

php artisan make:model Company -m

Simply copy this code and paste in companies model

<?php

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

class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('email');
$table->string('address');
});
}

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

Lets migrate table to database run this command

php artisan migrate

After that, lets go to app/models directory and open company.php model file. Then add the following code into it:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{

protected $fillable =['name', 'email', 'address'];

}

Step 5 — Make 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');
});

use App\Http\Controllers\DataTableAjaxCRUDController;

Route::get('ajax-crud-datatable', [DataTableAjaxCRUDController::class, 'index']);
Route::post('store-company', [DataTableAjaxCRUDController::class, 'store']);
Route::post('edit-company', [DataTableAjaxCRUDController::class, 'edit']);
Route::post('delete-company', [DataTableAjaxCRUDController::class, 'destroy']);

Step 6 — Create AJAX CRUD Datatables Controller

php artisan make:controller DataTableAjaxCRUDController -r

Lets go to controller and copy below code and paste there

<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Company;
class DataTableAjaxCRUDController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if(request()->ajax()) {
return datatables()->of(Company::select('*'))
->addColumn('action', 'company-action')
->rawColumns(['action'])
->addIndexColumn()
->make(true);
}
return view('companies');
}

/**
* 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)
{
$companyId = $request->id;

$company = Company::updateOrCreate(
[
'id' => $companyId
],
[
'name' => $request->name,
'email' => $request->email,
'address' => $request->address
]);

return Response()->json($company);
}

/**
* 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)
{
$where = array('id' => $request->id);
$company = Company::where($where)->first();

return Response()->json($company);
}

/**
* 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)
{
$company = Company::where('id','$request->id')->delete();
return Response()->json($company);
}
}

Step 6 — Create Blade Views File

Create two blade views file, which is following:

  • companies.blade.php

Next go to companies.blade.php file and paste below code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 8 AJAX CRUD using DataTable js Tutorial From Scratch - Tutsmake.com</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 Ajax CRUD DataTables Tutorial</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" onClick="add()" href="javascript:void(0)"> Create Company</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<div class="card-body">
<table class="table table-bordered" id="ajax-crud-datatable">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Created at</th>
<th>Action</th>
</tr>
</thead>
</table>
</div>
</div>
<!-- boostrap company model -->
<div class="modal fade" id="company-modal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="CompanyModal"></h4>
</div>
<div class="modal-body">
<form action="javascript:void(0)" id="CompanyForm" name="CompanyForm" class="form-horizontal" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" id="id">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Company Name</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Company Name" maxlength="50" required="">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Company Email</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Company Email" maxlength="50" required="">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Company Address</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="address" name="address" placeholder="Enter Company Address" required="">
</div>
</div>
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" id="btn-save">Save changes
</button>
</div>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<!-- end bootstrap model -->
</body>
<script type="text/javascript">
$(document).ready( function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#ajax-crud-datatable').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('ajax-crud-datatable') }}",
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'email', name: 'email' },
{ data: 'address', name: 'address' },
{ data: 'created_at', name: 'created_at' },
{data: 'action', name: 'action', orderable: false},
],
order: [[0, 'desc']]
});
});
function add(){
$('#CompanyForm').trigger("reset");
$('#CompanyModal').html("Add Company");
$('#company-modal').modal('show');
$('#id').val('');
}
function editFunc(id){
$.ajax({
type:"POST",
url: "{{ url('edit-company') }}",
data: { id: id },
dataType: 'json',
success: function(res){
$('#CompanyModal').html("Edit Company");
$('#company-modal').modal('show');
$('#id').val(res.id);
$('#name').val(res.name);
$('#address').val(res.address);
$('#email').val(res.email);
}
});
}
function deleteFunc(id){
if (confirm("Delete Record?") == true) {
var id = id;
// ajax
$.ajax({
type:"POST",
url: "{{ url('delete-company') }}",
data: { id: id },
dataType: 'json',
success: function(res){
var oTable = $('#ajax-crud-datatable').dataTable();
oTable.fnDraw(false);
}
});
}
}
$('#CompanyForm').submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type:'POST',
url: "{{ url('store-company')}}",
data: formData,
cache:false,
contentType: false,
processData: false,
success: (data) => {
$("#company-modal").modal('hide');
var oTable = $('#ajax-crud-datatable').dataTable();
oTable.fnDraw(false);
$("#btn-save").html('Submit');
$("#btn-save"). attr("disabled", false);
},
error: function(data){
console.log(data);
}
});
});
</script>
</html>

Lets go to your browser and run this url

http://127.0.0.1:8000/ajax-crud-datatable

Now its open like this and successfully working and store in database as well.

Thanks 🙏🙏

Tagged : / / / /

Laravel 5.8 CRUD operation with example

In this tutorial we will learn how we can create a project in CRUD (Create, Read, Update and Delete) operations through Laravel. We will get to know how to create, update read and delete in Laravel from Scratch.

Initially have to download Laravel 5.8 version. For this you have to go to command prompt, in which first we have go to our folder path in which we want to download Laravel 5.8. After this we have to run “composer” command, because all Laravel depository is handled by composer. After run “composer” command we have to run following command:

composer create-project laravel/laravel=5.8 crud --prefer-dist

After downloading Laravel 5.8, we have to make Mysql database connection from Laravel 5.8.

We have to find .env file in your Laravel 5.8 folder. Open that file and under we have to define our mysql database before that we have to create a database in our mysql named as ‘crud’ and then we have to configure our .env as given below.

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

After this we have to create migration file in our Laravel folder. For this we have to write following command in your command prompt.

php artisan make:migration create_crud_table --create=crud

This command will create migration file in database/migrations folder. In this file we have to define table column which we want to create in table. Below we can find migration file in which we have define table column.

Now we want to migrate this table definition from this Laravel application to mysql database. For this we have write following command in command prompt. This command will make crud table in mysql database for perform CRUD operation from Laravel 5.8 application.

php artisan migrate

Now we have to create model for our database which is used for operations in controller class. To create a model we will run the command given below.

php artisan make:model Crud -m

This command will make Crud.php model file in app folder. In this file we have to define table column name which we can see below source code of Crud.php file.

Now we have to create Laravel 5.8 crud controller. For this we have to go to command prompt and under this we have write following command.

php artisan make:controller CrudsController --resource

This command will make CrudsController.php file in app/Http/Controllers folder. Once we open this file, we can find all predefine method for do CRUD operation in this controller file. We have to just add code for doing particular operation. Below we can find CRUD controller file code.

Now we have to set route of all CrudsController class method. For this we have to open to routes/web.php file. In this file we have to write following code for set route of all method.

Route::resource('crud','CrudsController');

Now in this step we have to set data in view file which has been store under resources/views folder, because this view file has received data from controller method, so here we have to set data in view file. Below we can find all view file which has been used in Crud application, and we can also find how data has been set and how to make form in Laravel 5.8 view file.

Now we can run the Laravel 5.8 Crud application, for this we have to go to command prompt, and write following command.

php artisan serve

For reference you can see the video given below:

Tagged :

CRUD Operation Prerequisites.

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 performing the operation.

Laravel the most popular PHP-based framework for creating database-driven apps is based on the MVC (Model-View-Controller) architecture and can be used for easily creating apps for making CRUD (Create, Retrieve, Update, Delete) operations in the database.

1. Installing Laravel The first and foremost thing we need to have Laravel and composer installed in our system. the command for installation is
composer global require “laravel/installer=~1.1”
if this does not works then we could just write
composer global require “laravel/installer”
and it would install the file.
2. Creating Laravel Project Then we need to create a project and for this, we need to type
composer create-project –prefer-dist laravel/laravel project_name “5.8.*”
If this does not works then we could just write
composer create-project –prefer-dist laravel/laravel project_name
it would create a Laravel project named blog (with the latest version).

3. Making Model and Migrating After the project is created we need to create the Model. For this we use command.

$ php artisan make:model Model_name -mcr or $ php artisan make:model Model_name -a

(Note:- Model name should begin with a capital letter) Here mcr is (migration controller resources) we can use m/c/r separately but for better convenience, we use a which can equivalently be used in place of “mcr”.

After the model is created we need to create the database and attach it. We can instruct for the database creation through codes or we can go to PHPMyAdmin, and there we can create the database and provide the name of the database in the .env file

After database creation, we need to migrate. Migrations allow to add or drop fields in the database without deleting the records already present. for this we use $ php artisan migrate We could also refresh migration using $ php artisan migrate:refresh

All the prerequisites to perform CRUD operations are completed and are ready to go. Now we can move to views to create the Html page and perform desired CRUD operations.

For CRUD Operation Detailed Explanation with Example. https://www.scmgalaxy.com/tutorials/crud-operation-detailed-explaination-with-example/

Tagged : / / / / / /