Method appends does not exist Laravel - laravel

I want to select all hotels where minrate between 0 and 49 it marche perfectly , but when i want to select all hotels where minrate between 0-49 and 50-99 It is you to say when it enters the loop "for" it return this error
ErrorException in Macroable.php line 74: Method appends does not exist. (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php) (View: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php)
Function Laravel
public function pricefilter(Request $request)
{
$query = Input::get('query', false);
$brans = request()->priceFilter;
$books = new Collection();
if (!empty($brans)) {
$product = "";
foreach ($brans as $data) {
$minMax = explode("-",$data);
$product[] = Afica::search()
->multiMatch(['name','city_hotel'], $query, ['fuzziness' => 'AUTO'])
->filter()
->range('minrate',['from'=>$minMax[0],'to'=>$minMax[1]])
->paginate(26)
->appends('priceFilter' , request('priceFilter'));
}
$books = $product[0];
for ($i = 1; $i < count($product); $i++) {
$books = $books->union($product[$i]);
}
} else {
$books = Afica::search()
->paginate(26);
}
if ($request->ajax()) {
return view('presult', compact('books'));
}
return view('welcome',compact('books'));
}
View presult
<!--deal-->
<?php $myArray = array();?>
#if (empty($myArray))
#foreach($books as $Afica)
<?php $collection = collect($Afica);?>
<article class="one-third">
<figure><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></figure>
<div class="details">
<h3>{{{ $collection->get('name') }}}
<span class="stars">
<?php
for ($i=1 ; $i<= $collection->get('class') ; $i++)
{
echo ' <i class="material-icons"></i>';
}
?>
</span>
</h3>
<span class="address">{{{ $collection->get('city_hotel') }}},{{{ $collection->get('address') }}} Show on map</span>
<span class="rating">{{{ $collection->get('preferred') }}}</span>
<span class="price">Max rate <em>$ {{{ $collection->get('maxrate') }}}</em> </span>
<span class="pricee">Min rate <em>$ {{{ $collection->get('minrate') }}}</em> </span>
<div class="description">
<p>{{{ $collection->get('desc_en') }}}More info</p>
</div>
Book now
</div>
</article><?php $myArray[] = $collection->get('id');?>
<!--//deal-->
#endforeach
#endif
<!--//bottom navigation-->
<div class="bottom-nav">
Back up
<div class="pager">
<?php $query=Input::get('query', '');?>
#if($query == "")
<span>First Page</span>
#else
<span><a href="?query=<?php echo $query?>&page=1"=>FirstPage</a></span>
#endif
{!! $books->appends(['query' => Input::get('query')])->render() !!}
#if($query == "")
<span>Last Page</span>
#else
<span>Last Page</span>
#endif
<!--bottom navigation-->
</div>
</div>
<!--//bottom navigation-->

It looks like you are calling ->appends on your variable called $books in several places within your view.
If $books is empty (i.e. null) this might be causing your problems.
I would suggest wrapping your $books in if is set functions as a starting point
if (isset($books) && $books != NULL) {
$books->appends(['query' => Input::get('query')])->render()
} else {
// Do something which tells the user that there are no books
}
An even better solution would be to handle all of the logic for the view in your controller and use something like a #forelse loop instead of a #foreach loop. This would make it easier to handle situations where you return an empty collection.
#forelse ($books as $book)
// Do your for each book markup
#empty
// Do markup which shows when you have no books
#endforelse
Corrections to the template
<?php $myArray = array(); ?>
#if (empty($myArray))
#foreach($books as $Afica)
<?php $collection = collect($Afica);?>
<article class="one-third">
<figure><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></figure>
<div class="details">
<h3>{{{ $collection->get('name') }}}
<span class="stars">
<?php
for ($i=1 ; $i<= $collection->get('class') ; $i++) {
echo ' <i class="material-icons"></i>';
}
?>
</span>
</h3>
</div>
<span class="address">{{{ $collection->get('city_hotel') }}},{{{ $collection->get('address') }}} Show on map</span>
<span class="rating">{{{ $collection->get('preferred') }}}</span>
<span class="price">Max rate <em>$ {{{ $collection->get('maxrate') }}}</em> </span>
<span class="pricee">Min rate <em>$ {{{ $collection->get('minrate') }}}</em> </span>
<div class="description">
<p>{{{ $collection->get('desc_en') }}}More info</p>
</div>
<div>
Book now
</div>
</article><?php $myArray[] = $collection->get('id');?>
#endforeach
#endif
<div class="bottom-nav">
Back up
<div class="pager">
<?php $query=Input::get('query', '');?>
#if($query == "")
<span>First Page</span>
#else
<span>FirstPage</span>
#endif
{!! $books->appends(['query' => Input::get('query')])->render() !!}
#if($query == "")
<span>Last Page</span>
#else
<span>Last Page</span>
#endif
</div>
</div>

Related

laravel blade - show only one data from database

I have a table with multiple fields called "skills" . how do I show only one(first) data associated with "skills" .
#foreach ($others as $other )
#if ($other->type == 'skills')
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
#endif
#endforeach
in my controller-
public function index()
{
$sliders = Slider::latest()->with('service')->get();
$clients = OurClient::all();
$galleries = Gallery::all();
$services = Service::all();
$others = Others::all();
return view('frontend.pages.home',compact('services','galleries','clients','others','sliders'));
}
this is showing the same data twice. I want only the first data
I solved it. changed the blade file
<div class="row">
<?php $count = 0; ?>
#foreach ($others as $other )
#if ($other->type == 'skills')
<?php if($count == 1) break; ?>
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
<?php $count++; ?>
#endif
#endforeach
</div>

LaravelShoppingcart package, rowId property not working

Hi I'm using the LaravelShoppingcart package(https://github.com/bumbummen99/LaravelShoppingcart) and When I output {{ $item->rowId }} on the blade template I get the rowId on the browser when the blade is rendered but when I pass it in the qtyIncreased({{ $item->rowId}}) method and try to dump and die it in the livewire component it doesn't work. Mark you when I pass the $item->id and dump and die it, it works. The only error I'm getting is on the console and is:
Error handling response: TypeError: Cannot destructure property 'yt' of 'undefined' as it is undefined.
at chrome-extension://cmedhionkhpnakcndndgjdbohmhepckk/scripts/contentscript.js:535:6
Any assistance is highly appreciated.
Live wire blade component
<div class="cart-overlay {{ $active ? 'transparent' : '' }}">
<div class="cart {{ $active ? 'showCart' : '' }}">
<span class="close-cart" wire:click="$emit('closeCart')">
<i class="fas fa-window-close"></i>
</span>
<h2>Cart</h2>
<div class="cart-content">
#foreach ($cartContent as $item)
<div class="cart-item">
<img src="{{ $item->options->image }}" alt="product" />
<div>
<h4>{{ $item->name }}</h4>
<h5>${{ $item->price }}.99</h5>
<span class="remove-item">remove</span>
</div>
<div>
<i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item-
>rowId }})"></i>
<p class="item-amount">{{ $item->qty }}</p>
<i class="fas fa-chevron-down"></i>
</div>
</div>
#endforeach
</div>
<div class="cart-footer">
<h3>your total : $ <span class="cart-total">{{ $cartTotal }}</span></h3>
<button class="clear-cart banner-btn">clear cart</button>
</div>
</div>
</div>
Livewire Class component
<?php
namespace App\Http\Livewire;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class CartOverlay extends Component
{
public $active = false;
public function render()
{
$cartContent = Cart::content();
$cartTotal = Cart::total();
return view('livewire.cart-overlay', compact(['cartContent', 'cartTotal']));
}
public $listeners = [
'showCart',
'closeCart'
];
public function qtyIncreased($rowId)
{
dd($rowId);
}
public function showCart()
{
$this->active = true;
}
public function closeCart()
{
$this->active = false;
}
}
I managed to fixed it, the parameter in the function should be passed in as a string. So, I added quotes around the parameter. Changed it from <i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item->rowId }})"></i>
TO
<i class="fas fa-chevron-up" wire:click="qtyIncreased('{{ $item->rowId }}')"></i>

i want to hide html code when product is_featured column status all equal to zero using laravel

i am trying to hide html code by if condition when all product featured is zero, html code should not be shown in front page please help me how can i do that ?
controller
public function index()
{
$data = [
'products' => Product::with('productColorGallary')->where('is_featured', 1)->first(),
];
return view('home', $data);
}
html view
#if($products->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$products->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#endif
You are getting your products in your database with is_featured = 1. It means that your if condition in your blade will be always false.
Plus, are you trying to get all products or just one ?
If it's many, then :
Your controller
public function index()
{
$products = Product::with('productColorGallary')->get();
return view('home', compact('products');
}
and your blade
#foreach($products as $product)
#if($product->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$product->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#else
SOMETHING HERE IF PRODUCT IS FEATURED
#endif
SOMETHING HERE COMMONS TO BOTH FEATURED AND NOT FEATURED
#endforeach
Products is an array key, so you can use like $data['products'] etc..
But if you create a collection/object like the following should be work:
public function index()
{
// first will return the first matched item from db
// and you will get only is_featured = 1
$products = Product::with('productColorGallary')->where('is_featured', 1)->first();
return view('home', compact('products');
}

How do I get total in a foreach loop

I have a div that displays items , quantities and total prices for each item using a foreach loop. I now want to get the total amount and dont seem to get it.
This is my blade file
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
<?php
$total = 0;
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
?>
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
<?php $total_amount += $total?>
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
I have also tried <div><strong class="order-total">KSh <?php echo $total_amount?> </strong></div> but still does not work.
I get an error of undefined variable total_amount. How do I go about it?
The variables $total and $total_amount must be outside of the foreach
here I have rewritten your code
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
#php $total = 0; $total_amount = 0; #endphp
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
#php
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
#endphp
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
#php $total_amount += $total; #endphp
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>

How to add header and footer on every page with wkhtmltopdf unpatched qt?

I am using laravel-snappy & wkhtmltopdf 0.12.2.4 (unpatched qt) which doesn't support the --header-html switch so I have to manually create a header but the problem is the header and footer only appear on the first page. Is it possible to add a header and footer to every page without upgrading or downgrading wkhtmltopdf?
html
<div class="container">
<div class="header">
<img src="{{URL ('/images/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>Parts Orientation</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
<div class="col-xs-2 marginThumbnail">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="120" height="120"/>
<p class="displayText {{$part->class}}">
{{$part->name}}
</p>
</div>
</div>
#endforeach
<div class="footer">
<p> {{$date}} </p>
</div>
</div>
</div>
</body>
</html>
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString()])
->setOrientation('landscape')
->setPaper('a4');
}
}
I was able to figure out a solution that works for me. Its very messy but it works
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
// divide by 36 because I can fit 36 items on each page
$totalPages = ceil(count($parts)/36);
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString(), "totalPages" => $totalPages])
->setOrientation('landscape')
->setPaper('a4');
}
}
html
<body>
#php
$count = 0;
$page = 0;
#endphp
<div class="container">
<div class="header">
<img src="{{URL ('/img/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>PARTS ORIENTATION</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
#php
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
if ($count === 36) {
echo '<div class = "newPageHeader">';
echo '<h2>PARTS ORIENTATION</h2>';
echo '</div>';
$count = 0;
$page++;
}
#endphp
<div class="col-xs-2 outline">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="127" height="127"/>
<p class="displayText {{$part->c,ass}}">
{{$part->text}}
</p>
</div>
</div>
#php
$count++;
if ($count == 36) {
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
$page = ($page == 0) ? 1 : $page;
echo '<footer class="footer">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='pageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
elseif ($totalPages == $page) {
echo '<footer class="lastFooter">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='lastPageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
#endphp
#endforeach
</div>
</div>
</body>

Resources