-
Notifications
You must be signed in to change notification settings - Fork 49
Association Views
JP Barbosa edited this page Mar 14, 2016
·
4 revisions
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');
}
}
}
mkdir resources/views/authors
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>
nano resources/views/authors/show.blade.php
<div>
<h1>{{ $author->name }}</h1>
<p>{{ $author->email }}</p>
{!! link_to_route('authors.index', 'Authors') !!}
</div>
nano resources/views/authors/create.blade.php
{!! Form::open(['route' => 'authors.store', 'id' => 'authors-form']) !!}
@include ('authors.form', ['submitButtonText' => 'Add Author'])
{!! Form::close() !!}
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() !!}
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) !!}
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>
...
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'));
}
...
}
nano resources/views/articles/show.blade.php
...
<article>
<h1>{{ $article->title }}</h1>
<div>{{ $article->content }}</div>
<div>{{ $article->author->name }}</div>
</article>
...
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>
...
php artisan serve
open http://localhost:8000/authors
open http://localhost:8000/articles
git add .
git commit -m "Add authors associations views"
Next step: Basic Template
- Setup
- Basic CRUD
- Validation
- Views
- Association
- Association Controller
- Association Views
- Basic Template
- Bootstrap
- Bootstrap CRUD
- Alerts
- Welcome Page
- Ajax CRUD
- Send Email
- Send Email Views
- Jobs Queue
- Captcha
- Async External Content
- Cached External Content
- Tests Setup
- Functional Tests
- Acceptance Tests
- Continuous Integration
- Deploy with Heroku
- Deploy with Forge
- Update README