এই Lesson এ দেখবো কিভাবে আমাদের প্রজেক্ট এ নতুন Receipt অ্যাড করতে হয়।
Routes:
Route::get('users/{id}/receipts', 'UserReceiptsController@index')->name('user.receipts');
Route::post('users/{id}/receipts', 'UserReceiptsController@store')->name('user.receipts.store');
Route::delete('users/{id}/receipts/{receipt_id}', 'UserReceiptsController@destroy')->name('user.receipts.destroy');
UserReceiptsController
namespace App\Http\Controllers;
use App\Http\Requests\ReceiptRequest;
use App\Receipt;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class UserReceiptsController extends Controller
{
public function __construct()
{
$this->data['tab_menu'] = 'receipts';
}
public function index( $id )
{
$this->data['user'] = User::findOrFail($id);
return view('users.receipts.receipts', $this->data);
}
public function store(ReceiptRequest $request, $user_id)
{
$formData = $request->all();
$formData['user_id'] = $user_id;
$formData['admin_id'] = Auth::id();
if( Receipt::create($formData) ) {
Session::flash('message', 'Receipt Added Successfully');
}
return redirect()->route('user.receipts', ['id' => $user_id]);
}
public function destroy($user_id, $receipt_id)
{
if( Receipt::destroy($receipt_id) ) {
Session::flash('message', 'Receipt Deleted Successfully');
}
return redirect()->route('user.receipts', ['id' => $user_id]);
}
}
ReceiptRequest
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ReceiptRequest 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 [
'date' => 'required',
'amount' => 'required',
'note' => 'nullable'
];
}
}
app/Receipt.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Receipt extends Model
{
protected $fillable = ['date', 'amount', 'note', 'user_id', 'admin_id'];
public function admin()
{
return $this->belongsTo(Admin::class);
}
}
app/Admin.php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function payments()
{
return $this->hasMany(Payment::class);
}
public function receipts()
{
return $this->hasMany(Receipt::class);
}
}
users/user_layout.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-4">
<a class="btn btn-info" href="{{ route('users.index') }}"> <i class="fa fa-arrow-left" aria-hidden="true"></i> Back </a>
</div>
<div class="col-md-8 text-right">
<a class="btn btn-info" href="{{ url('users/create') }}"> <i class="fa fa-plus"></i> New Sale </a>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newPurchase">
<i class="fa fa-plus"></i> New Purchase
</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newPayment">
<i class="fa fa-plus"></i> New Payment
</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newReceipt">
<i class="fa fa-plus"></i> New Receipt
</button>
</div>
</div>
<div class="row clearfix mt-5">
<div class="col-md-2">
<div class="nav flex-column nav-pills">
<a class="nav-link @if($tab_menu == 'user_info') active @endif " href=" {{ route('users.show', $user->id) }} ">User Info</a>
<a class="nav-link @if($tab_menu == 'sales') active @endif " href="{{ route('user.sales', $user->id) }}">Sales</a>
<a class="nav-link @if($tab_menu == 'purchases') active @endif " href="{{ route('user.purchases', $user->id) }}">Purchases</a>
<a class="nav-link @if($tab_menu == 'payments') active @endif " href="{{ route('user.payments', $user->id) }}">Payments</a>
<a class="nav-link @if($tab_menu == 'receipts') active @endif " href="{{ route('user.receipts', $user->id) }}">Receipts</a>
</div>
</div>
<div class="col-md-10">
@yield('user_content')
</div>
</div>
{{-- Modal For add new payment --}}
<div class="modal fade" id="newPayment" tabindex="-1" role="dialog" aria-labelledby="newPaymentModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.payments.store', $user->id], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newPaymentModalLabel"> New Payments </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="amount" class="col-sm-3 col-form-label">Amount <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('amount', NULL, [ 'class'=>'form-control', 'id' => 'amount', 'placeholder' => 'Amount', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
{{-- Modal For Receipt --}}
<div class="modal fade" id="newReceipt" tabindex="-1" role="dialog" aria-labelledby="newReceiptModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.receipts.store', $user->id], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newReceiptModalLabel"> New Receipts </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="amount" class="col-sm-3 col-form-label">Amount <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('amount', NULL, [ 'class'=>'form-control', 'id' => 'amount', 'placeholder' => 'Amount', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
@stop
users/receipts/receipts.blade.php
@extends('users.user_layout')
@section('user_content')
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> Receipts of <strong>{{ $user->name }} </strong></h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Admin</th>
<th>Date</th>
<th class="text-right">Total</th>
<th>Note</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="2">Total</th>
<th class="text-right"> {{ $user->receipts()->sum('amount') }} </th>
<th></th>
<th class="text-right"></th>
</tr>
</tfoot>
<tbody>
@foreach ($user->receipts as $receipt)
<tr>
<td> {{ optional($receipt->admin)->name }} </td>
<td> {{ $receipt->date }} </td>
<td class="text-right"> {{ $receipt->amount }} </td>
<td> {{ $receipt->note }} </td>
<td class="text-right">
<form method="POST" action=" {{ route('user.receipts.destroy', ['id' => $user->id, 'receipt_id' => $receipt->id]) }} ">
@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
users/users.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-6">
<h2> Users </h2>
</div>
<div class="col-md-6 text-right">
<a class="btn btn-info" href="{{ url('users/create') }}"> <i class="fa fa-plus"></i> New user </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">Users</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>Group</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Group</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th class="text-right">Actions</th>
</tr>
</tfoot>
<tbody>
@foreach ($users as $user)
<tr>
<td> {{ $user->id }} </td>
<td> {{ optional($user->group)->title }} </td>
<td> {{ $user->name }} </td>
<td> {{ $user->email }} </td>
<td> {{ $user->phone }} </td>
<td> {{ $user->address }} </td>
<td class="text-right">
<form method="POST" action=" {{ route('users.destroy', ['user' => $user->id]) }} ">
<a class="btn btn-primary btn-sm" href="{{ route('users.show', ['user' => $user->id]) }}">
<i class="fa fa-eye"></i>
</a>
<a class="btn btn-primary btn-sm" href="{{ route('users.edit', ['user' => $user->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