douzhuo6270
2015-01-12 06:47
浏览 104
已采纳

Laravel Eloquent ORM让功能混乱

I am new to laravel so can anybody explain me that what exactly get() function of eloquent ORM does. I have following query and I need to know on which line the database is queried.

$propertiesQuery = Property::with('country', 'city','bannerInvoicesCount','rejectedBannerInvoicesCount')
           ->where('is_deleted','=',1);  
        if(Auth::user()->type == 'po') {
            $propertiesQuery->where('user_id', '=', Auth::user()->id);
        }

        $propertiesQuery->orderBy('created_at', 'DESC');
        $properties = $propertiesQuery->get();

Now in above code I have accessed Model once whether that is the point when db is queried or when I have called get() function. I am not been able to comprehend the internal working of this. That exactly on which statement the query is executed.

图片转代码服务由CSDN问答提供 功能建议

我是laravel的新手所以任何人都可以解释一下eloquent ORM究竟是什么样的get()函数。 我有以下查询,我需要知道查询数据库的哪一行。

  $ propertiesQuery = Property :: with('country','city','bannerInvoicesCount'  ,'rejectedBannerInvoicesCount')
  - > where('is_deleted','=',1);  
 if(Auth :: user() - > type =='po'){
 $ propertiesQuery-> where('user_id','=',Auth :: user() - > id);  
} 
 
 $ propertiesQuery-> orderBy('created_at','DESC'); 
 $ properties = $ propertiesQuery-> get(); 
   
 \  n 

现在在上面的代码中,我已经访问过Model,无论是查询db还是调用get()函数。 我无法理解这方面的内部工作。 这正是执行查询的语句。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhanglun4482 2015-01-12 07:11
    已采纳

    @Raza before eloquant $propertiesQuery->get() step, it prepare your query like:

    select * from table where user_id=1
    

    and when you call/trigger the $propertiesQuery->get() method, it forward your query to database and fetch the records against your query. like, normally we do in plain sql:

    eg: mysql_query($query);
    then mysql_fetch_assoc() etc methods
    

    so get() method contains on: execute the query and return all the records in the array format.

    I hope it will help you.

    打赏 评论

相关推荐 更多相似问题