এই লেসনে আমাদের POS প্রজেক্ট এর Product এর List View, Product Insert, Update, Delete করা দেখানো হয়েছে।
Project Link:
https://github.com/almamuncsit/mini_pos
Rotes:
Route::resource('products', 'ProductsController' );
ProductsController:
namespace App\Http\Controllers;
use App\Category;
use App\Http\Requests\ProductRequest;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$this->data['products'] = Product::all();
return view('products.products', $this->data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$this->data['categories'] = Category::arrayForSelect();
$this->data['mode'] = 'create';
$this->data['headline'] = 'Add New Product';
return view('products.form', $this->data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ProductRequest $request)
{
$formData = $request->all();
if( Product::create($formData) ) {
Session::flash('message', 'Product Created Successfully');
}
return redirect()->to('products');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$this->data['product'] = Product::find($id);
return view('products.show', $this->data);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$this->data['product'] = Product::findOrFail($id);
$this->data['categories'] = Category::arrayForSelect();
$this->data['mode'] = 'edit';
$this->data['headline'] = 'Update Product Information';
return view('products.form', $this->data);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ProductRequest $request, $id)
{
$data = $request->all();
$product = Product::find($id);
$product->category_id = $data['category_id'];
$product->title = $data['title'];
$product->description = $data['description'];
$product->cost_price = $data['cost_price'];
$product->price = $data['price'];
if( $product->save() ) {
Session::flash('message', 'Product Updated Successfully');
}
return redirect()->to('products');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if( Product::destroy($id) ) {
Session::flash('message', 'Product Deleted Successfully');
}
return redirect()->to('products');
}
}
ProductRequest:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'string|required',
'description' => 'string|required',
'cost_price' => 'nullable|numeric',
'price' => 'nullable|numeric',
'category_id' => 'required',
];
}
}
Category Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['title'];
public function products()
{
return $this->hasMany(Product::class);
}
public static function arrayForSelect()
{
$arr = [];
$categories = Category::all();
foreach ($categories as $category) {
$arr[$category->id] = $category->title;
}
return $arr;
}
}
Product Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['title', 'description', 'category_id', 'cost_price', 'price'];
public function category()
{
return $this->belongsTo(Category::class);
}
}
products/products.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-6">
<h2> Products </h2>
</div>
<div class="col-md-6 text-right">
<a class="btn btn-info" href="{{ route('products.create') }}"> <i class="fa fa-plus"></i> New Product </a>
</div>
</div>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Products</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Category</th>
<th>Title</th>
<th>Cost Price</th>
<th>Sale Price</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Category</th>
<th>Title</th>
<th>Cost Price</th>
<th>Sale Price</th>
<th class="text-right">Actions</th>
</tr>
</tfoot>
<tbody>
@foreach ($products as $product)
<tr>
<td> {{ $product->id }} </td>
<td> {{ $product->category->title }} </td>
<td> {{ $product->title }} </td>
<td> {{ $product->cost_price }} </td>
<td> {{ $product->price }} </td>
<td class="text-right">
<form method="POST" action=" {{ route('products.destroy', ['product' => $product->id]) }} ">
<a class="btn btn-primary btn-sm" href="{{ route('products.show', ['product' => $product->id]) }}">
<i class="fa fa-eye"></i>
</a>
<a class="btn btn-primary btn-sm" href="{{ route('products.edit', ['product' => $product->id]) }}">
<i class="fa fa-edit"></i>
</a>
@csrf
@method('DELETE')
<button onclick="return confirm('Are you sure?')" type="submit" class="btn btn-danger btn-sm">
<i class="fa fa-trash"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@stop
products/show.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-4">
<a class="btn btn-primary" href="{{ route('products.index') }}"> <i class="fa fa-arrow-left" aria-hidden="true"></i> Back </a>
</div>
</div>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> {{ $product->title }} </h6>
</div>
<div class="card-body">
<div class="row clearfix justify-content-md-center">
<div class="col-md-12">
<table class="table table-borderless table-striped">
<tr>
<th class="text-right">Category :</th>
<td> {{ $product->category->title }} </td>
</tr>
<tr>
<th class="text-right">Title : </th>
<td> {{ $product->title }} </td>
</tr>
<tr>
<th class="text-right">Description: </th>
<td> {{ $product->description }} </td>
</tr>
<tr>
<th class="text-right">Cost Price : </th>
<td> {{ $product->cost_price }} </td>
</tr>
<tr>
<th class="text-right">Sale Price : </th>
<td> {{ $product->price }} </td>
</tr>
</table>
</div>
</div>
</div>
</div>
@stop
products/form.blade.php
@extends('layout.main')
@section('main_content')
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<h2> {{ $headline }} </h2>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">{{ $headline }}</h6>
</div>
<div class="card-body">
<div class="row justify-content-md-center">
<div class="col-md-12">
@if($mode == 'edit')
{!! Form::model($product, [ 'route' => ['products.update', $product->id], 'method' => 'put' ]) !!}
@else
{!! Form::open([ 'route' => 'products.store', 'method' => 'post' ]) !!}
@endif
<div class="form-group row">
<label for="title" class="col-sm-2 text-right col-form-label">Title <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('title', NULL, [ 'class'=>'form-control', 'id' => 'title', 'placeholder' => 'Title' ]) }}
</div>
</div>
<div class="form-group row">
<label for="description" class="col-sm-2 text-right col-form-label">Description <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::textarea('description', NULL, [ 'class'=>'form-control', 'id' => 'description', 'placeholder' => 'Description' ]) }}
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 text-right col-form-label">Category <span class="text-danger">*</span> </label>
<div class="col-sm-5">
{{ Form::select('category_id', $categories, NULL, [ 'class'=>'form-control', 'id' => 'group', 'placeholder' => 'Select Category' ]) }}
</div>
</div>
<div class="form-group row">
<label for="cost_price" class="col-sm-2 text-right col-form-label">Cost Price</label>
<div class="col-sm-5">
{{ Form::text('cost_price', NULL, [ 'class'=>'form-control', 'id' => 'cost_price', 'placeholder' => 'Cost Price' ]) }}
</div>
</div>
<div class="form-group row">
<label for="price" class="col-sm-2 text-right col-form-label">Sale Price</label>
<div class="col-sm-5">
{{ Form::text('price', NULL, [ 'class'=>'form-control', 'id' => 'price', 'placeholder' => 'Sale Price' ]) }}
</div>
</div>
<div class="form-group row mt-4">
<label for="price" class="col-sm-2 text-right col-form-label"></label>
<div class="col-sm-5">
<button type="submit" class="btn btn-primary btn-lg"> <i class="fa fa-save"></i> Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@stop