এই Lesson এ দেখবো কিভাবে আমাদের প্রজেক্ট এ নতুন Payment অ্যাড করতে হয়।
Routes:
Route::get('users/{id}/payments', 'UserPaymentsController@index')->name('user.payments');
Route::post('users/{id}/payments', 'UserPaymentsController@store')->name('user.payments.store');
Route::delete('users/{id}/payments/{payment_id}', 'UserPaymentsController@destroy')->name('user.payments.destroy');
UserPaymentsController.php
namespace App\Http\Controllers;
use App\Http\Requests\PayemntRequest;
use App\Payment;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class UserPaymentsController extends Controller
{
public function __construct()
{
$this->data['tab_menu'] = 'payments';
}
public function index( $id )
{
$this->data['user'] = User::findOrFail($id);
return view('users.payments.payments', $this->data);
}
/**
* Add new Payment
* @param PayemntRequest $request
* @param int $user_id
*/
public function store(PayemntRequest $request, $user_id)
{
$formData = $request->all();
$formData['user_id'] = $user_id;
if( Payment::create($formData) ) {
Session::flash('message', 'Payment Added Successfully');
}
return redirect()->route('user.payments', ['id' => $user_id]);
}
/**
* Delete a payment
* @param int $user_id
* @param int $payment_id
*/
public function destroy($user_id, $payment_id)
{
if( Payment::destroy($payment_id) ) {
Session::flash('message', 'Payment Deleted Successfully');
}
return redirect()->route('user.payments', ['id' => $user_id]);
}
}
Payment.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
protected $fillable = ['date', 'amount', 'note', 'user_id'];
}
PayemntRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PayemntRequest 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'
];
}
}
2020_09_03_141625_update_payment_and_receipt_note.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdatePaymentAndReceiptNote extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('payments', function (Blueprint $table) {
$table->string('note')->nullable()->change();
});
Schema::table('receipts', function (Blueprint $table) {
$table->string('note')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('payments', function (Blueprint $table) {
$table->string('note')->change();
});
Schema::table('receipts', function (Blueprint $table) {
$table->string('note')->change();
});
}
}
Installing doctrine/dbal
composer require doctrine/dbal
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>
<a class="btn btn-info" href="{{ url('users/create') }}"> <i class="fa fa-plus"></i> New Receipt </a>
</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>
@stop
payments/payments.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"> Payments 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>User</th>
<th>Date</th>
<th>Total</th>
<th>Note</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="2" class="text-right">Total : </th>
<th> {{ $user->payments()->sum('amount') }} </th>
<th></th>
<th></th>
</tr>
</tfoot>
<tbody>
@foreach ($user->payments as $payment)
<tr>
<td> {{ $user->name }} </td>
<td> {{ $payment->date }} </td>
<td> {{ $payment->amount }} </td>
<td> {{ $payment->note }} </td>
<td class="text-right">
<form method="POST" action=" {{ route('user.payments.destroy', ['id' => $user->id, 'payment_id' => $payment->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>
{{-- Modal For add new payment --}}
<!-- Modal -->
<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>
@stop