CRUD OPERATIONS IN LARAVEL WITH ELOQUENT
requirements before work:
1.xampp:
2.laravel project
3.vs code
1.create database in xampp
open xampp
start mysql and apache
click on:new
type database name[EXAMPLE :flip]
click on: create
2. connect the database with laravel
Open Laravel project in vs code
open .env file
find DB_DATABASE=flip
3. create migration
open project in vs code
open cmd in vs code[ control+`]
type command to create migration:
syntax:
php artisan make:migration tablename
example:
php artisan make:migration products
note: migration name must be plural
3. create a model for a table
note:
the model must be same as table name.
model name must be written as singular.
open project in vs code
open cmd in vs code
type command to create model:
syntax:
php artisan make:model tablename --controller --resource
example:
php artisan make:model product --controller --resource
steps to store data in database using mysql
4. create view for registeration form:
1. go to resources/views/
2. create a view file named like add_product.blade.php
3.write the code of form
<html>
<head>
</head>
<body>
<form action="/addproduct" method="post">
@csrf
<div>
<label>name</label>
<input type="text" name="pname">
</div>
<div>
<label>price</label>
<input type="number" name="pprice">
</div>
<div>
<label>company</label>
<input type="text" name="pcomp">
</div>
<div><input type="submit"></div>
</form>
</body>
</html>
5. create the routes:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/newproduct',[ProductController::class,'create'])->name('add');
Route::post('/addproduct',[ProductController::class,'store'])->name('save');
6. write program in controller file to save data of form:
1.go to app/http/controllers/product_controller.php
2. write what the create function will do:
public function create()
{
return view("add_product");
}
3. write what store function will do:
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
$product=new Product();
$product->name=$request->pname;
$product->price=$request->pprice;
$product->company=$request->pcomp;
$product->save();
return redirect()->route('view');
}
7. open webpage to see the output:
1. open command prompt in vs code
2. type command:
php artisan serve
3. opne browser and write the url:
http://127.0.0.1:8000/newproduct
4.after registeration ,check in database's table
SHOW ALL DATA
8.write program to fetch data from table
1.go to app/http/controllers/productcontroller.php
2.write the program to fetch data from database's table using index function
public function index()
{
//
$products=Product::all();
return view("Products",compact('products'));
}
9. create view to display the data of table
1. go to resources/views/
2.create a view file : products.blade.php
<html>
<head>
<Style>
tr:nth-child(even)
{
background:lightgrey;
padding:10px;
margin:10px;
}
td
{
padding:10px;
}
</style>
</head>
<body>
<a href="{{route('add')}}" id="hre">add new</a>
<table class="tbl">
<tr class="trow">
<th class="thead">name</th>
<th class="thead">price</th>
<th class="thead">company</th>
</tr>
@foreach($products as $v)
<tr>
<td class="tdata">{{$v->name}}</td>
<td class="tdata">{{$v->price}}</td>
<td class="tdata">{{$v->company}}</td>
<td class="tdata"><a href="{{route('edit',$v->id)}}" id="href">edit</a></td>
<td class="tdata">
<form action="{{route('delete',$v->id)}}" method="post">
@csrf
@method('delete')
<input type="submit" value="delete">
</form>
</td>
<td class="tdata"><a href="{{route('show',$v->id)}}" id="href">view</a></td>
</tr>
@endforeach
</table>
</body>
</html>
10. create the route to show the products data page
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/newproduct',[ProductController::class,'create'])->name('add');
Route::post('/addproduct',[ProductController::class,'store'])->name('save');
Route::get('/view',[ProductController::class,'index'])->name('view');
11. see data by accessing the created url
1. open the link in browser:
http://127.0.0.1:8000/view
DELETE THE DATA
12. write the program in the controller file to delete data
1. go to app/http/controllers/productcontroller.php
2. scroll down to the destroy function
3. type deletion code in destroy function
public function destroy(Request $req,Product $product)
{
$product->destroy($req->id);
return redirect()->route('view');
}
13. create the route for delete
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/newproduct',[ProductController::class,'create'])->name('add');
Route::post('/addproduct',[ProductController::class,'store'])->name('save');
Route::get('/view',[ProductController::class,'index'])->name('view');
Route::delete('/delete/{id}',[ProductController::class,'destroy'])->name('delete');
NOW, OPEN THE VIEW PAGE THEN CLICK ON DELETE BUTTON TO THE DATA.
UPDATE THE DATA
13. create the route for edit form page
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/newproduct',[ProductController::class,'create'])->name('add');
Route::post('/addproduct',[ProductController::class,'store'])->name('save');
Route::get('/view',[ProductController::class,'index'])->name('view');
Route::delete('/delete/{id}',[ProductController::class,'destroy'])->name('delete');
Route::get('/edit/{id}',[ProductController::class,'edit'])->name('edit');
14. write the program in the controller file for the edit form page:
1. go to app/http/controller/productcontroller
2. scroll down to the edit function
/**
* Show the form for editing the specified resource.
*/
public function edit(product $product,Request $req)
{
$p= Product::find($req->id);
return view('updateproduct',compact('p'));
}
15. create the view for edit form page:
<html>
<head>
<title>update the product</title>
</head>
<body>
<form action="{{route('update',$p->id)}}" method="post">
@csrf
@method('PUT')
<div>
<label>name</label>
<input type="text" value="{{$p->name}}"name="pname">
</div>
<div>
<label>price</label>
<input type="number" value="{{$p->price}}" name="pprice">
</div>
<div>
<label>company</label>
<input type="text" value="{{$p->company}}" name="pcomp">
</div>
<div><input type="submit" value="update"></div>
</form>
</body>
</html>
16. write the program in the controller file to update the data
1. go to app/http/controller/productcontroller
2. scroll down to the update function
/**
* Update the specified resource in storage.
*/
public function update(Request $request, product $product)
{
//
$pro=Product::find($request->id);
$pro->name=$request->pname;
$pro->price=$request->pprice;
$pro->company=$request->pcomp;
$pro->save();
return redirect()->route('view');
}
17. create the route for updation
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/newproduct',[ProductController::class,'create'])->name('add');
Route::post('/addproduct',[ProductController::class,'store'])->name('save');
Route::get('/view',[ProductController::class,'index'])->name('view');
Route::delete('/delete/{id}',[ProductController::class,'destroy'])->name('delete');
Route::get('/edit/{id}',[ProductController::class,'edit'])->name('edit');
Route::put('/update/{id}',[ProductController::class,'update'])->name('update');
NOW, OPEN THE VIEW PAGE THEN CLICK ON THE update BUTTON and update the data.
0 Comments