laravel 如何在laravel中统计和获取数据查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22907350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:19:13  来源:igfitidea点击:

How to count and fetch data query in laravel?

phpmysqllaravel

提问by user3395037

How to merge this two queries ?

如何合并这两个查询?

$data = DB::table('category_to_news')
      ->where('category_to_news.name', ucwords($category))
      ->remember(1440)
      ->count();

and

$data = DB::table('category_to_news')
      ->where('category_to_news.name', ucwords($category))
      ->remember(1440)
      ->get();

回答by Quasdunk

So, as far as I understand from your comment, you simply want to get allrecords from the table category_to_newsand you want to know how many records are in there, right?

因此,据我从您的评论中了解到,您只是想从表中获取所有记录,category_to_news并且想知道其中有多少记录,对吗?

MySQL's countis an aggregate functions, which means: It takes a set of values, performs a calculation and returns a singlevalue. If you put it into your names-query, you get the same value in each record. I'm not sure if that has anything to do with 'optimization'.

MySQLcount是一个聚合函数,这意味着:它接受一组值,执行计算并返回单个值。如果你把它放到你的名字查询中,你会在每条记录中得到相同的值。我不确定这是否与“优化”有关。

As already said, you simply run your query as usual:

如前所述,您只需像往常一样运行查询:

$data = DB::table('category_to_news')
      ->where('name', ucwords($category))
      ->remember(1440)
      ->get(['title']);

$datais now of type Illuminate\Support\Collectionwhich provides handy functions for collections, and one them is count()(not to be confused with the above mentioned aggregate function - you're back in PHP again, not MySQL).

$data现在是为集合Illuminate\Support\Collection提供方便的函数的类型,其中之一是count()不要与上面提到的聚合函数混淆——你又回到了 PHP,而不是 MySQL)。

So $data->count()gives you the number of items in the collection (which pretty much is an array on steroids) without even hitting the database.

So$data->count()为您提供集合中的项目数(这几乎是一个类固醇数组),甚至无需访问数据库。

回答by Shahid Chaudhary

Hi DB class dont return collection object it give error "call member function on array" but eloquent return collection object. for above code we can use collect helper function to make it collection instance then use count and other collection methods https://laravel.com/docs/5.1/collections#available-methods.

嗨,DB 类不返回集合对象,它给出错误“在数组上调用成员函数”,但雄辩地返回集合对象。对于上面的代码,我们可以使用 collect 辅助函数来制作它的集合实例,然后使用 count 和其他收集方法https://laravel.com/docs/5.1/collections#available-methods

$data = DB::table('category_to_news')
      ->where('name', ucwords($category))
      ->remember(1440)
      ->get();
$data = collect($data);

$data->count();

回答by The Alpha

You my get it using:

你我得到它使用:

$data = DB::table('category_to_news')
          ->where('name', ucwords($category))
          ->remember(1440)
          ->get();

To get the count, try this:

要获得计数,请尝试以下操作:

$data->count();

Why you are using DB::table(...), instead you may use Eloquentmodel like this, create the model in your modelsdirectory:

你为什么使用DB::table(...),而不是你可以使用Eloquent这样的模型,在你的models目录中创建模型:

class CategoryToNews extends Eloquent {
    protected $table = 'category_to_news';
    protected $primaryKey = 'id'; // if different than id then change it here
}

Now, you may easily use:

现在,您可以轻松使用:

$data = CategoryToNews::whereName(ucwords($category))->get();

To get the count, use:

要获得计数,请使用:

$data->count();