Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JohnnyB's avatar

Trying to get posts from a category id with mysql db

Hi,

I'm new to this community and programming so I hope I'm posting this question in the right section. If not, please move it to the appropriate one.

I'm modifying a laravel script and i'm trying to pull posts from a certain category id and the developer is making it very hard for me to understand how to do it.

I want to list posts with thumbnail, description, date, etc. It's for my home page. I want to get posts only from a certain category number from my db.

Here's my blade.php:

@if($lastFeatures) @include('pages.indexpostloadpage') @endif

My indexpostloadpage:

@foreach($lastFeatures as $k => $item)

    @if( $item->type=='quiz')
        <div class="badge quiz"><div class="badge-img"></div></div>
    @elseif($item->featured_at !== null)
        <div class="badge featured"><div class="badge-img"></div></div>
    @else
        {{  reaction_icon_get($item) }}
    @endif

    <div class="content-timeline--right">
        <div  class="content-timeline__link clearfix">
            <div class="content-timeline__media">
                <figure class="content-timeline__media__image">
                    <a class="clearfix" style="display:block" href="{{ makeposturl($item) }}"   title="{!! $item->title  !!}">
                    <img data-original="{{ makepreview($item->thumb, 's', 'posts') }}" class="lazy" alt="{!! $item->title  !!}" width="262" height="147" style="display: inline;">
                    </a>
                </figure>
            </div>
            <div class="content-timeline__detail">
                <div class="content-timeline__detail__container">

                    <a href="{{ makeposturl($item) }}"   title="{!! $item->title  !!}">
                        <h3 class="content-timeline__detail__title" style="margin-right:10px">{!! $item->title  !!}</h3>
                    </a>

                    <div class="content-timeline__detail--top hide-mobile">
                        <p class="content-timeline__detail__desc">{{ str_limit($item->body, 75) }}</p>
                    </div>

                    <div class="content-timeline__detail--bottom">


                        <div class="content-timeline__detail__date share_counts " >{{ $DB_USER_LANG=="en" ? $item->created_at->format('F d, Y') : $item->created_at->diffForHumans() }}</div>

                    </div>

                </div>
            </div>
          </div>

    </div>
</div>

My IndexController:

public function index() { if(getenvcong('Siteactive')=='no'){ return view('errors.maintenance'); }

    $homepagebuilder=getenvcong('p-homepagebuilder');

    $HomeColSec1Tit1=null; $HomeColSec2Tit1=null; $HomeColSec3Tit1=null;$HomeColSec1Type1=null; $HomeColSec2Type1=null; $HomeColSec3Type1=null;
    if($homepagebuilder=="on"){
        $HomeColSec1Tit1=getenvcong('HomeColSec1Tit1');
        $HomeColSec2Tit1=getenvcong('HomeColSec2Tit1');
        $HomeColSec3Tit1=getenvcong('HomeColSec3Tit1');
        $HomeColSec1Type1=getenvcong('HomeColSec1Type1');
        $HomeColSec2Type1=getenvcong('HomeColSec2Type1');
        $HomeColSec3Type1=getenvcong('HomeColSec3Type1');
    }
       //set default
      if($HomeColSec1Type1==null){ $HomeColSec1Type1=config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec1Type1') !== null ? config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec1Type1') : '["list", "quiz"]';}
      if($HomeColSec2Type1==null){ $HomeColSec2Type1=config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec2Type1') !== null ? config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec2Type1') : '["news"]';}
      if($HomeColSec3Type1==null){ $HomeColSec3Type1=config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec3Type1') !== null ? config('buzzytheme_'.getenvcong('CurrentTheme').'.HomeColSec3Type1') : '["video"]';}

    //colums 1
    $lastFeatures=        Posts::forhome()->typesAccepted($HomeColSec1Type1)->typesActivete()->approve('yes')->latest("published_at")->paginate(10);

    //colums 2
    $lastNews =           Posts::forhome()->typesAccepted($HomeColSec1Type1)->typesActivete()->approve('yes')->latest("published_at")->paginate(config('buzzytheme_'.getenvcong('CurrentTheme').'.homepage_news_limit'));

    //colums 3
    $lastTrendingVideos = Posts::forhome()->typesAccepted($HomeColSec1Type1)->typesActivete()->approve('yes')->latest("published_at")->take(10)->get();

    $lastFeaturestop = Posts::forhome('Features')->typesActivete()->approve('yes')->where("featured_at", '>', '')->latest("featured_at")->take(10)->get();

    $lastvideoscol1  = Posts::forhome()->byType('video')->typesActivete()->approve('yes')->getStats('one_day_stats', 'DESC')->paginate(3);

    $lastpoll        = Posts::forhome()->byType('poll')->typesActivete()->approve('yes')->latest("published_at")->paginate(2);

    $lastTrending    = Posts::forhome()->typesActivete()->approve('yes')->getStats('one_day_stats', 'DESC', 10)->get();


    if(\Request::query('page')){

        if(\Request::ajax()){

            if(\Request::query("timeline")=="right"){

            return view('pages.indexrightpostloadpage', compact('lastNews'));

            }else{

            return view('pages.indexpostloadpage', compact('lastFeatures', 'lastvideoscol1', 'lastpoll'));

            }

        }else{
            return redirect('/');
        }

    }else{

        if(Posts::count() < 1){

          return view('errors.starting');

        }
    }


    return view('pages.index', compact('lastFeaturestop', 'lastFeatures', 'lastvideoscol1', 'lastpoll', 'lastNews','lastNewsVideos', 'lastTrending', 'lastTrendingVideos', 'HomeColSec1Tit1', 'HomeColSec2Tit1', 'HomeColSec3Tit1'));
}

My DB tables look something like this:

posts -> categories -> ["2,13,"]

The 2 being the parent category and the 13 being the sub-category.

I'm not sure if I need to add anything else but that's the only pages I think needed to me modified to obtain my query. I've been playing around with the queries in IndexController.php and could not get anything to work.

Any help would be appreciated.

Thank you!

0 likes
7 replies
JohnnyB's avatar

Hi,

I'm not sure where i'm suppose to use the code you gave me? Is this suppose to go in my blade.php or in one of these?

//colums 1 $lastFeatures= Posts::forhome()->typesAccepted($HomeColSec1Type1)->typesActivete()->approve('yes')->latest("published_at")->paginate(10);

Because if I understand correctly, IndexController is used for my index.php, aka homepage right? So I think I would need to change one of the queries in IndexController to meet my requirements and then call it in my blade.php template?

Thanks for the help!

JohnnyB's avatar

Ok so I added this to the top of IndexController.php:

namespace App\Http\Controllers;

use App\Posts; use App\Http\Requests; $category = \App\Categories::find($id)->$categories->posts();

class IndexController extends Controller

but i'm still getting this error:

ErrorException in IndexController.php line 7: Undefined variable: id

aurawindsurfing's avatar

Error says exactly what it is - undefined variable $id. The controller does not know what it is.

The answer is actually in your question:

" i'm trying to pull posts from a certain category id"

so $id = id of your category,

just declare it beforehand in your controller method.

JohnnyB's avatar

Yeah I don't think I understand this enough to make the changes myself. I'll try to get someone to look over my code instead. Thanks anyway!

aurawindsurfing's avatar

@JohnnyB it is easier then you think. First problem will take you a week, second 3 days. You will be flying in no time just keep asking and trying things and you will get there!

1 like

Please or to participate in this conversation.