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
Cat::all()->count()Cat::get()->count(), same thing just using get instead.