method links does not exist - laravel 5.4 - laravel

I am using filter by serial number in table,, but when I click the button error like this
" method links does not exist"
this is my controller
public function show(Request $request)
{
$instrument = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instrument->appends($request->only('serial_number'));
return view('settinginstrument.index', compact('settinginstrument'));
}
this is my blade:
<table class="table">
#foreach ($instruments as $instrument)
<tr>
<td>
Serial Number : {{ $instrument->serial_number }}
#if($instrument->customer !== NULL)
<div class="text-muted"><i class="fa fa-hospital-o" aria-hidden="true"></i> {{ $instrument->customer->name }}</div>
#endif
#if($instrument->contractType !== NULL)
<div class="text-muted"><i class="fa fa-compress" aria-hidden="true"></i> {{ $instrument->contractType->name }}</div>
#endif
</td>
<td>
<div class="pull-right">
<a type="button" href="{{ route('instrument.edit', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
class="btn btn-success ">
<i class="fa fa-pencil"></i> Edit
</a>
#if($instrument->customer == NULL || $instrument->contractType == NULL)
<a href="{{ route('instrument.destroy', ['instrumentType' => $instrumentType->id, 'instrument' => $instrument->id]) }}"
data-method="delete" data-confirm="" class="btn btn-danger">
<i class="fa fa-trash"></i> <span class="hidden-xs">Delete</span>
</a>
#endif
</div>
</td>
</tr>
#endforeach
</table>
<div class="paginator text-center">{{ $instruments->links() }}</div>
#else
....................................
where wrong with my code?

You are not passing the param to the view:
public function show(Request $request)
{
$instruments = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instrument->appends($request->only('serial_number'));
return view('settinginstrument.index',['instruments' => $instruments);
}
Then on the blade:
{{$instruments->links()}}

When you have zero counts of record and you are trying to access the links() then such issue occurs
Add on condition to check the record exists.
#if($instruments)
<div class="paginator text-center">{{ $instruments->links() }}</div>
#endif
Also in the controller you have one typo error $instruments and $instrument
I recommend that add the condition above the foreach
and on blank result show some message that record not found.
Update
Also pass the $instruments to the balde
$instruments = Instrument::when($request->serial_number, function ($query) use ($request) {
$query->where('serial_number', 'like', "%{$request->serial_number}%");
})->paginate(5);
$instruments->appends($request->only('serial_number'));
return view('settinginstrument.index', compact('instruments'));

Related

How do I use 2 variable in foreach in laravel blade

One variable is used for searching and
Another variable is used to display name based on id from DB
public function recorddisplay(Request $request)
{
$search =$request['search'] ??"";
if($search != "")
{
$covidrecord=Covidrecord::where('fullname','LIKE',"%$search%")
->orWhere('province','LIKE',"%$search%")
->orWhere('district','LIKE',"%$search%")
->orWhere('localgovernment','LIKE',"%$search%")->paginate(15);
}
else{
$covidrecord= Covidrecord::paginate(15);
}
$data = compact('covidrecord','search');
$dataCovid=DB::table('coviddeathrecord')
->join('district','coviddeathrecord.district','=','district.id')
->join('province','coviddeathrecord.province','=','province.id')
->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);
return view('admin.dashboard.display', compact('dataCovid'))->with($data);
}
This is the blade
#foreach(array_merge($covidrecord,$dataCovid) as $data)
<tr>
<td>
<div class="d-flex px-2 py-1">
<div class="d-flex flex-column justify-content-center">
{{-- <label>{{ $data->fullname }}</label> --}}
<h6 class="mb-0 text-sm">{{$data->fullname}}</h6>
<p class="text-xs text-secondary mb-0">{{$data->gender }} ,{{$data->age }} वर्ष</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{ $address->province }}</p>
<p class="text-xs text-secondary mb-0">{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data->tole }}</p>
</td>
#endforeach
As I am using array_merge, it is throwing the error
//array_merge(): Expected parameter 1 to be an array, object given
You can pass variables in a compact function:
return view('admin.dashboard.display', compact('data', 'dataCovid'));
Also please see compact function
return like this
return view('website.condo_living.index')->with([
'var1'=>$var1,
'var2'=>$var2,
]);
to use 2 variables in foreach in your blade. It´s better that you pass you data from controller
return view('yourSubFolderView.View')->with('yourVariable')->with('yourVariable')
Also you can use
return view('yourSubFolderView.View')->compact('yourVariable', 'yourVariable')
UPDATE
in this line:
return view('admin.dashboard.display', compact('dataCovid'))->with($data);
change to:
return view('admin.dashboard.display', compact('dataCovid'))->with('data',$data);
and in your blade, you can access to $data or $dataCovid with for-each
in your code was missing your variable name 'data', $data
FYI, the a covidrecord variable returns collections of data and a dataCovid variable that returns an array of data that you have to standardize the types of variables to deal with in the view array only or collection only
I convert the data type to collection of array
and passing collection correctly from your method:
public function recorddisplay(Request $request)
{
$search = $request->get('search') ?? "";
if($search != "") {
$covidrecord = DB::table('covidrecord')->where('fullname','LIKE',"%$search%")
->orWhere('province','LIKE',"%$search%")
->orWhere('district','LIKE',"%$search%")
->orWhere('localgovernment','LIKE',"%$search%")
->paginate(15)
->items();
} else {
$covidrecord= DB::table('covidrecord')->paginate(15)->items();
}
$dataCovid = DB::table('coviddeathrecord')
->join('district','coviddeathrecord.district','=','district.id')
->join('province','coviddeathrecord.province','=','province.id')
->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);
$items = collect($users)->concat($data);
return view('admin.dashboard.display', compact('items'));
}
and within the blade you can looping normal with collection of array like:
#foreach($items as $data)
<tr>
<td>
<div class="d-flex px-2 py-1">
<div class="d-flex flex-column justify-content-center">
{{-- <label>{{ $data['fullname'] }}</label> --}}
<h6 class="mb-0 text-sm">{{$data['fullname']}}</h6>
<p class="text-xs text-secondary mb-0">{{$data['gender'] }} ,{{$data['age'] }} वर्ष</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{ $address->province }}</p>
<p class="text-xs text-secondary mb-0">{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data['tole'] }}</p>
</td>
#endforeach

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>

How to get id of slider for get slider_group?

I have two tables sliders and slider_group.
In my controller in create function in laravel, I want the get the id that shows in browser show when I browse it shows like this.
http://localhost:8000/admin/sliders/create/21
blade
<a href="{{ route('admin::sliders.create', ['groupId', $sliderGroup->id]) }}" class="mr-1">
Now how to get the id of slider_group table to group_id of requisitions.
<tbody>
#foreach($sliderGroups as $sliderGroup)
<tr>
<td class="text-truncate">
<i class="la la-dot-circle-o success font-medium-1 mr-1"></i>
{{ $sliderGroup->id }}
</td>
<td class="text-wrap">
{{ $sliderGroup->title }}
</td>
<td class="text-wrap">
{{ getShamsiDate($sliderGroup->created_at) }}
</td>
<td>
<div class="row">
<a href="{{ route('admin::sliders.create', ['groupId', $sliderGroup->id]) }}" class="mr-1">
<i class="la la-plus text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
<a href="{{ route('admin::slider-groups.edit', $sliderGroup->id) }}" class="mr-1">
<i class="ft-edit text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
<form action="{{ route('admin::slider-groups.destroy', $sliderGroup) }}" method="post" #submit.prevent="confirmDelete">
#method('delete')
#csrf
<button type="submit" class="btn btn-default p-0">
<i class="ft-trash-2 text-grey font-medium-5 font-weight-normal"></i>
</button>
</form>
</div>
</td>
</tr>
#endforeach
</tbody>
I get this in address bar like this
http://localhost:8000/admin/sliders/create?groupId&3
It has an error.
404 | Not Found
Controller
public function index(Request $request)
{
if ($request->search) {
$sliderGroups = SliderGroup::search($request->search)->paginate(30);
} else {
$sliderGroups = SliderGroup::paginate(30);
}
if ($sliderGroups->count() == 0 && $request->search ) {
msg()->warning('it is not found.');
}
return view('slider::admin.groups.index', compact('sliderGroups'));
}
web.php
Route::group([
'prefix' => 'admin',
'as' => 'admin::',
], function() {
Route::resource('sliders', 'Admin\SliderController');
Route::resource('slider-groups', 'Admin\SliderGroupController');
});

Undefined variable: posts (View: C:\xampp\htdocs\blog\resources\views\store\search.blade.php)

so i am trying to implement search bar, in the blog i'm working on and when i search it gives me undefined variable post in search.blade.php and i have tried all i can . please help
heres my search.blade file
#extends('layouts.main')
#section('content')
<div class="col-lg-8">
#foreach($posts as $post)
<h2>
{!! $post->title !!}
</h2>
<p class="lead">
by {!! $post->author !!}
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted on {!! $post->created_at !!}</p>
<hr>
{!! Html::image('/img/posts/'. $post->image, $post->title, array('style'=>'width: 675px; height:225px;')) !!}{
<hr>
<p>{!! $post->short_desc !!}</p>
<a class="btn btn-primary" href="/store/view/{{ $post->id }}">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
#endforeach
</div>
#endsection
And the controller function
public function getSearch(Request $request) {
$keyword = $request->input('keyword');
$categories = Category::all();
if( $keyword != ""){
return view('store.search')->with('posts', Post::where('title', 'LIKE', '%'.$keyword.'%')->get())
->with('keyword', $keyword)
->with('categories', $categories);
}
else {
return redirect('/') ;
}
}

Accessing collection from a relationship

So I have 2 tables.
DirtyEvent model and Event model.
I am retrieving DirtyEvent with Event which works fine
In blade I have:
#if (\Request::is('profile/event'))
#foreach ($events as $event)
#if (empty( $event->image ))
<div class="card-header">
<span class="card-title"> {{($event->title) }}</span>
</div>
#else
<div class="card-image">
<img class="img-responsive" src="{{ $event->image }}"></img>
<span class="card-title"> {{($event->title) }}</span>
</div>
#endif
<div class="card-content">
<p><strong>Starts: </strong>#php echo ($startdate) #endphp - {{$event->startime }}</p>
<br>
#if ($startdate != $enddate)
<p><strong>Ends: </strong>#php echo ($enddate) #endphp - {{$event->endtime }}</p>
<br>
#endif
<p><strong>Description:</strong></p>
<br>
<p>{{$event->description }}</p>
</div>
<div class="card-action">
<i class="fa fa-pencil-square-o fa-2x" aria-hidden="true"></i>
<form method="POST" action={{ url('events/delete/' . $event->id) }}>
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="delete" style="border:none;"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></button>
</form>
</div>
#foreach ($events->publicevents as $eventss)
{{dd($events)}}
#endforeach
#endforeach
However dd($events) gives me:
DirtyEvent Collection -> relations -> publicevents -> Event collection
But, It is saying that publicevents do not exist in current collection.
Controller
public function index()
{
$id = Auth::id();
$events = DirtyEvent::where('user_id', $id)
->with('publicevents')
->get();
return view('events.viewEvent', compact('events'));
}
Model
public function publicevents()
{
return $this->hasMany('App\Event', 'event_id');
}
I guess not all dirty event objects have events. So check this first with empty() or isEmpty() or count():
#if (!empty($event->publicevents))
#foreach ($event->publicevents as $eventss)
{{ $eventss->id }}
#endforeach
#endif
Update
It should be $event->publicevents, not $events->publicevetns.

Resources