3

I'm implementing an API that can create clients and create contacts. Contacts may be associated with a client.

I have my ClientController.php with all the CRUD methods.

I also have a ContactController.php with all the CRUD methods.

I want to allow a contact to be created and assigned to a client all in the one API call.

I was thinking the best way would be add a function to my ClientController and call it via the API: /api/client/6/addContact

public function addContact($clientId, Request $request) {
   $contact = new Contact($request->all())->save();

   $client = Client::find($clientId)->attach($contact->id);

   return response()->json(null, 200);

}

But my issue is that the method to add a contact (and validate it) is in the ContactController.php, so i'm doubling up on the code. How can I use the ContactController@store method from the ClientController@addContact ?

Is there a common API architecture for these types of issues?

Thanks!

5
  • You could use a service pattern. In a project I'm working on right now, I make plain PHP classes in an App\Services namespace that I can then inject into my controllers (or other services) to provide functionality, passing in any required HTTP data like get/post variables to the service. Commented Aug 23, 2018 at 0:01
  • 1
    @Nathan maybe he should use eloquent observers. he is not trying to use a third party library.Ben Southall, if you use Laravel 5.3 or plus, you should use observers because you are observing add client method. for more info read this please : laravel.com/docs/5.3/eloquent#observers Commented Aug 23, 2018 at 0:17
  • @Thamerbelfkih thanks for this, i read about observers many times but never thought i could use it to achieve this Commented Aug 23, 2018 at 0:18
  • so as i understood you have a client that is saved, now you want to create a contact and attach it to a client, if so why don't you just call $client->contacts()->attach($contact->id) in the store method of ContactController ? Commented Aug 23, 2018 at 0:30
  • Thanks @Nathan could you give me an example of your service? I can seem to find this pattern anywhere in the laravel docs. Commented Aug 23, 2018 at 2:08

2 Answers 2

3

I'd suggest using a nested api resource route:

// generate the controller class
php artisan make:controller Api/ContactController --api

// create the route definitions
Route::apiResource('clients.contacts', 'ContactController');

The above creates the routes:

GET /clients/{client}/contacts
POST /clients/{client}/contacts
GET /clients/{client}/contacts/{contact}
PUT/PATCH /clients/{client}/contacts/{contact}
DELETE /clients/{client}/contacts/{contact}
Sign up to request clarification or add additional context in comments.

Comments

0

You are creating a one to many relationship. I would focus on establishing that relationship in eloquent and then the rest will fall into place:

https://laravel.com/docs/5.6/eloquent-relationships#one-to-many

After you create the relationship correctly your API calls will be structured around a json string (object) that will have one client and one or more contacts.

If you have any questions, please ask, but this basic strategy will get you to where you want to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.