show last post from each category

前端 未结 3 1660
时光取名叫无心
时光取名叫无心 2020-12-11 10:30

I have two models Post and Category

// migration post

public function up()
{
    Schema::create(\'posts\', function (Blueprint $table) {
        $ta         


        
相关标签:
3条回答
  • 2020-12-11 10:56

    Hiren was close, but you need to go from the category since your post is owned by the category

    $category->posts()->latest()->first();
    

    Alternatively you could work backwards:

    $post = Post::latest()->whereHas('category', function($q) use($category_id) {
        return $q->where('id', $category_id);
    })->first();
    

    For this to work you'll need to define your model relationships:

    Category Model needs this function:

    public function posts() 
    {
        return $this->hasMany(App\Post::class);
    }
    

    Post Model needs this function:

    public function category()
    {
        return $this->belongsTo(App\Category::class);
    }
    

    To respond to Alexey Mezenin, we can just pass a callback to with() to define which posts we want to pull in for each category, performing the correct eager load.

    Category::with(['posts' => function($q) {
        return $q->latest()->first();
    })->get(); 
    
    0 讨论(0)
  • 2020-12-11 11:07

    An Eloquent solution for loading categories with latest post is to create an additional hasOne() relationship in the Category model:

    public function latestPost()
    {
        return $this->hasOne(Post::class)->latest();
    }
    

    And then use eager loading:

    Category::with('latestPost')->get();
    

    This will generate just 2 queries to DB.

    0 讨论(0)
  • 2020-12-11 11:10
    public function up()
    {
        Schema::create('news', function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug')->unique();
            $table->unsignedInteger('author_id');
            $table->unsignedInteger('category_id');
            $table->string('subject');
            $table->text('short');
            $table->text('content');
            $table->integer('view')->default(0);
            $table->integer('status')->default(0);
            $table->string('image');
            $table->timestamps();
    
            $table->foreign('author_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            // $table->foreign('category_id')
            //       ->references('id')->on('categories')
            //       ->onDelete('cascade');
        });
        // Schema::enableForeignKeyConstraints();
    }
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->timestamps();
        });
    }
    

    in contoller:

        $latestpostlist = News::whereIn('created_at',function($query){
                $query->select(DB::raw('max(created_at)'))
                          ->from('news')
                          ->groupBy('category_id');
        })->get();
    

    in your case news will be post. this query worked for me

    0 讨论(0)
提交回复
热议问题