zoukankan      html  css  js  c++  java
  • 【渴求式加载】laravel中with()的使用(关联)

    测试了好半天才跑通,记录下自己的例子,以便查询使用:

    【Model】原模型 文章表 belongsTo 分类关系表

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateSupportFacadesDB;
    
    class Articles extends Model
    {
        protected $table = 'articles';
    
        public static function getValue($id)
        {
            return self::find($id)->toArray();
        }
    
        public function a_to_c()
        {
            return $this->hasOne('AppModelsClassificationArticles', 'article_id', 'id');
        }
    
        public function a_to_c2()
        {
            return $this->belongsTo('AppModelsClassificationArticles', 'id', 'article_id');
        }
    
        public function a_to_c3()
        {
            return $this->hasMany('AppModelsClassificationArticles', 'article_id', 'id');
        }
    
        /**
         * 返回侧边栏最新文章列表
         * @return mixed
         */
        public static function getNewList($num)
        {
            return self::select('*') -> join('classification_to_articles', 'classification_to_articles.article_id', '=', 'articles.id') -> orderBy('articles.created_at', 'desc') -> take($num) -> get() -> toArray();
        }
    
        /**
         * 返回侧边栏随机推荐文章列表
         * @param $num
         * @return IlluminateDatabaseEloquentBuilder[]|IlluminateDatabaseEloquentCollection
         */
        public static function getRandomList($num)
        {
            return self::with('a_to_c2') -> select('*') -> orderBy(DB::raw('RAND()')) -> take($num) -> get();
        }
    }

    关键语句:

        public function a_to_c2()
        {
            return $this->belongsTo('AppModelsClassificationArticles', 'id', 'article_id');
        }
    
    
        /**
         * 返回侧边栏随机推荐文章列表
         * @param $num
         * @return IlluminateDatabaseEloquentBuilder[]|IlluminateDatabaseEloquentCollection
         */
        public static function getRandomList($num)
        {
            return self::with('a_to_c2') -> select('*') -> orderBy(DB::raw('RAND()')) -> take($num) -> get();
        }

    结果:

     

     以前全toArray()转成数组,本次使用对象的方式:

    <?php
        $data_random = AppModelsArticles::getRandomList(8);
    ?>
    <div class="widget">
        <h3>随机推荐</h3>
    
        <hr>
        <ul class="hotComments">
            @foreach($data_random as $v)
            <li>
                <div class="hotComments-one-img">
                    <a href="/news/{{ $v -> a_to_c -> classification_id }}/{{ $v -> id }}.html" title="{{ $v -> title }}">
                        <img src="{{ $v -> img }}" alt="{{ $v -> title }}">
                    </a>
                </div>
                <div class="hotComments-recent-title">
                    <h4 class="title"><a href="/index.php?r=article/Content/index&content_id=126" title="{{ $v -> title }}">{{ $v -> title }}</a></h4>
    
                </div>
            </li>
            @endforeach
        </ul>
    </div>

    运行结果:

  • 相关阅读:
    浅析 Java 中的 final 关键字
    谷歌Java编程风格指南
    分布式事务之两阶段提交协议(2PC)and 使用事件和消息队列实现分布式事务
    零和博弈与木桶定律
    Executors类创建四种常见线程池
    软件设计的原则&101个设计模式-2011年04月25日 陈皓
    编程中的命名设计那点事-陈皓
    从面向对象的设计模式看软件设计- 2013年02月01日 陈皓
    SQL语句
    分布式事务
  • 原文地址:https://www.cnblogs.com/ichenchao/p/13035826.html
Copyright © 2011-2022 走看看