0

im trying to make an option for an admin, to choose in which order to show the categories in the website.

so in the control panel, he has an up arrow and a down arrow next to each category's title. if he clicks down, the category goes down in one order.

my problam is, if the category is at the bottom, last in the order, and the admin clicks on the down arrow, i want to show an error.

so i did something like this on my controller:

    /*
 * Category order - down
 */
public function down($id)
{

    $cat = Cat::findOrFail($id);

    $new_location = $cat->location + 1;

    $num_cats = count(Cat::all()); //number of cats
    //die($num_cats);
    if ($new_location >= $num_cats)
    {
        return Redirect::route('pages')->with('msg', 'it is allready the last category');
    }

    $cat->where('location', '=', $new_location)->update(['location' => $cat->location]); //moving the old category
    $cat->where('id', '=', $id)->update(['location' => $new_location]); //updating the new location

    return Redirect::route('pages')->with('msg', 'the cat has been updated');
}

but $num_cats turns to be null.

any ideas how can i fetch the number of all my categories?

EDIT: Model

class Cat extends Model { 
    public $timestamps = false; 
    protected $fillable = array( 'name', 'location', 'slug' ); 

    /*
     * A categorey has many pages 
     */ 
    public function pages() { 
        return $this->hasMany('App\Page')->where('solo', '!=', 1); 
    }
}

thanks

13
  • The collection that of which is returned from the all method, has a count method on it. Cat::all()->count() Commented Feb 27, 2015 at 10:33
  • i changed it and still returns null. Commented Feb 27, 2015 at 10:38
  • Is there any results returned from your all()? You could try Cat::get()->count(), same thing just using get instead. Commented Feb 27, 2015 at 10:44
  • still nothing... i added: <code>die($num_cats)</code> to see what i get, and i only see a blank page. so i tried <code>die(print_r($num_cats))</code> and now i get the number 31, though there are only 3 categories. Commented Feb 27, 2015 at 11:03
  • Include your model code. Commented Feb 27, 2015 at 11:08

2 Answers 2

1

Suggest you do count like this$num_cats = Cat::count();

Because the return of Cat::all() is an object of Illuminate\Database\Eloquent\Collection

You need to know the function count int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

AND

Cat::count(); SQL: SELECT count(*) as aggregate FROM "cats"; Need less memory.

Cat::all()->count(); SQL: SELECT * FROM "cats"; Need more memory.

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

3 Comments

not working so i tried $num_cats = Cat::all()->items; and i get this error: Cannot access protected property Illuminate\Database\Eloquent\Collection::$items
@amf oh, sorry. items is a protected property, Collection::all() will return it. Look here. If you want get the count ONLY, please use $num_cats = Cat::count().
so it's the same problem i had... it returns a null value.
0

You can use any of this to get counts:

$num_cats = count(Cat::all());

or

$num_cats = Cat::all()->count();

or

$num_cats = count(Cat::all()->toArray());

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.