Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot use class because it is not a trait

I have followed the tutorial here on building a validation service for laravel. I am having issues now when trying to call the validator from one of my controllers. I am seeing the error:

validController cannot use Portal\Service\Validation\Laravel\AppInstancesValidator - it is not a trait

here is my controller:

class validController extends BaseController {

   use \Portal\Service\Validation\Laravel\AppInstancesValidator;

   public function validateInstance() {
      $post = Input::all();

      $instVal = new AppInstancesValidator( App::make('validator'));

      return $instVal->with($post)->passes();
  }

}

and my validator:

namespace Portal\Service\Validation\Laravel;

use Portal\Service\Validation\ValidableInterface;

class AppInstancesValidator extends LaravelValidator implements ValidableInterface {


  protected $rules = array(
    'app_name' => 'required',
    'app_instance_name' => 'required',
    'app_instance_ip' => 'required|ip'
  );

}
like image 752
arrowill12 Avatar asked May 08 '14 21:05

arrowill12


2 Answers

Try putting the use before the class declaration:

<?php // namespace Portal\Controllers;

use \Portal\Service\Validation\Laravel\AppInstancesValidator;

class validController extends BaseController {

    public function validateInstance() {}

}
like image 115
Sam Avatar answered Nov 08 '22 00:11

Sam


Your 'use' statement should be above the class definition of validController

like image 36
c-griffin Avatar answered Nov 08 '22 02:11

c-griffin