lundi 17 décembre 2018

Laravel 5, how to make object in correct way?

I have the service for UserController

class UserService
{

  protected $user;

  public function __construct(User $user) {
      $this->user = $user;
  } 

  public function getAll()
  {
     $this->user->get();
     // do something with $this->user
  }

  public function update($data)
  {
      return $this->user->update($data);
  }

}

class UserController extends Controller
{

  private $service;

  public function __construct(UserService $service)
  {
      $this->service = $service; // < ------ error
  }

  public function index(User $user)
  {
      return $this->service->getAll();
  }

  public function update(Request $request, User $user)
  {
      return $this->service->update();
  }

}

Needed to avoid the situation when I should make object inside each method of the controller

How can I make object from UserService with parameter $user in the constructor for all methods of the controller?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire