Nested html formBuilder with Laravel - laravel

cart.blade.php
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
{!! Form::open(["url"=>"/pay"]) !!}
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop
Issue: I can delete any item, but during click check out nothing happens, but when I remove clear item form, check out run successfully.
I want to run two operations, How Can I solve it?
Thanks

You can have several forms in a page but they should not be nested.
As given in html5 working draft:
4.10.3 The form element
Content model:
Flow content, but with no form element descendants.
As far as I know form is just the html tags which can be used to send a group(array) of data at once to the server.
In your case your best bet would be to use the ajax query.(though it would make our page javascript dependent)
Or, as I observed you can just seperate the two form like below:
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
<<!-- Remove the Form tag from here and insert it below -->>
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<<!-- Remove the Form tag from above and insert it below -->>
{!! Form::open(["url"=>"/pay"]) !!}
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop

Related

cart total only calculating for the first 2 products only in laravel

I have 2 types of products in my app.Products with attribute and products without attribute. I can successfully add the products to cart, but I am having an issue whereby on the part of showing the grand total,only the total of the first 2 products subtotal are calculated and shown.I haven't understood where the bug is. I have tried debugging the code but still can't figure it out. Here is the HTML code that process the grand total price.
//table that shows the cart details and where the subtotal is calculated
<table class="userdatatable table table-striped table-bordered nowrap" style="width:100%; border:2px solid black;">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Discount</th>
<th>Total</th>
<th>Remove</th>
</tr>
</thead>
<body>
<?php $total_price=0; ?>
<?php $attributetotal_price=0; ?>
<?php $noattributetotal_price=0; ?>
#foreach($cartitems as $item)
<tr>
#if ($item->product->is_attribute==1)
<?php $attrpric=Merchadise::getdiscountedattrprice($item['product_id'],$item['size']);
?>,
#else
<?php $discountedprice=Merchadise::getdiscountedprice($item['product_id']);
?>
#endif
<td>{{ $item->product->merch_name }}</td>
#if ($item->product->is_attribute==1)
<td>{{ $attrpric['merch_price'] }}</td>
#else
<td>{{ $item->product->merch_price }}</td>
#endif
<td>
<img src="{{ asset ('images/productimages/small/'.$item->product->merch_image) }}" style="width:100px; height:100px;" alt="Product">
</td>
<td>
<button class="itemupdate qtyminus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
<input data-id={{ $item->id }} class="quantity" min="1" name="quantity[]" value="{{ $item->quantity }}" type="number">
<button class="itemupdate qtyplus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
</td>
#if ($item->product->is_attribute==1)
<td>sh.{{ $attrpric['discount'] * $item['quantity'] }}</td>
<td>sh.{{ $attrpric['final_price'] * $item['quantity'] }}</td>
#elseif($item->product->is_attribute==0)
<td>{{ ($item->product->merch_price-$discountedprice) * $item['quantity'] }}</td>
<td>{{ $discountedprice * $item['quantity'] }}</td>
#endif
<td>
<a class="btn btn-primary btn-xs" onclick="confirm return('Are you Sure You want to Delete?')" href="{{ route('deletecartitem', $item->id) }}"><i class="fa fa-trash"></i></a>
</td>
</tr>
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$total_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$total_price+($discountedprice * $item['quantity'] );?>
#endif
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-5 ml-auto">
<div class="cart-page-total">
<h2>Cart totals</h2>
<ul class="mb-20">
<li>Coupon Discount
<span class="couponAmount">
#if (Session::has('couponAmount'))
-Sh.{{ Session::get('couponAmount') }}
#else
sh.0
#endif
</span>
</li>
<li>Grand Total
<span class="grand_total">Sh.{{ $attributetotal_price+$noattributetotal_price-Session::get('couponAmount') }}</span>
</li>
</ul>
#auth
Checkout <i class="fa fa-angle-right"></i>
#else
<p>To proceed to checkout create or log in to your account...</p>
<span href="#" data-toggle="modal" data-target="#RegistrationModal" class="btn btn-success btn-block">Create/Login an Account<i class="fa fa-angle-right"></i></span>
#endauth
</div>
</div>
</div>
here is my products migration table
it was just a minor bug.i removed the $total_price
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$attributetotal_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$noattributetotal_price+($discountedprice * $item['quantity'] );?>
#endif

Laravel Problem Call to undefined method App\Models\User::getRoleNames()

#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Users Management</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th>Roles</th>
<th width="280px">Action</th>
</tr>
#foreach ($data as $key => $user)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
#if(!empty($user->getRoleNames()))
#foreach($user->getRoleNames() as $v)
<label class="badge badge-success">{{ $v }}</label>
#endforeach
#endif
</td>
<td>
<a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
{!! $data->render() !!}
<p class="text-center text-primary"><small>Tutorial by ItSolutionStuff.com</small></p>
#endsection
Im agree with Gary, if you are using Spatie add the next code to User.php
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
Please add this method into User.php model file
public function getRoleNames()
{
return $this->belongsToMany(Role::class);
}
If this method not work then share you table structure and user model file code.
In the controller that call this view, you should to do:
$data = Model::all(); // ---> this solved that for me
return view('model.view',compact('data'));

how to implement Laravel AJAX live search on CRUD management table?

I have a CRUD table that's holding records of doctors that get
retrieved from the MySQL database. I've been following a few tutorials
and managed to set up AJAX live results search.
Now the problem comes from the fact that the logic is stored in a
Controller. I can succesfully retrieve the details from the database
using '-- foreach($data as $row) --' but I don't know how to retrieve
the CRUD actions ( In this case Delete.The create method is separated
from the table itself. I also can't implement status cell using i)
I've tried putting the actions inside the td'. .'td. tags but I get
Routing errors.
Any help is appreciated.
I have the code for the CRUD actions, images etc..inside
"index.blade.php". They should be somehow implemented in the
controller though. doctor controller
function liveSearchDoctor(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data =Doctor::
where('name', 'like', '%'.$query.'%')
->orWhere('mobile', 'like', '%'.$query.'%')
->orWhere('email', 'like', '%'.$query.'%')
->orderBy('id', 'desc')
->paginate(2);
}
else
{
$data =Doctor::latest()->paginate(2);
}
$total_data = $data->count();
if($total_data > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td class="center"><img src="'.$row->image.'" style="width:40px;height:40px;"/></td>
<td>'.$row->id.'</td>
<td>'.$row->name.'</td>
<td>'.$row->department->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->mobile.'</td>
//status should be implement here
<td><a class="btn btn-primary" href="'.route('doctors.edit',$row->id).'"><i class="fas fa-edit">Edit</i></a>
//delete form should be implement here
</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_data
);
echo json_encode($data);
}
}
index.blade.php (( Note: The CRUD actions and everything is here
implemented succesfully. I however can't implement delete in the
controller )) here is the index.blade.php
#extends('layout.master')
#section('css')
#endsection
#section('content')
<br>
<div class="br-pagebody">
<div class="br-section-wrapper">
#include('include._message')
<div class="row">
<h6 class="br-section-label">Doctors List</h6>
</div>
<div class="row">
<div class="col-lg-4 ">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Search doctor Data" />
</div>
</div>
<div class="col-lg-3 offset-lg-5">
Add New Doctor
</div>
</div>
</br>
<div class="table-responsive">
<table class="table table-bordered table-colored table-dark">
<thead class="thead-colored thead-dark">
<tr>
<th>Image</th>
<th>Doctor Id</th>
<th>Name</th>
<th>Department</th>
<th>Email</th>
<th>mobile</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($doctors as $doctor)
<tr>
<td class="center"><img src="{{URL::asset($doctor->image)}}" style="width:40px;height:40px;"/></td>
<td class="center">{{$doctor->id}}</td>
<td class="center">{{$doctor->name}}</td>
<td class="center">{{$doctor->department->name}}</td>
<td class="center">{{$doctor->email}}</td>
<td class="center">{{$doctor->mobile}}</td>
<td class="center">
#if($doctor->status==1)
<span class="badge badge-success">Active</span>
#else
<span class="label label-danger">Deactive</span>
#endif
</td>
<td class="center">
<a class="btn btn-primary" href="{{route('doctors.edit',$doctor->id)}}">
<i class="fas fa-edit">Edit</i>
</a>
{!! Form::open(['method' => 'DELETE','route' => ['doctors.destroy', $doctor->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
</div><!-- table-wrapper -->
<div class="d-flex justify-content-center">
{!! $doctors->links() !!}
</div>
</div>
</div>
#endsection
#section('js')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
fetch_doctor_data();
function fetch_doctor_data(query = '')
{
console.log(query)
$.ajax({
url:"{{route('search.doctor')}}",
method:"GET",
data:{'query':query},
dataType:'json',
success:function(data)
{
console.log(data)
$('tbody').html(data.table_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_doctor_data(query);
});
});
</script>
#endsection

Display date to view with a M-D-Year

I am new to laravel, with regards to displaying the date to the view with the MM-DD-YYYY format
#extends('layouts.common')
#section('title' ,'Administrator | Category')
#section('contents')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">All Categories</div>
<div class="panel-body">
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Category Name</th>
<th>Published At</th>
</tr>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td><a href="{{ url('/category', $category->id) }}"> {{
$category->name }}</a></td>
<td>{{$category->published_at }}</td>
<td><a href="{{ route('category.edit',$category->id) }}"><i
class="icon fa fa-edit"></i>&nbsp&nbsp&nbsp</a><i
class="icon fa fa-times"></i></td>
</tr>
</tbody>
#endforeach
</table>
</div>
</div>
</div>
</div>
#endsection #section('js') {!!Html::script('assets/js/jquery.min.js')!!}
{!!Html::script('assets/js/bootstrap.min.js') !!} #endsection
With regards to that i would like to display to the view the {{$category->published_at }} data into that format. Please help thanks
Please follow way below
{!! date('d-m-Y', strtotime($model->start_date)) !!}
or
{{ date('d-m-Y', strtotime($model->start_date)) }}
I Hope Help You!
In your Category model
public $dates = ['published_at'];
Then in view you can use
$category->published_at->format('m-d-Y')
Further reading: Eloquent: Mutators
Another approach. Extends Skysplit answer
In Category Model
public function getPublishedAtAttribute(){
return $this->attributes['published_at']->format('m-d-Y');
}
And never worry about published_at again. Just
$category->published_at

pagination in laravel 5.3 when count of pages = 1

I have cities table and it has 7 cities I have a view page to display these cities with 10 cities per page
Controller:
$cities = City::orderBy('id', 'desc')->paginate(10);
return view('cities.home', compact('cities'));
View:
<div class="table-responsive panel panel-sky">
<table class="table table-striped table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>Created At</th>
<th>Action</th>
</tr>
#foreach($cities as $city)
<tr id="product{{$city->id}}">
<td>{{$city->id}}</td>
<td>{{$city->name}}</td>
<td>{{ $city->created_at }}</td>
<td>
<i class="glyphicon glyphicon-edit action-icon"></i>
<a type="button" class="pointer" data-toggle="modal" data-target="#deleteModal" data-type="btn-danger" data-action="delete-product" data-id="{{ $city->id }}" data-msg="Are you sure you want to delete this city?" data-title="Confirm Deletion">
<span class='glyphicon glyphicon-remove action-icon'></span>
</a>
</td>
</tr>
#endforeach
</table>
<div>
<div class="col-md-12"><?php echo $cities->render(); ?> </div>
but the issue is the paginate render is displayed with page#1, this issue occurred in laravel5.3 and did not find in laravel5.2
If you want to render pagination in your own way, you can use paginator methods. For example, to check if there is just one page, you could try:
#if ($cities->total() > 10)
// Render pagination
#endif

Resources