请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

模型关联增加条件

老师,有这么一个需求:
获取所有栏目下面的信息,这些信息需要按照修改时间排序,然后limit 4,请问如何获取。
目前的代码:
Category 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;

class Category extends Model
{

    use ModelTree, AdminBuilder;

    protected $table = 'admin_categories';


    public function info() {
        return $this->hasMany(Info::class, 'category_id');
    }
}

Info 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Info extends Model
{
	protected $table = 'admin_infos';


	public function category() {
		return $this->belongsTo(Category::class, 'category_id');

	}
}

这个问题其实和教程中,我想获取所有文章下的评论,每个文章4条评论,每个评论按照修改时间获取。一样一样的,通过关联模型如何获取呢?

正在回答 回答被采纳积分+3

1回答

提问者 AMOR跳跳 2018-11-27 23:14:49
# 关于这个问题,我有个一个解答 (不知道是否还有更好的方法)
# 这是路由:
Route::get('category/infos', function(\App\Models\Category $category, \App\Models\Info $info){
   return $category::all()->each(function($item, $key) {
       $item->apiInfo->toArray();
   });
});

# 这个Category 模型
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;

class Category extends Model
{

    use ModelTree, AdminBuilder;

    protected $table = 'admin_categories';


    public function apiInfo(string $where = null, int $is_limit = null) {
        return $this->hasMany(Info::class, 'category_id')->limit(4)->orderByDesc('updated_at');
//        return $this->hasMany(Info::class, 'category_id');
    }

    // 用作数据填充的关联关系方法
    public function info_f() {
        return $this -> hasMany(Info::class, 'category_id');
    }

}

# 这是返回的JSON
[
    {
        "id":1,
        "parent_id":0,
        "order":0,
        "title":"pcsoft",
        "classpath":"pcsoft",
        "created_at":"2018-11-27 14:36:32",
        "updated_at":"2018-11-27 14:36:32",
        "api_info":Array[4]
    },
    {
        "id":2,
        "parent_id":1,
        "order":0,
        "title":"work",
        "classpath":"work",
        "created_at":"2018-11-27 14:47:54",
        "updated_at":"2018-11-27 14:47:54",
        "api_info":Array[4]
    },
    {
        "id":3,
        "parent_id":0,
        "order":0,
        "title":"pcgame",
        "classpath":"pcgame",
        "created_at":"2018-11-27 14:48:05",
        "updated_at":"2018-11-27 14:48:05",
        "api_info":Array[4]
    },
    {
        "id":4,
        "parent_id":0,
        "order":0,
        "title":"iossoft",
        "classpath":"iossoft",
        "created_at":"2018-11-27 14:48:22",
        "updated_at":"2018-11-27 14:48:22",
        "api_info":Array[4]
    },
    {
        "id":5,
        "parent_id":0,
        "order":0,
        "title":"iosgame",
        "classpath":"iosgame",
        "created_at":"2018-11-27 14:48:32",
        "updated_at":"2018-11-27 14:48:32",
        "api_info":Array[4]
    },
    {
        "id":6,
        "parent_id":3,
        "order":0,
        "title":"selfpc",
        "classpath":"selfpc",
        "created_at":"2018-11-27 14:48:52",
        "updated_at":"2018-11-27 14:48:52",
        "api_info":[
            {
                "id":128,
                "title":"Ms.",
                "info_url":"http://pfeffer.com/",
                "image_url":"https://lorempixel.com/640/480/?35123",
                "category_id":6,
                "created_at":"2005-06-26 14:23:56",
                "updated_at":"2016-06-27 22:56:55"
            },
            {
                "id":163,
                "title":"Prof.",
                "info_url":"http://www.herzog.info/",
                "image_url":"https://lorempixel.com/640/480/?88262",
                "category_id":6,
                "created_at":"2010-07-15 00:04:26",
                "updated_at":"2015-03-15 01:15:52"
            },
            {
                "id":198,
                "title":"Prof.",
                "info_url":"http://kohler.com/ad-autem-laboriosam-quo.html",
                "image_url":"https://lorempixel.com/640/480/?93485",
                "category_id":6,
                "created_at":"1980-08-23 08:08:37",
                "updated_at":"2013-07-19 14:34:24"
            },
            {
                "id":18,
                "title":"Prof.",
                "info_url":"https://reilly.com/fuga-consequatur-sit-sequi-quae-odio-distinctio-numquam.html",
                "image_url":"https://lorempixel.com/640/480/?94247",
                "category_id":6,
                "created_at":"2011-02-02 01:48:33",
                "updated_at":"2013-03-12 09:51:44"
            }
        ]
    },
    {
        "id":7,
        "parent_id":4,
        "order":0,
        "title":"wechat",
        "classpath":"wecaht",
        "created_at":"2018-11-27 14:49:09",
        "updated_at":"2018-11-27 14:49:09",
        "api_info":[
            {
                "id":103,
                "title":"Mrs.",
                "info_url":"http://www.ward.org/",
                "image_url":"https://lorempixel.com/640/480/?41503",
                "category_id":7,
                "created_at":"2002-05-08 09:40:09",
                "updated_at":"2015-05-29 22:29:09"
            },
            {
                "id":173,
                "title":"Prof.",
                "info_url":"http://nicolas.com/ea-labore-non-animi-aut-totam-dicta-reprehenderit",
                "image_url":"https://lorempixel.com/640/480/?99170",
                "category_id":7,
                "created_at":"1983-12-06 13:09:28",
                "updated_at":"2014-03-24 04:23:00"
            },
            {
                "id":42,
                "title":"Dr.",
                "info_url":"http://www.hirthe.info/",
                "image_url":"https://lorempixel.com/640/480/?50939",
                "category_id":7,
                "created_at":"1999-05-21 08:20:05",
                "updated_at":"2014-02-15 16:39:58"
            },
            {
                "id":120,
                "title":"Mr.",
                "info_url":"http://www.schinner.com/at-occaecati-eveniet-aut-ut-aspernatur-ratione",
                "image_url":"https://lorempixel.com/640/480/?79172",
                "category_id":7,
                "created_at":"1985-04-27 16:17:02",
                "updated_at":"2013-05-22 17:06:40"
            }
        ]
    }
]


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信