Postingan lainnya
Cara membuat pagination pada relasi tabel laravel?
Permisi kaka, saya sudah berhasil menampilkan data dari dua tabel, tabel momenta dan posts yang terhubung pada tabel categories sesuai id-nya. permasalahnya, jika category mempunyai banyak post bagaimana cara buat pagination-nya kak? Mohon bantuannya kak. Terimakasih :)
17 Jawaban:
Controller-nya
$user = \App\User::paginate(10) // misal 10 data per page
View-nya
{{ $user->links() }} // tinggal masukan script ini pada view
di controller artikelnya sudah saya paginate kak, tapi gak bisa kak. soalnya tabel artikel sama alamat mempunyai user_id kak.
model momentum
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Momentum extends Model
{
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function momentums()
{
return $this->hasMany('App\Models\Momentum');
}
}
model post
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function posts()
{
return $this->hasMany('App\Models\Post');
}
}
model category
namespace App|Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
public function momentums()
{
return $this->hasMany('App\Models\Momentum');
}
public function posts()
{
return $this->hasMany('App\Models\Post');
}
}
category controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Momentum;
use App\Models\Category;
use App\Models\Post;
class CategoryController extends Controller
{
public function index($name)
{
$category= Category::where('name', $name)->first();
return view('category.index', compact('category'));
}
}
disini saya menampilkan isi moment dan post sesuai categorynya kak.
ini view category index kak
@foreach ($category->posts as $post)
<div class="card category-card">
<div class="row">
<div class="col-md-7">
<img class="img-fluid category-img" src="{{ asset('images/' . $post->image) }}" alt="" style="">
</div>
<div class="col-md-5 category-body">
<h4 class="category-h4"><a href="{{ url('blog/' . $post->slug) }}">{{ $post->title }}</a></h4>
<p class="category-p">{!! substr(strip_tags($post->body),0,80) !!} {{ strlen($post->body) ? '__' : '' }}</p>
<small class="category-small" >{{ date('F d, Y', strtotime($post->created_at)) }}</small>
</div>
</div>
</div>
@endforeach
route
Route::get('category/{name?}', ['as' => 'category.index','uses' => 'CategoryController@index']);
gak ada error kak. kalo mau ditambah paginate-nya bagaimana kak?
Coba kaya gini
CategoryController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Momentum;
use App\Models\Category;
use App\Models\Post;
class CategoryController extends Controller
{
public function index($name)
{
$category= Category::where('name', $name)->first()->paginate(10);
return view('category.index', compact('category'));
}
}
Category Index
<div class="row">
<div class="col-md-6">
@foreach ($category->posts as $post)
<div class="card z-depth-0" style="border-radius: 0px; border: 1.5px solid #E5E5E5; padding: 5px; margin-top: 10px;">
<img class="card-img-top img-fluid" src="{{ asset('images/' . $post->image) }}" alt="Card image cap">
<p><a href="{{ url('category/' . $post->category->name) }}" style="font-variant: small-caps;"><small>{{ ucwords($post->category->name) }}</small></a></p>
<div class="card-body">
<h5 class="card-title" style="margin-top: -33px; line-height: 30px; font-weight: bold;"><a href="{{ url('moment/' . $post->slug) }}">{{ $post->title }}</a></h5>
<p class="card-text">{!! substr(strip_tags($post->body),0,100) !!} {{ strlen($post->body) ? '__' : '' }}</p>
<small><i class="fa fa-calendar" aria-hidden="true"></i> {{ $post->created_at }}</small><hr>
</div>
</div>
@endforeach
@foreach ($category->momentums as $moment)<hr>
<div class="card z-depth-0" style="border-radius: 0px; border: 1.5px solid #E5E5E5; padding: 5px; margin-top: 10px;">
<img class="card-img-top img-fluid" src="{{ asset('images/' . $moment->image) }}" alt="Card image cap">
<p><a href="{{ url('category/' . $moment->category->name) }}" style="font-variant: small-caps;"><small>{{ ucwords($moment->category->name) }}</small></a></p>
<div class="card-body">
<h5 class="card-title" style="margin-top: -33px; line-height: 30px; font-weight: bold;"><a href="{{ url('moment/' . $moment->slug) }}">{{ $post->title }}</a></h5>
<p class="card-text">{!! substr(strip_tags($moment->body),0,100) !!} {{ strlen($moment->body) ? '__' : '' }}</p>
<small><i class="fa fa-calendar" aria-hidden="true"></i> {{ $moment->created_at }}</small>
</div>
</div>
@endforeach
</div>
</div>
{{ $category->links() }}
tampil error seperti ini kak "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name (View: /var/www/html/metoo/resources/views/category/index.blade.php)"
Coba Controller-nya gini dulu
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Momentum;
use App\Models\Category;
use App\Models\Post;
class CategoryController extends Controller
{
public function index()
{
$category= Category::where('name')->paginate(10);
return view('category.index', compact('category'));
}
}
Terus diroute yang {name?} dihapus dulu.
tetap gak bisa kak. vaariable $name itu untuk akses parameter kak
Setau ane kalo pagination itu penggunaanya seperti ini
<?php
namespace App\Http\Controllers;
use App\User;
class UserController extends Controller
{
public function index()
{
$users = User::paginate(15);
return view('user.index', ['users' => $users]);
}
}
soalnya saya menampilkannya berdasarkan categorynya kak. Terus bagaimana solusinya ya kak? padahal website saya dikit lagi mau jadi :(
dicoba coba aja, eksperiment sendiri. ane soalnya pake pagination cuma di page home, belum sampe kesana.