Inheritance in controller.php Laravel 5.5

0

Is it possible to create a function in the Controller.php file and then use it in the controllers? I try to do it and I get the error:

Method [findArticleById] does not exist on [App\Http\Controllers\ArticlesController].

The function in Controller.php:

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    private function findArticleById($id)
    {
      return Article::where('id', $id)->firstOrFail();
    }
}

The controller:

public function show($id)
  {
    $article = $this->findArticleById($id);

    addVisitCountToArticle($article);

    return view('articles.item.show', compact('article'));
  }
    
asked by MarianoV 19.12.2017 в 00:26
source

1 answer

0

The second controller should extend the first as the first extends BaseController.

The second would look like this (*):

class SegundoController extends Controller
   {
       public function show($id)
          {
            $article = $this->findArticleById($id);

            addVisitCountToArticle($article);

            return view('articles.item.show', compact('article'));
          }
   }

(*) I invent names of classe since you do not put it. I do not mention the namespace although it should be taken into account. Do not forget to import the classe Controller

    
answered by 19.12.2017 в 08:26