mrkarma4ya
7 months ago
3515
9
Laravel

Calling model method from controller

Posted 7 months ago by mrkarma4ya

Hello,

I'm trying to follow this: https://github.com/alexeymezenin/laravel-best-practices#fat-models-skinny-controllers

And I'm trying to exactly what they're doing, but getting an error.

Here's my model Category.php

class Category extends Model
{
    //
    public function posts(){
        return $this->belongstoMany('App\Post');
    }

    public function getAllCategories(){
        $this->orderBy('category', 'asc')->paginate(10)->get();
    }
}

Here's my [email protected]

public function index()
{      
   return view('categories.index', ['categories' => $this->category->getAllCategories()]);  
}

This is the error:

ErrorException Undefined property: App\Http\Controllers\CategoryController::$category

I'm not really sure how to properly call the method inside the model.

Please sign in or create an account to participate in this conversation.