Skip to content

Association Views

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

Association Views

Add HTML returns in authors controller
nano app/Http/Controllers/AuthorsController.php
<?php

namespace App\Http\Controllers;

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

class AuthorsController extends Controller
{

    public function index()
    {
        $authors = Author::all();

        if (Request::wantsJson()) {
            return $authors;
        } else {
            return view('authors.index', compact('authors'));
        }
    }

    public function create()
    {
        return view('authors.create');
    }

    public function store(AuthorRequest $request)
    {
        $author = Author::create($request->all());

        if (Request::wantsJson()) {
            return $author;
        } else {
            return redirect('authors');
        }
    }

    public function show(Author $author)
    {
        if (Request::wantsJson()) {
            return $author;
        } else {
            return view('authors.show', compact('author'));
        }
    }

    public function edit(Author $author)
    {
        return view('authors.edit', compact('author'));
    }

    public function update(AuthorRequest $request, Author $author)
    {
        $author->update($request->all());

        if (Request::wantsJson()) {
            return $author;
        } else {
            return redirect('authors');
        }
    }

    public function destroy(Author $author)
    {
        $deleted = $author->delete();

        if (Request::wantsJson()) {
            return (string) $deleted;
        } else {
            return redirect('authors');
        }
    }

}
Create views folder
mkdir resources/views/authors
Create view for index action
nano resources/views/authors/index.blade.php
<h1>Authors</h1>
{!! link_to_route('authors.create', 'New Author') !!}
<table border="1">
    <tr>
        <th>Edit</th>
        <th>Delete</th>
        <th>Name</th>
    </tr>
    @foreach ($authors as $author)
        <tr>
            <td>{!! link_to_route('authors.edit', 'Edit', $author->id) !!}</td>
            <td>
                {!! Form::open(['method' => 'DELETE', 'route' => ['authors.destroy', $author->id]]) !!}
                    <button type="submit">Delete</button>
                {!! Form::close() !!}
            </td>
            <td>{!! link_to_route('authors.show', $author->name, $author->id) !!}</td>
        </tr>
    @endforeach
</table>
Create view for show action
nano resources/views/authors/show.blade.php
<div>
    <h1>{{ $author->name }}</h1>
    <p>{{ $author->email }}</p>
    {!! link_to_route('authors.index', 'Authors') !!}
</div>
Create view for create action
nano resources/views/authors/create.blade.php
{!! Form::open(['route' => 'authors.store', 'id' => 'authors-form']) !!}
    @include ('authors.form', ['submitButtonText' => 'Add Author'])
{!! Form::close() !!}
Create view for edit action
nano resources/views/authors/edit.blade.php
{!! Form::model($author, ['method' => 'PATCH', 'route' => ['authors.update', $author->id], 'id' => 'authors-form']) !!}
    @include ('authors.form', ['submitButtonText' => 'Edit Author'])
{!! Form::close() !!}
Create form partial
nano resources/views/authors/form.blade.php
<div>
    {!! Form::label('name', 'Name:') !!}
    {!! Form::text('name') !!}
</div>
<div>
    {!! Form::label('email', 'E-mail:') !!}
    {!! Form::text('email') !!}
</div>
{!! Form::submit($submitButtonText) !!}
Add author to articles index
nano resources/views/articles/index.blade.php
...
        <th>Title</th>
        <th>Author</th>
    </tr>
...
            <td>{!! link_to_route('articles.show', $article->title, $article->id) !!}</td>
            <td>{!! $article->author->name !!}</td>
       </tr>
...
Add authors to create and edit on articles controller
nano app/Http/Controllers/ArticlesController.php
...
use App\Author;

class ArticlesController extends Controller
{
    ...
    public function create()
    {
        $article = new Article;
        $authors = Author::lists('name', 'id')->all();

        return view('articles.create', compact('article', 'authors'));
    }
    ...
    public function edit(Article $article)
    {
        $authors = Author::lists('name', 'id')->all();

        return view('articles.edit', compact('article', 'authors'));
    }
    ...
}
Add author to articles show
nano resources/views/articles/show.blade.php
...
<article>
    <h1>{{ $article->title }}</h1>
    <div>{{ $article->content }}</div>
    <div>{{ $article->author->name }}</div>
</article>
...
Add author to articles form
nano resources/views/articles/form.blade.php
...
<div>
    {!! Form::label('content', 'Content:') !!}
    {!! Form::textArea('content') !!}
</div>
<div>
    {!! Form::label('author_id', 'Author:') !!}
    {!! Form::select('author_id', $authors, $article->author_id) !!}
</div>
...
Run the server
php artisan serve
Open authors and articles in the browser and play with CRUD
open http://localhost:8000/authors
open http://localhost:8000/articles
Add associations views to Git
git add .
git commit -m "Add authors associations views"
Next step: Basic Template
Clone this wiki locally