I want to add pagination to my searching product item view using my show controller
This is my controller
public function show($id)
{
$categories = Item::all();
$products = Item::find($id)->products->paginate(3);
return view('category.index', compact('categories', 'products'));
}
This is my view
#extends('layout.front')
#section('page')
<div class="row" id="portfolio">
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-3" style="margin-top:20px;">
<nav class="main-nav">
<ul class="main-nav-ul">
<li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li>
<li>Computer<span></span>
<ul>
#if(!empty($categories))
#forelse($categories as $category)
<li>{{ $category->name }}</li>
#empty
<li>No data found</li>
#endforelse
#endif
</ul>
</li>
<li>CCTV</li>
<li>Gaming</li>
</ul>
</nav>
</div>
<!-- /.col-lg-3 -->
<div class="col-lg-9">
<div class="row" style="margin-top:20px;">
#if(!empty($products))
#foreach($products as $key=>$product)
<div class="col-md-4 col-sm-6 portfolio-item" id="items">
<div class="card h-100">
<img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image">
<div class="card-body">
<h4 class="card-title">
{{ $product->category_name }}<br />{{ $product->item_name}}
</h4>
<p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
<h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>
{{--#endforelse--}}
</div>
</div>
</div>
#endforeach
#endif
</div>
<!-- /.row -->
</div>
<!-- /.col-lg-9 -->
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<div class="col-lg-12">{!! $products->links() !!}</div>
#endsection
i used laravel default pagination and customize my bootstrap pagination to laravel.
But in my show function in controller pagination not working. how to fix searching products pagination.
i used links method. but it's not working for this view.
<div class="col-lg-12">{!! $comproducts->links() !!}</div>
You are not using pagination anywhere in the code. Please check the documentation on how to add pagination. You need to use paginate function when making the db call, and then use paginator links.
https://laravel.com/docs/5.5/pagination
{{$products->render()}}
put it below the end of the foreach loop.
Hi Please find pagination code here
Table:stocks PK:id,Varchar:name
StockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Controllers\Controller as Controller;
class StockController extends Controller {
public function usaPage() {
$stocks = DB::table('stocks')->orderBy('name', 'ASC')->paginate(50);
return view('stock.usa', ['stocks' => $stocks]);
}
?>
usa.blade.php
#section('content')
<div class="row">
<div class="col-md-12">
<table class="table">
<tr>
<th scope="col">Company Name</th>
<th scope="col">ID</th>
</tr>
#foreach ($stocks as $stock)
<tr><td> {{ $stock->name }}</a></td><td> {{ $stock->id }}</td></tr>
#endforeach
</table>
</div>
</div>
#endsection
Related
I have created a table called jeniskerja which contains the category (id's range from 1-5) of the pekerjaan's table. I already made the eloquent model for each of the said table;
jeniskerja (model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Jeniskerja extends Eloquent
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function pekerjaans(){
return $this->hasMany(Pekerjaan::class);
}
}
pekerjaan (model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Pekerjaan extends Eloquent
{
use HasFactory;
protected $guarded = [];
protected $dates = ['tanggal'];
public function penyedia(){
return $this->belongsTo(Penyedia::class, 'penyedia_id');
}
public function jeniskerja(){
return $this->belongsTo(Jeniskerja::class, 'jeniskerja_id');
}
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
}
At the moment, when the user click on the vendor's name, it will direct them to show a detail of the vendor and the table list of work... What I need is to show the table based on the category id when the user click on the said category tab... Here is the picture to show the page
And here is my code to view the page:
<section class="content">
<div class="container-fluid">
<div class="card card-default color-palette-box">
<div class="card-header">
<h3 class="card-title">
<b>{{$penyedia->nama}}</b>
</h3>
</div>
<div class="card-body">
<div class="col-12">
<h5><i class="fas fa-id-card"></i>  NPWP: {{$penyedia->npwp}}</h5><br>
<h5><i class="fas fa-map-pin"></i>  Alamat: {{$penyedia->alamat}}</h5>
</div>
</div>
<hr class="solid" style="border-top: 1px solid;">
<div class="card-body">
<div class="col-12">
<h5><i class="fas fa-building"></i>  Bentuk Usaha: {{$penyedia->bentuk_usaha}}</h5><br>
<h5><i class="fas fa-envelope"></i>  Email: {{$penyedia->email}}</h5>
</div>
</div>
</div>
</div>
</section>
<section class="content">
<div class="container-fluid">
<!-- Small boxes (Stat box) -->
#foreach ($penyedia as $penyedias)
#endforeach
<div class="row">
{{-- <div class="col-lg-4 col-6">
<!-- small card -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{$penyedia->count('nama')}}</h3>
<p>Penyedia</p>
</div>
<div class="icon">
<i class="fas fa-store"></i>
</div>
<a href="/datapenyedia" class="small-box-footer">
More info <i class="fas fa-arrow-circle-right"></i>
</a>
</div>
</div> --}}
<!-- ./col -->
<div class="col-lg-6 col-8">
<!-- small box -->
<div class="small-box bg-lightblue">
<div class="inner">
<h3>{{$pekerjaan->count('pekerjaan')}}</h3>
<p>Pekerjaan</p>
</div>
<div class="icon">
<i class="fas fa-map"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
<div class="col-lg-6 col-8">
<!-- small box -->
<div class="small-box bg-lightblue">
<div class="inner">
<h3>Rp. {{number_format($pekerjaan->sum('nilai_kontrak'))}}</h3>
<p>Nilai Kontrak</p>
</div>
<div class="icon">
<i class="fas fa-money-bill-wave"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
</div>
</section>
#foreach ($jeniskerja as $jeniskerjas)
<section class="content">
<div class="container-fluid">
<div class="card card-default collapsed-card">
<div class="card-header collapsed-card">
<h3 class="card-title">
<b>{{$jeniskerjas->nama_jenis}}</b>
</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fas fa-plus"></i>
</button>
</div>
</div>
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px" rowspan="2">Tahun Anggaran</th>
<th rowspan="2">Tanggal Kontrak</th>
<th rowspan="2">Paket Pekerjaan</th>
<th rowspan="2">Nama Perusahaan</th>
<th rowspan="2">Lokasi Pekerjaan</th>
<th rowspan="2">Jenis Pekerjaan</th>
<th rowspan="2">HPS</th>
<th rowspan="2">Nilai</th>
<th rowspan="2">Dokumen</th>
<th colspan="2">Tanggal</th>
</tr>
<tr>
<td>Tgl Buat</td>
<td>Tgl Update</td>
</tr>
</thead>
<tbody>
#php $no = 1; /*$totalNilai = 0.0;*/ #endphp
#foreach ($pekerjaan as $pekerjaans)
<tr>
<td>{{$pekerjaans->tanggal->format('Y')}}</td>
<td>{{$pekerjaans->tanggal->format('d/m/Y')}}</td>
<td>{{$pekerjaans->pekerjaan}}</td>
<td>{{$pekerjaans->penyedia->nama}}</td>
<td>{{$pekerjaans->lokasi}}</td>
<td>{{$pekerjaans->jeniskerja->nama_jenis}}</td>
<td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
#php
$pekerjaans->nilai_total = $pekerjaans->nilai_1 + $pekerjaans->nilai_2 + $pekerjaans->nilai_3 + $pekerjaans->nilai_4;
#endphp
<td>{{$pekerjaans->nilai_total}}</td>
<td><u>{{$pekerjaans->status}}</u></td>
<td>
</td>
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</section>
#endforeach
And here is the AdminController file
public function showpenyedia(Penyedia $penyedia){
$pekerjaan = $penyedia->pekerjaans; //show pekerjaan(work) based on penyedia(vendor)
$jeniskerja = Jeniskerja::all();
return view('admin.showpenyedia', compact('penyedia','pekerjaan','jeniskerja'));
}
I am doing a live search on livewire, my search term is already binded with the livewire controller, but when I add it to the where clause in render function, it still displays all the data on the table not the one i am searching. I tried replacing the searchterm with a string as keyword in LIKE and it works. It seems that i can't use the input in searchterm as a keyword in the where clause.
here is my controller;
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Item;
use Livewire\WithPagination;
class Items extends Component
{
use WithPagination;
public $searchTerm;
public function render()
{
$select = Item::where('itemName', 'LIKE', '%' . $this->searchTerm . '%')->paginate(6);
return view('livewire.items', ['items' => $select]);
}
}
this is my view
<div class="row browse mt-6">
<div class="col float-left category ">
<h4 class="float-left font-weight-bold">All Categories</h4>
<h4 class="float-left font-weight-bold">Men</h4>
<h4 class="float-left font-weight-bold">Women</h4>
</div>
<div class="col search-cat float-right">
<input type="text" id="search" placeholder="Browse Products" wire:model="searchTerm"/>
</div>
{{$searchTerm}}
</div>
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
<table class="table-center">
<tbody>
<div>
#foreach($items as $item)
<tr class="mx-auto">
<div class="d-inline w-30 item-desc mx-auto m-1">
<h4 class="pt-1">{{ $item->itemName }}</h4>
<div class="">
<img src="items/{{ $item->itemPic }}.png" alt="" class="item-pic">
</div>
<div class="row">
<div class="col d-inline float-left">
Price: US ${{ $item->itemPrice }}
</div>
<div class="col d-inline float-right">
{{ $item->itemCOD }}</td>
</div>
</div>
</div>
</tr>
</tbody>
#endforeach
</div>
<hr style="border-top: dotted 1px;" />
<div class="pagination-content">
{{ $items->links('pagination::pagination-custom') }}
</div>
<hr style="border-top: dotted 1px;" />
</table>
</div>
There's two things here that could potentially cause problems,
Your tr element does not have wire:key, the value to wire:key must be unique across the entire page
Your view consists of multiple root elements (if this is your entire view), these are the three root-elements in your view - and in Livewire, its very important that the view only has one root element.
<div class="row browse mt-6">
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
Also, as a sidenote, your loop also closes the tbody tag, which is odd, so I moved that out of the loop. It also doesn't quite make sense to put divs and hrs inside the table tag, those should probably be outside of the tabletag.
Here's the updated view, with wire:key on the tr and with the whole view wrapped in one div,
<div>
<div class="row browse mt-6">
<div class="col float-left category ">
<h4 class="float-left font-weight-bold">All Categories</h4>
<h4 class="float-left font-weight-bold">Men</h4>
<h4 class="float-left font-weight-bold">Women</h4>
</div>
<div class="col search-cat float-right">
<input type="text" id="search" placeholder="Browse Products" wire:model="searchTerm"/>
</div>
{{$searchTerm}}
</div>
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
<table class="table-center">
<tbody>
<div>
#foreach($items as $item)
<tr class="mx-auto" wire:key="item-{{ $item->id }}">
<div class="d-inline w-30 item-desc mx-auto m-1">
<h4 class="pt-1">{{ $item->itemName }}</h4>
<div class="">
<img src="items/{{ $item->itemPic }}.png" alt="" class="item-pic">
</div>
<div class="row">
<div class="col d-inline float-left">
Price: US ${{ $item->itemPrice }}
</div>
<div class="col d-inline float-right">
{{ $item->itemCOD }}</td>
</div>
</div>
</div>
</tr>
#endforeach
</div>
</tbody>
<hr style="border-top: dotted 1px;" />
<div class="pagination-content">
{{ $items->links('pagination::pagination-custom') }}
</div>
<hr style="border-top: dotted 1px;" />
</table>
</div>
</div>
Have a look at this resource from the official Livewire docs,
https://laravel-livewire.com/docs/2.x/troubleshooting#dom-diffing-issues
Maybe because you not render component
Try add <livewire:name-component /> in parent view like this
<div class>
<livewire:name-component />
</div>
I have this:
#foreach($portfolios as $portfolio)
<div id="item-portfolio-expand{{$portfolio->id}}" class="item-portfolio-expand expand">
<div class="container">
<div class="row">
<div class="col-md-8">
col-md-8
</div>
<div class="col-md-4">
{{ __('Name')}}: {{ $portfolio->name }}
</div>
</div>
</div>
</div>
#endforeach
And this:
#foreach ($portfolios as $portfolio)
<div class="portfolio-item {{ $portfolio->tag }} " data-category="transition">
<img src="{{ $portfolio->image_show }}">
<a href="#item-portfolio-expand{{$portfolio->id}}" data-toggle="collapse" data-target="#item-portfolio-expand{{$portfolio->id}}" data-parent="#item-portfolio-expand{{$portfolio->id}}">
<div class="portfolio-item-overlay">
<p class="name-item-overlay">{{ $portfolio->name }}</p>
</div>
</a>
</div>
#endforeach
And when I press the in the first foreach, I want to toggle the second foreach at <div id = "item-portfolio-expand {{$ portfolio-> id}}" class = "item- portfolio-expand expand "> I just don't succeed at anything, I use bootstrap and jquery in the project. .item-portfolio-expand has display: none;
And at the same time when one is open and another is selected, to close the one already open and to appear the one selected with toggle.
I have an articles page that displays a loop of every article in my website. THen I have a single post page which displays the article itself. I am trying to modify the post page so that recent posts will display in a loop on the sidebar. I was trying to copy the loop code from the articles page on to my post page but I got an error.
Articles page code (no errors):
<div class="row col-md-12">
#if(isset($_REQUEST['searchItem']) && $_REQUEST['searchItem']<>'')
#if(isset($msg))
<div class="alert alert-danger">
<ul>
<li><h3>{{ $msg }}</h3></li>
</ul>
</div>
#else
#foreach($result as $result)
<div class="col-md-4">
<div class="content">
{{substr(($result->title),0,88)}}..
<img style="padding: 5px; width:100%; height:218px;" src="{{asset('thumbnails/'.$result->thumbnail)}}" class="img-responsive">
</div>
</div>
#endforeach
#endif
#else
#foreach($post as $post)
<div class="col-md-4">
<div class="content">
{{substr(($post->title),0,88)}}..
<img style="padding: 5px; width:100%; height:218px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="img-responsive">
</div>
</div>
#endforeach
#endif
</div>
Code for the single post page:
<div class="container layer1">
#if(isset($_REQUEST['searchItem']) && $_REQUEST['searchItem']<>'')
#if(isset($msg))
<div class="alert alert-danger">
<ul>
<li><h3>{{ $msg }}</h3></li>
</ul>
</div>
#else
<div class="row" style="width:25%;float:left;padding-left:5px;">
<div class="post-content">
<div class="post-container">
<h1>{{ $post->title }}</h1>
<p style="color:#888;">{{ date ('M-d-Y',strtotime($post->created_at)) }}</p>
<!-- <p><img src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"></p> -->
<p><?php echo html_entity_decode($post->content); ?></p>
</div>
</div>
</div>
<div class="row" style="width:25%;float:left;padding-left:5px;">
<div class="post-content">
<div class="post-container">
<div class="container articles-page">
<div class="articles-title">
</div>
<div class="container">
<h3>Recent Posts</h3>
#foreach($result as $result)
<div class="col-md-4">
<div class="content">
{{substr(($result->title),0,88)}}..
<img style="padding: 5px; width:100%; height:218px;" src="{{asset('thumbnails/'.$result->thumbnail)}}" class="img-responsive">
</div>
</div>
#endforeach
#endif
#else
#foreach($post as $post)
error is here => <p><img style="width:100px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"><br>
{{substr(($post->title),0,88)}}..</p>
</tr>
#endforeach
#endif
<div class="row col-md-12">
<div class="panel-body">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The error is on this line:
<p><img style="width:100px;" src="{{asset('thumbnails/'.$post->thumbnail)}}" class="responsive"><br>
Code in controller file:
//view post
public function viewpost($url){
$url ='articles/'.$url;
if(count(posts::where('url', '=', $url)->first())<>1){
return redirect()->back();
}
return view('viewpost')
->with('post', posts::where('url', '=', $url)->first());
}
//all posts route
public function allposts(){
$post=posts::All();
if(count($post)==0){
return redirect()->back()->with('message', 'There are no posts at this moment');
}
else{
return view ('admin-SchoolDir.allposts',array('post' => $post))->with("title","Admin-Allposts");
}
}
The post defined in the viewpost method is an object while you want to use it as an array in your view .
Your code must be like this
public function viewpost($url){
$url ='articles/'.$url;
if(count(posts::where('url', '=', $url)->first())<>1){
return redirect()->back();
}
return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
}
And in your view change
#foreach($post as $post)
to
#foreach($posts as $post)
Edit :
You dont need to use count over single object to check for existence your code should be like this
public function viewpost($url){
$url ='articles/'.$url;
if(!posts::where('url', '=', $url)->first()){
return redirect()->back();
}
return view('viewpost')->with(['post'=>posts::where('url', '=', $url)->first(),'posts'=>posts::where('url', '=', $url)->get()]);
}
I'm getting this error message for my code:
Undefined variable: questions (View:
home/xxxx/PhpstormProjects/xxxxx/resources/views/home.blade.php)
Please, try to shed some light on what could be wrong.
I have provided my QuestionController & question template. Not sure, what my code is missing. Here is my code:
QuestionController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;
class QuestionController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(Question $question)
{
return view('question')->with('question', $question);
}
question.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
Edit Question
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary `float-left"`
href="#">
Answer Question
</a></div>
<div class="card-body">
#forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}`</div>`
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
View
</a>
</div>
</div>
#empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
#endforelse
</div>
</div>
</div>
#endsection
home.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Questions
<div class="card-body">
<div class="card-deck">
#foreach($questions as $question)
<div class="col-sm-4 d-flex align-items-stretch">
<div class="card mb-3 ">
<div class="card-header">
<small class="text-muted">
Updated: {{ $question->created_at->diffForHumans() }}
Answers: {{ $question->answers()->count() }}
</small>
</div>
<div class="card-body">
<p class="card-text">{{$question->body}}</p>
</div>
<div class="card-footer">
<p class="card-text">
<a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
View
</a>
</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
<div class="card-footer">
<div class="float-right">
{{ $questions->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The first think that comes to my mind is that you are giving a variable named question to your view in your controller and you are trying to use $questions in your foreach when it must be $question as $q
The other thing is that you may want to pass all Questions rows you have to you view, it can be accomplished (this may not be the best solution) by editing your AppServiceProvider.php
You have to add this.
use View;
use App\Question;
public function boot()
{
//
View::share('questions',Question::all());
}
But if you do this, it will share the rows obtained with all your views.
hope this helps.