Skip to content

Association Controller

JP Barbosa edited this page Mar 14, 2016 · 4 revisions

Association Controller

Generate authors controller
php artisan make:controller AuthorsController
Add basic RESTfull methods for authors controller
nano app/Http/Controllers/AuthorsController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Author;

class AuthorsController extends Controller
{

    public function index()
    {
        return Author::all();
    }

    public function store(Request $request)
    {
        return Author::create($request->all());
    }

    public function show(Author $author)
    {
        return $author;
    }

    public function update(Request $request, Author $author)
    {
        $author->update($request->all());
        return $author;
    }

    public function destroy(Author $author)
    {
        return (string) $author->delete();
    }

}
Add routes for authors
nano app/Http/routes.php
Route::resource('authors', 'AuthorsController');
Create author using cURL
php artisan serve
curl -H "Accept: application/json" \
     http://localhost:8000/authors \
     --data "name=Author Name" \
     --data "[email protected]"
{"name":"Author Name","email":"[email protected]"...}
Open authors index and check if the new record was created
curl -H "Accept: application/json" \
     http://localhost:8000/authors
Generate authors request for validation
php artisan make:request AuthorRequest
Add validations rules and change authorize to return true
nano app/Http/Requests/AuthorRequest.php
    ...
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email'
        ];
    }
    ...
Change authors controller to use authors request
nano app/Http/Controllers/AuthorsController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\AuthorRequest;
use App\Http\Controllers\Controller;
use App\Author;

class AuthorsController extends Controller
    ...
    public function store(AuthorRequest $request)
    ...
    public function update(AuthorRequest $request, Author $author)
    ...
}
Try to create invalid author using cURL
curl -H "Accept: application/json" \
     http://localhost:8000/authors \
     --data "name=Author Name" \
     --data "email=invalid_email_address"
{"email":["The email must be a valid email address."]}
Create valid author using cURL
curl -H "Accept: application/json" \
     http://localhost:8000/authors \
     --data "name=Author Name" \
     --data "[email protected]"
{"name":"Author Name","email":"[email protected]"...}
Add author_id as required in article validation
nano app/Http/Requests/ArticleRequest.php
    ...
    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'content' => 'required',
            'author_id' => 'required',
        ];
    }
    ...
Try to create article without author using cURL
curl -H "Accept: application/json" \
     http://localhost:8000/articles \
     --data "title=Article Title" \
     --data "content=Article Content"
{"author_id":["The author id field is required."]}
Create article with author using cURL
curl -H "Accept: application/json" \
     http://localhost:8000/articles \
     --data "title=My Article Title" \
     --data "content=My Article Content" \
     --data "author_id=1"
{"title":"My Article Title","content":"My Article Content","author_id":"1"...}
Add associations controller to Git
git add .
git commit -m "Add authors associations controller"
Next step: Association Views
Clone this wiki locally