v2.5.2
Giriş yap

Laravelde yorum sayısını anasayfada yazdırmak

goxaria
351 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Merhaba blog tarzı bir şey kodlamaya çalışıyorum.

Post ve comments işlemini yaptım ancak commentsdeki belli ide göre olan satır sayısını anasayfadaki loopun içerisinde nasıl yazdırırım biraz kafam karıştı...

Ufak bir örnek gösterebilecek olan var mı acaba? Şimdiden teşekkürler...

Cevap yaz
Cevaplar (4)
goxaria
834 gün önce
$comments = Post::select('posts.*', 'users.name', 'comments.*')
    ->join('comments', 'comments.post_id', 'posts.id')
    ->join('users', 'users.id', '=', 'comments.user_id')
    ->orderBy('comments.created_at', 'DESC')
    ->withCount('comments')
    ->get();

Bu şekilde yapabildim çalıştı görünüyor umarım yanlış bir şey yapmamışımdır sonucu buraya ekleyeyim dedim.

goxaria
834 gün önce
$comments = Post::select('posts.*', 'users.name', 'comments.*')
    ->join('comments', 'comments.post_id', 'posts.id')
    ->join('users', 'users.id', '=', 'comments.user_id')
    ->selectRaw('posts.*, count(comments.post_id) as commentcount')
    ->groupBy('posts.id')
    ->orderBy('comments.created_at', 'DESC')
    ->get();

Son olarak bu şekilde çalıştırdım ancak grupladığım için tüm gönderileri post edemiyorum sadece sonuncusunu gösterebiliyorum... Ancak tüm sonuçları göstermem gerekiyor...

goxaria
834 gün önce

@m100 hocam merhaba,

$comments = Comment::select('comments.*', 'posts.title', 'users.name')
    ->join('posts', 'posts.id', 'comments.post_id')
    ->join('users', 'users.id', '=', 'comments.user_id')
    ->orderBy('comments.created_at', 'DESC')
    ->get();

benim yapım böyle comments tablosundaki satırları posts.idye göre çekip yazdırmam gerekiyor ne yaptıysam başaramadım...

m100
835 gün önce

query builderi kullanarak şu şekilde yapabilirsin:

 $Array = DB::table('Tablo adı')->Where('kullanıcıid stun adı', $kullanıcıidsi)->count();