1

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();
3
  • What exactly do you want to do? If you just need to know how many results you got, you don't need to run another query. Just call $data->count(). Commented Apr 7, 2014 at 8:46
  • i want to count all row and get title field value from table category_to_news , with count tehe query will be optimize Commented Apr 7, 2014 at 8:49
  • @user3395037 as @Quasdunk said, just do the second one of those, and use $data->count() to get the count. It doesn't do an SQL COUNT(*) but instead simply counts the results returned in the $data array. Commented Apr 7, 2014 at 9:37

3 Answers 3

2

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

MySQL's count is an aggregate functions, which means: It takes a set of values, performs a calculation and returns a single value. 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'.

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

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

$data is now of type Illuminate\Support\Collection which 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).

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 .

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

$data->count();

Comments

0

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 Eloquent model like this, create the model in your models directory:

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.