Problem with Laravel CRUD (Edit Function) - laravel

Good day everyone. I'm quite new to new to Laravel and was doing some basic CRUD coding, I was able to code the add and view function, but I'm having a hard time with the edit and delete function.
I have 4 files, the master blade file, the web.php (routing), the blade file and the Form Controller.
This is the promo.blade.php file:
<table class="table table-striped" id="table1" >
<thead>
<tr>
<th class="text-center">ACTION</th>
<th class="text-center">Offer ID</th>
<th class="text-center">Promo Name</th>
<th class="text-center">Promo Price</th>
<th class="text-center">Status</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $item)
<tr>
<td class="text-center">
<a href="#" data-bs-toggle="modal" data-bs-target="#show" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-success"><i class="bi bi-eye-fill"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#edit" data-mainid="{{$item->id}}" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-primary"><i class="bi bi-pencil-square"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#delete" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-danger"><i class="bi bi-trash"></i></span>
</a>
</td>
<td class="date text-center">{{ $item->offerId }}</td>
<td class="date text-center">{{ $item->promoName }}</td>
<td class="number text-center">{{ $item->promoPrice}}</td>
<td class="number text-center">{{ $item->isActive }}</td>
</tr>
#endforeach
</tbody>
</table>
<!--Start Modal Edit -->
<div class="modal fade text-left" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel160" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title white" id="add">
Edit Promo
</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<i data-feather="x"></i>
</button>
</div>
<div class="modal-body">
<form action="{{ route('promo.edit') }}" method="POST">
#csrf
<div class="form-group">
<label for="">ID</label>
<input type="text" class="form-control" name="id" id="id" value="">
<span style="color:red">#error('id'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Offer ID</label>
<input type="text" class="form-control" name="offerId" id="offerId" value="">
<span style="color:red">#error('offerId'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Name</label>
<input type="text" class="form-control" name="promoName" id="promoName" value="">
<span style="color:red">#error('promoName'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Price</label>
<input type="number" class="form-control" name="promoPrice" id="promoPrice" value="">
<span style="color:red">#error('promoPrice'){{$message}} #enderror</span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light-secondary" data-bs-dismiss="modal">
<i class="bx bx-x d-block d-sm-none"></i>
<span class="d-none d-sm-block">CANCEL</span>
</button>
<button type="submit" class="btn btn-primary ml-1">
<i class="bx bx-check d-block d-sm-none"></i>
<span class="d-none d-sm-block">SAVE</span>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal Edit-->
Then this is the web.php file, for the routing:
Route::get('promo.promo', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.promo');
Route::post('promo.add', [App\Http\Controllers\FormControllerPromo::class, 'addPromo'])->name('promo.add');
Route::post('promo.delete/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDelete'])->middleware('auth');
Route::get('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.edit');
Route::get('promo.edit/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDetail'])->middleware('auth');
Route::post('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'edit'])->name('promo.edit');
This is the master.blade.php file:
<!--Start Modal edit for Promo-->
<script type="text/javascript">
$('#edit').on('show.bs.modal', function (event){
var button = $(event.relatedTarget)
var mainid = button.data('mainid')
var id = button.data('myofferid')
var title = button.data('mytitle')
var price = button.data('myprice')
var modal = $(this)
modal.find('.modal-body #id').val(mainid);
modal.find('.modal-body #offerId').val(id);
modal.find('.modal-body #promoName').val(title);
modal.find('.modal-body #promoPrice').val(price);
})
</script>
<!--End Modal edit for Promo-->
I think this is the part where the code wont execute properly.
This is the FormControllerPromo.php file:
// view form
public function index()
{
return view('promo.promo');
}
// view record
public function viewRecord()
{
$data = DB::table('promo')->get();
return view('promo.promo',compact('data'));
}
// view detail
public function viewDetail($id)
{
$data = DB::table('promo')->where('id',$id)->get();
return view('promo.promo',compact('data'));
}
// edit promo
public function edit(Request $request){
$id = $request->input('id');
$offerId = $request->input('offerId');
$promoName = $request->input('promoName');
$promoPrice = $request->input('promoPrice');
DB::table('promo')
->where('id', $id) // find your user by their email
->limit(1) // optional - to ensure only one record is updated.
->update(array('offerId' => $offerId, 'promoName' => $promoName, 'promoPrice' => $promoPrice)); // update the record in the DB.
$data = DB::table('promo');
return view('promo.promo',compact('data'));
}
I've been trying to code this for almost a week now with no success, any help is highly appreciated. :)

the update seems right, should work. but when you pass the $data variable to your view, you should call ->get(), because otherwise you return a query builder instance, that later raises the Undefined property error when trying to access {{$item->offerId}}.
change second last line in your example
//from this:
$data = DB::table('promo');
//to this:
$data = DB::table('promo')->get();
//or to this if you want to show only one record:
$data = DB::table('promo')->where('id', $id)->get();

Related

can't refresh a page after updateing a form in laravel

hello i need to do an update inside a model using Laravel, the problem is when i click on the update button (his name in the code is Modifier) the page is not refreshing.
i have tried to work with an a tag in place of button .and with the a tag the page refresh but my data is not updating.
can someone tell me where is the problem in the code
the index view code :
<table class="table table-bordered table-left">
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Email</th>
<th>Rôle</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($users as $key=>$user)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
#if ($user->is_admin==1)Administrateur
#else Caissier
#endif
</td>
<td>
<div class="btn-group">
<a class="btn btn-success" href="" data-toggle="modal" data-target="#edituser{{ $user->id }}">
<i class="fas fa-edit"></i>
</a>
<a href="" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</div>
</td>
</tr>
{{-- edit model --}}
<div class="modal right fade" id="edituser{{ $user->id }}" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="staticBackdropLabel">Modifier l'utilisateur</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('users.update', $user->id) }}" method="post">
#csrf
#method('put')
<div class="form-group">
<label for="name">Nom</label>
<input type="text" value="{{ $user->name }}" name="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" value="{{ $user->email }}" name="email" class="form-control">
</div>
<div class="form-group">
<label for="name">Mot de passe</label>
<input type="password" readonly name="password" value="{{ $user->password }}" class="form-control">
</div>
{{-- <div class="form-group">
<label for="name">Confirmer le mot de passe</label>
<input type="password" name="confirm_password" class="form-control">
</div> --}}
<div class="form-group">
<label for="name">Rôle</label>
<select name="is_admin" id="" class="form-control">
<option value="1" #if ($user->is_admin==1)
selected
#endif>Administrateur</option>
<option value="2" #if ($user->is_admin==2)
selected
#endif>Caissier</option>
</select>
</div>
<div >
<button class="btn btn-success btn-block">Modifier</button>
</div>
</form>
</div>
</div>
</div>
</div>
#endforeach
</tbody>
</table>
the controller code :
public function update(Request $request, $id)
{
$users = User::find($id);
if (!$users) {
return back()->with('Error','user created');
}
$users->update($request->all());
return back()->with('Success','user created');
}
Your button
<button class="btn btn-success btn-block">Modifier</button>,
should be
<button class="btn btn-success btn-block" type="submit">Modifier</button> in order to submit the form to your controller.

Laravel - How to Customize Ellipsis with a button

I have a web application project using Laravel-5.8. In the project, I applied HTML text editor.
Controller
public function create()
{
return view('organization.announcements.create');
}
public function store(StoreAnnouncementRequest $request)
{
try {
$announcement = new OrgAnnouncement();
$announcement->title = $request->title;
$announcement->description = $request->description;
$announcement->save();
Session::flash('success', 'Announcement is created successfully');
return redirect()->route('organization.announcements.index');
} catch (Exception $exception) {
Session::flash('danger', 'Announcement creation failed!');
return redirect()->route('organization.announcements.index');
}
}
View
<table class=" table table-bordered table-striped table-hover datatable">
<thead>
<tr>
<th width="10">
#
</th>
<th>
Announcement Title
</th>
<th>
Description
</th>
<th>
</th>
</tr>
</thead>
<tbody>
#foreach($announcements as $key => $announcement)
<td>
{{$key+1}}
</td>
<td>
{{$announcement->title ?? '' }}
</td>
<td>
{{str_limit($announcement->description, $limit = 20, $end = ' ...')}}
</td>
<td>
#can('announcement_show')
<a class="btn btn-xs btn-primary" href="{{ route('organization.announcements.show', $announcement->id) }}">
{{ trans('global.view') }}
</a>
#endcan
#can('announcement_edit')
<a class="btn btn-xs btn-info" href="{{ route('organization.announcements.edit', ['id'=>$announcement->id]) }}">
{{ trans('global.edit') }}
</a>
#endcan
#can('announcement_delete')
<a class="btn btn-xs btn-danger" data-toggle="modal" data-target="#confirm-delete{{ $announcement->id }}" data-original-title="Close">
<span style="color:white;">{{ trans('global.delete') }}</span>
</a>
#endcan
<div class="modal fade" id="confirm-delete{{ $announcement->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Delete Announcement</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{route('organization.announcements.destroy',$announcement->id)}}" method="post">
{{ csrf_field() }}
<p>Are you sure you want to delete this Announcement?</p>
<div class="modal-header">
<h4>{{ $announcement->title }}</h4>
</div>
</form>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
</td>
</tr>
#endforeach
</tbody>
</table>
The field description is the text editor. On the view blade, I truncated the field description to 20 characters and ... will be displayed as shown in my field.
However, what I want to achieve is that I want to replace the ... with a button that will have view more. Then it is clicked, it will show the full content in a modal form of redirect to another page.
How do I achieve this?
Thank you
You may use
<td>
{{str_limit($announcement->description, $limit = 20, $end = ' ...')}}
#if(strlen($announcement->description) > 20)
Show more
#endif
</td>

Getting validation to work for address in laravel

I'm making a page that before the user submits they need to add their address.
For example: The user goes to the order confirmation and they forget to add their address and they fill in the rest of the page (the page has delivery/collection radio buttons and payment option radio buttons) when the user clicks on the confirm button the page needs to go back and give an error that the address wasn't entered.
The address portion of the page only has a "Add Address" button that takes the user to a form to enter their address.
Is there a way to do this. I thought maybe validation would work but I don't think I'm doing it correctly in this instance.
I have a hidden form that grabs some info and it has an address field in it so that it gets passed into the controller so that I can save it, but it's like it doesn't pick it up
My order-confirmation.blade.php
#extends('layouts.public')
#section('content')
<div class="content_wrapper">
<h1>Order Confirmation</h1>
<?php
$delivery = getDeliveryFee();
?>
{{ $invoice_number }}
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-12 mt-15">
#if($message = Session::get('success'))
<div class="alert alert-success" role="alert">
{{ $message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
#if($message = Session::get('error'))
<div class="alert alert-danger" role="alert">
{{ $message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Billing Information
</button>
</h2>
</div>
<div id="collapseOne" class="collapse {{ !$errors->any() ? 'show' : '' }}" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<div class="row">
<div class="col-lg-6">
<div class="address">
#if ($errors->confirmation_errors->has('delivery_address'))
<div class="help-block text-danger">
<strong>Please add an address NOW</strong>
</div>
#endif
#foreach($addresses as $address)
#if(!empty($address->complex))
{{ $address->complex }},
#endif
<div>{{ $address->address }},</div>
<div>{{ $address->suburb }},</div>
<div>{{ $address->city }},</div>
<div>{{ $address->province }},</div>
<div>{{ $address->postal_code }}</div>
<div class="row edit-delete">
<div class="col-lg-2">
<a class="btn btn-primary edit-button" href="{{ route('account.edit.delivery.address', [$address->id]) }}">Edit</a>
<span>/</span>
</div>
<div class="col-lg-2">
<form action="{{ route('account.delete.delivery.address', [$address->id]) }}" method="post">
#csrf
{{ method_field('DELETE') }}
<button class="btn btn-danger delete-button"><i class="fa fa-pencil"></i> Delete</button>
</form>
</div>
</div>
#endforeach
</div>
</div>
<div class="col-lg-6">
Add Address
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0">
<button class="btn btn-link collapsed" id="delivery-collection" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Delivery/Collection
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse {{ $errors->confirmation_errors->any() ? 'show' : '' }}" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
<p>
Please select your delivery option
</p>
#if ($errors->confirmation_errors->has('delivery_collection'))
<div class="help-block text-danger">
<strong>Please select your delivery option</strong>
</div>
#endif
<div class="delivery-option">
<input type="radio" class="form-check-input {{ $errors->confirmation_errors->has('delivery_collection') ? 'is-invalid' : '' }}" name="delivery-option" id="delivery" value="delivery">
<label for="delivery" class="form-check-label">
Delivery
</label>
<input type="radio" class="form-check-input {{ $errors->confirmation_errors->has('delivery_collection') ? 'is-invalid' : '' }}" name="delivery-option" id="collection" value="collection">
<label for="collection" class="form-check-label">
Collection
</label>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingThree">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Payment Options
</button>
</h2>
</div>
<div id="collapseThree" class="collapse {{ $errors->any() ? 'show' : '' }}" aria-labelledby="headingThree" data-parent="#accordionExample">
<div class="card-body">
#if ($errors->has('payment_option'))
<div class="help-block text-danger">
<strong>Please select a payment option</strong>
</div>
#endif
<div class="row">
<div class="col-lg-12">
<div class="payment-option">
<input type="radio" class="form-check-input {{ $errors->has('payment_method') ? 'is-invalid' : '' }}" name="payment_method" id="payfast-eft" value="payfast-eft">
<label for="payfast-eft" class="form-check-label">
EFT with PayFast
</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingThree">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseFour" aria-expanded="false" aria-controls="collapseThree">
Review Your Order
</button>
</h2>
</div>
<div id="collapseFour" class="collapse" aria-labelledby="collapseFour" data-parent="#accordionExample">
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Code</th>
<th scope="col">Quantity</th>
<th scope="col">Unit Price</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
#foreach($products as $product)
<?php
$image = getImagesArray($product['item']['image']);
?>
<tr>
<th>
#if(!empty($image))
<img src={!! asset("product_images/thumbs/$image[0]") !!}>
#endif
{{ $product['item']['title'] }}
</th>
<td>{{ $product['item']['supplier_code'] }}</td>
<td>{{ $product['qty'] }}</td>
<td>R {{ $product['item']['price'] }}</td>
<td>R {{ $product['price'] }}</td>
</tr>
#endforeach
<tr>
<th colspan="4">
<div class="float-right">
Sub Total
</div>
</th>
<td id="totalPrice" data-price="{{ $totalPrice }}">
R {{ $totalPrice }}
</td>
</tr>
<tr class="delivery-fees">
<th colspan="4">
<div class="float-right">
Delivery Fee
</div>
</th>
<td id="delivery-price" data-price="{{ $delivery }}">
R {{ $delivery }}
</td>
</tr>
<?php
$total = $totalPrice + $delivery;
?>
<tr class="total-price">
<th colspan="4">
<div class="float-right">
Total:
</div>
</th>
<td>
R <span id="completePrice"></span>
</td>
</tr>
</tbody>
</table>
<div class="confirm-order-btn pb-15">
#foreach($products as $product)
<!-- BEGIN PAYFAST EFT -->
<div class="payfast-eft" style="display: none">
<form action="{{ route('payment.gateway') }}" method="POST">
#csrf
<input type="hidden" name="merchant_id" value="merchant_id">
<input type="hidden" name="merchant_key" value="merchant_key">
<input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">
<input type="hidden" name="m_payment_id" value="{{ $invoice_number }}">
<input type="hidden" name="amount" class="completePrice" value="">
<input type="hidden" name="item_name" value="{{ $product['item']['title'] }}">
<input type="hidden" name="item_description" value="{{ $product['item']['description'] }}">
<input type="hidden" name="email_confirmation" value="1">
<input type="hidden" name="confirmation_address" value="">
<input type="hidden" name="payment_method" value="payfast_eft">
<input type="hidden" name="delivery_collection" class="delivery_collection" value="">
<input type="hidden" name="delivery_fee" class="delivery_fee" value="{{ $delivery }}">
<!-- THIS IS WHERE THE ADDRESS IS ADDED TO THE HIDDEN FORM -->
<input type="hidden" name="delivery_address" class="delivery_address" value="{{ $address }}">
<?php
$success = url('payfast-success');
$cancel = url('payfast-cancel');
$notify = url('payfast-notify');
$original_str = getAscii('merchant_id=merchant_id&merchant_key=merchant_key&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=email#domain.com&payment_method=eft');
$hash_str = hash('MD5', $original_str);
$hash = strtolower($hash_str);
?>
<input type="hidden" name="signature" value="{{ $hash }}">
<button type="submit" class="btn btn-success float-right confirm-payfast-order">
Confirm Order
</button>
</form>
</div>
<!-- END PAYFAST EFT -->
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
var price = $("#totalPrice").data('price'); //get data-price by this syntax
$('#completePrice').html(price);
$('.completePrice').val(price);
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="collection"){
$(".delivery-fees").hide('slow');
var price = $("#totalPrice").data('price'); //get data-price by this syntax
var deliveryprice = 0; //get data-price by this syntax
var totalPrice = parseFloat(price) + parseFloat(deliveryprice);
$('#completePrice').html(totalPrice);
$('.completePrice').val(totalPrice);
$('.delivery_collection').val('collection');
$('.confirm-order').removeAttr('disabled');
$('.confirm-payfast-order').removeAttr('disabled');
}
if($(this).attr("value")=="delivery"){
$(".delivery-fees").show('slow');
var price = $("#totalPrice").data('price'); //get data-price by this syntax
var deliveryprice = $("#delivery-price").data('price'); //get data-price by this syntax
var totalPrice = parseFloat(price) + parseFloat(deliveryprice);
$('#completePrice').html(totalPrice);
$('.completePrice').val(totalPrice);
$('.delivery_collection').val('delivery');
}
});
/* BEGIN EFT PAYFAST */
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="payfast-eft"){
$(".payfast-eft").show('slow');
$(".payfast-card").hide();
$(".i-pay").hide();
$(".confirm-order").hide();
$(".payfast-debit-card").hide();
}
});
/* END EFT PAYFAST */
});
</script>
#stop
my controller function
public function paymentGateway(Request $request)
{
if($request->payment_method == 'payfast_eft')
{
$process = 'Order Paid';
$paid = '1';
}
if($request->delivery_collection == 'collection')
{
$delivery_fee = null;
}else{
$delivery_fee = $request->delivery_fee;
}
$orders = Order::all();
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$validation = Validator::make($request->all(), $this->getRules());
if($validation->fails())
{
return redirect()->route('cart.deliveryConfirmation')
->withErrors($validation, 'confirmation_errors')
->with('error', 'There were validation errors');
}
foreach($orders as $order)
{
$order = Order::find($order->id)->where('invoice_number', $request->m_payment_id)->first();
$order->cart = serialize($cart);
$order->address = $request->delivery_address;
$order->delivered_date = null;
$order->delivery_fee = $delivery_fee;
$order->delivery_option = $request->delivery_collection;
$order->process = $process;
$order->order_date = Carbon::now('+2:00');
$order->payment_method = $request->payment_method;
$order->paid = $paid;
$order->order_price = $request->amount;
$order->save();
}
$merchant_id = $request->merchant_id;
$merchant_key = $request->merchant_key;
$return_url = $request->return_url;
$cancel_url = $request->cancel_url;
$m_payment_id = $request->m_payment_id;
$amount = $request->amount;
$item_name = $request->item_name;
$item_description = $request->item_description;
$email_confirmation = '1';
$confirmation_address = 'email#domain.com';
$payment_method = $request->payment_method;
$signature = $request->signature;
if($request->payment_method == 'payfast_eft')
{
$url = 'https://sandbox.payfast.co.za/eng/process?merchant_id='.$merchant_id.'&merchant_key='.$merchant_key.'&return_url='.$return_url.'&cancel_url='.$cancel_url.'&m_payment_id='.$m_payment_id.'&amount='.$amount.'&item_name='.$item_name.'&item_description='.$item_description.'&email_confirmation='.$email_confirmation.'&confirmation_address='.$confirmation_address.'&payment_method='.$payment_method;
}
return redirect()->to($url);
}
protected function getRules()
{
return [
'delivery_collection' => 'required',
'payment_method' => 'required',
'delivery_address' => 'required'
];
}
basically on the paymentGateway controller you need to check whether the address is provided if not redirect to the previous page where address is selected and saved in db

How can I fetch detail of each perticular ID?

In My list view I have all the details of leave. But when I click on details It will display me a Pop-up. In Pop up box it has to fetch and give me all the details of particular field but instead of that it always give me details of last inserted record
Here Is code of my list file
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Employee Name</th>
<th>Leave Type</th>
<th>Leave Duration</th>
<th>Applied On</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if($allLeaves != null)
#foreach($allLeaves as $leave)
<tr>
<td> {{ $leave->username }} </td>
<td> {{ $leave->typeOfLeave }} </td>
<td>
{{ $leave->startDate }} To
{{ $leave->endDate }}
</td>
<td> {{ $leave->startDate }} </td>
<td>
#if($leave['status']=='Pending')
<span class="btn btn-warning">Pending</span>
#elseif($leave['status']=='Approved')
<span class="btn btn-success">Approved</span>
#else
<span class="btn btn-danger">Rejected</span>
#endif
</td>
<td>Details</td>
</tr>
</tbody>
</table>
and in same page i wrote code of fetch records
<form id="myform" class="form-horizontal" method="POST" action="{{ route('approve_leave', $leave->id) }}">
#csrf
<div class="card-body">
<div class="row">
<label class="col-md-6"><strong> Employee Name</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="emp_name" disabled value="{{$leave->username}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Leave Type</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="leavetype" disabled value="{{$leave->typeOfLeave}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Leave Duration</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="leaveduration" disabled value="{{ $leave->startDate }} To {{ $leave->endDate }}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Reason</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="reason" disabled value="{{$leave->reasonForLeave}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Applied on</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="appliedon" disabled value="{{$leave->startDate}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Action</strong></label>
<div class="col-md-6">
<select class="form-control" id="status" name="status" value="{{$leave->status}}">
<option value="Pending" selected="selected">Pending</option>
<option value="Approved">Approved</option>
<option value="Rejected">Rejected</option>
</select>
</div>
</div>
#endforeach
<br>
<div class="row">
<label class="col-md-6"><strong>Reason For Action</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="reason" name="reasonForAction" placeholder="Reason Of Action" style="border:none">
</div>
</div>
<br>
<div class="modal-footer">
<button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Save</button>
<button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
And This is the code i wrote in controller file
//Code Of list view
public function listOfLeave()
{
$allLeaves = LeaveManagement::all();
return view('pages.leavelist', compact('allLeaves'));
}
//Code of click on details button and fetch record of that particular id
public function handleLeave($id)
{
$leave = LeaveManagement::find($id);
return view('pages.leavelist', compact('leave', 'id'));
}
//code of approve reject leave and change the status of leave
public function approveLeave(Request $request ,$id)
{
$leave = LeaveManagement::find($id);
$leave->status = $request->get('status');
$leave->reasonForAction = $request->get('reasonForAction');
$leave->save();
return view('pages.leavelist');
}
If you want to show data according to user's id then you have to pass each field like data-fieldname and then you can fetch that field's data as shown in script.
Button:
<button type="button"
class="btn btn-xs btn-default confirm-modal"
data-toggle="modal"
data-target="#model"
data-id="{{ $singleRecord->id }}"
data-name="{{ $singleRecord->full_name }}"
data-leave_reason="{{ $singleRecord->leave_reason }}"
data-from_date="{{ date('d-F-Y', strtotime($singleRecord->from_date)) }}"
data-to_date="{{ date('d-F-Y', strtotime($singleRecord->to_date)) }}"
>Details </button>
And add script
<script type="text/javascript">
$('.confirm-modal').click(function (event) {
$('.employee_name').text($(this).data('name'));
$('#from_date').text($(this).data('from_date'));
$('#to_date').text($(this).data('to_date'));
$('#total_leaves').text($(this).data('total_leaves'));
$('.leave_reason').text($(this).data('leave_reason'));
})
</script>
In model div, add each id to display that data.
<div class="col-md-12">
<label for="to_date">To Date:</label>
<span id="to_date"></span>
Hope this helps :)
Edit your modal button and modal id like below,
<td>Details</td>
and your modal,
<div id="details_{{ $leave->id }}" class="modal fade edit-form" role="dialog">
or you can use javascript to populate your field,
<button type="button" class="btn btn-xs btn-default confirm-modal" data-toggle="modal" data-target="#details"
data-leave="{{ $leave }}">Details </button>
<script type="text/javascript">
$(document).on('click', '.confirm-modal', function(e){
var data = $(this).data('leave');
$('#emp_name').text(data.username);
$('#from_date').text(data.from_date');
$('#to_date').text(data.to_date);
$('#total_leaves').text(data.total_leaves);
$('#leave_reason').text(data.leave_reason);
})
</script>

Getting error while clicking on edit function button in laravel

I need to edit by data from database via my admin panel. So i created table with clickable button for edit function.
Now when I click on edit button, I am seeing ID number at bottom but getting sorry page couldn't found error ! I have double checked all controller, route and everything. It seems all good, but I don't know what's the error!
Route Code:
Route::get('/admin/baseFare',[
'uses' => 'ExtraBaseFareController#basefare',
'as' => 'base.fare'
]);
Route::get('/admin/baseFare/edit/{$id}',[
'uses' => 'ExtraBaseFareController#editBaseFare',
'as' => 'editbase.fare'
]);
Route::post('/admin/baseFare/update/{id}', [
'uses' => 'ExtraBaseFareController#baseFareUpdate',
'as' => 'base.fareupdate'
]);`
Controller Code:
public function basefare()
{
$base = BaseFare::all();
return view('Admin.BaseFare.index')->With('base', $base);
}
public function editBaseFare($id)
{
$base = BaseFare::find($id);
return view('Admin.BaseFare.editBaseFare')->with('base', $base);
}
public function baseFareUpdate(Request $request, $id)
{
$base = BaseFare::find($id);
$base->fareinpercentage = $request->fareinpercentage;
$base->fareinrupees = $request->fareinrupees;
$base->save();
Session::flash('success','Base fare successfully updated');
return redirect()->route('base.fare');
}
Index Page code:
<table class="table display nowrap table-striped table-bordered bootstrap-3 scroll-horizontal">
<thead>
<tr>
<th>S.No</th>
<th>Fare in Percentage (%)</th>
<th>Fare in Rupees (Rs)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#php $number = 1; #endphp
#foreach($base as $base)
<tr>
<td>
{{ $number.'.' }}
#php $number++; #endphp
</td>
<td>{{ $base->fareinpercentage }}</td>
<td>{{ $base->fareinrupees }}</td>
<td>
<a href="{{ route('editbase.fare',['id' => $base->basefareid ]) }}" class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill" title="Edit ">
<i class="la la-edit"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>`
Edit Page Code:
<form class="form" method="post" action="{{ route('base.fareupdate',['id' => $base->basefareid ]) }}">
<div class="form-body">
<h4 class="form-section"><i class="la la-eye"></i>Base Fare Controller</h4>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userinput2">Fare in Percentage (%)</label>
<input type="text" id="fareinpercentage" value="{{ $base->fareinpercentage }}" class="form-control border-primary" name="fareinpercentage">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userinput3">Fare in Rupee (Rs)</label>
<input type="text" id="fareinrupees" value="{{ $base->fareinrupees }}" class="form-control border-primary" name="fareinrupees">
</div>
</div>
</div>
</div>
<div class="form-actions right">
<button type="button" class="btn btn-warning mr-1">
<i class="ft-x"></i> Cancel
</button>
<button type="submit" name="submit" class="btn btn-primary">
<i class="la la-check-square-o"></i> Save
</button>
</div>
</form>`
These are the codes, the kindly help me to find our the error, The main function is to edit the field from database!
If I understand the question correctly you can't go to the edit page.
Run 'php artisan route:list' and compare the routes.
And I can't figure out why you have dollar sign before the id in the route.

Resources