方法追加不存在 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42673143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method appends does not exist Laravel
提问by koora et film Tv
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
我想选择所有在 0 到 49 之间的酒店,它完美地进行,但是当我想选择所有在 0-49 和 50-99 之间的酒店时,当它进入循环“for”时,它会返回这个错误
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)
Macroable.php 第 74 行中的 ErrorException:方法追加不存在。(查看: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php) (查看: C:\xampp\htdocs\elasticsearch\resources\views\presult.blade.php)
Function Laravel
功能 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><a href="#" title=""><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></a></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') }}} <a href="">Show on map</a></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') }}}<a href="hotel?query= $collection->get('name')">More info</a></p>
</div>
<a href="{{{ $collection->get('hotel_url') }}}" title="Book now" class="gradient-button">Book now</a>
</div>
</article><?php $myArray[] = $collection->get('id');?>
<!--//deal-->
@endforeach
@endif
<!--//bottom navigation-->
<div class="bottom-nav">
<a href="#" class="scroll-to-top" title="Back up">Back up</a>
<div class="pager">
<?php $query=Input::get('query', '');?>
@if($query == "")
<span><a href="?page=1">First Page</a></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><a href="?page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
@else
<span><a href="?query=<?php echo $query?>&page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
@endif
<!--bottom navigation-->
</div>
</div>
<!--//bottom navigation-->
回答by Spholt
It looks like you are calling ->appends
on your variable called $books
in several places within your view.
看起来您正在调用->appends
在$books
视图中的多个位置调用的变量。
If $books
is empty (i.e. null
) this might be causing your problems.
如果$books
为空(即null
),这可能会导致您的问题。
I would suggest wrapping your $books
in if is set functions as a starting point
我建议将您$books
的 if is set 功能包装为起点
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
循环而不是@foreach
循环之类的东西。这样可以更轻松地处理返回空集合的情况。
@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><a href="#" title=""><img src="{{ $collection->get('photo_url') }}" alt="" style="height: 215px!important;" /></a></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') }}} <a href="">Show on map</a></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') }}}<a href="hotel?query= $collection->get('name')">More info</a></p>
</div>
<div>
<a href="{{{ $collection->get('hotel_url') }}}" title="Book now" class="gradient-button">Book now</a>
</div>
</article><?php $myArray[] = $collection->get('id');?>
@endforeach
@endif
<div class="bottom-nav">
<a href="#" class="scroll-to-top" title="Back up">Back up</a>
<div class="pager">
<?php $query=Input::get('query', '');?>
@if($query == "")
<span><a href="?page=1">First Page</a></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><a href="?page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
@else
<span><a href="?query=<?php echo $query; ?>&page={!! $books->appends(['query' => Input::get('query')])->lastPage() !!}">Last Page</a></span>
@endif
</div>
</div>