-
Notifications
You must be signed in to change notification settings - Fork 49
Basic CRUD Views
JP Barbosa edited this page Mar 14, 2016
·
4 revisions
nano app/Http/Controllers/ArticlesController.php
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests\ArticleRequest;
use App\Article;
class ArticlesController extends Controller
{
public function index()
{
$articles = Article::all();
if (Request::wantsJson()) {
return $articles;
} else {
return view('articles.index', compact('articles'));
}
}
public function create()
{
$article = new Article;
return view('articles.create', compact('article'));
}
public function store(ArticleRequest $request)
{
$article = Article::create($request->all());
if (Request::wantsJson()) {
return $article;
} else {
return redirect('articles');
}
}
public function show(Article $article)
{
if (Request::wantsJson()) {
return $article;
} else {
return view('articles.show', compact('article'));
}
}
public function edit(Article $article)
{
return view('articles.edit', compact('article'));
}
public function update(ArticleRequest $request, Article $article)
{
$article->update($request->all());
if (Request::wantsJson()) {
return $article;
} else {
return redirect('articles');
}
}
public function destroy(Article $article)
{
$deleted = $article->delete();
if (Request::wantsJson()) {
return (string) $deleted;
} else {
return redirect('articles');
}
}
}
mkdir resources/views/articles
nano resources/views/articles/index.blade.php
<h1>Articles</h1>
{!! link_to_route('articles.create', 'New Article') !!}
<table border="1">
<tr>
<th>Edit</th>
<th>Delete</th>
<th>Title</th>
</tr>
@foreach ($articles as $article)
<tr>
<td>{!! link_to_route('articles.edit', 'Edit', $article->id) !!}</td>
<td>
{!! Form::open(['method' => 'DELETE', 'route' => ['articles.destroy', $article->id]]) !!}
<button type="submit">Delete</button>
{!! Form::close() !!}
</td>
<td>{!! link_to_route('articles.show', $article->title, $article->id) !!}</td>
</tr>
@endforeach
</table>
nano resources/views/articles/show.blade.php
<article>
<h1>{{ $article->title }}</h1>
<div>{{ $article->content }}</div>
</article>
{!! link_to_route('articles.index', 'Articles') !!}
nano resources/views/articles/create.blade.php
{!! Form::open(['route' => 'articles.store', 'id' => 'articles-form']) !!}
@include ('articles.form', ['submitButtonText' => 'Add Article'])
{!! Form::close() !!}
nano resources/views/articles/edit.blade.php
{!! Form::model($article, ['method' => 'PATCH', 'route' => ['articles.update', $article->id], 'id' => 'articles-form']) !!}
@include ('articles.form', ['submitButtonText' => 'Edit Article'])
{!! Form::close() !!}
nano resources/views/articles/form.blade.php
<div>
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title') !!}
</div>
<div>
{!! Form::label('content', 'Content:') !!}
{!! Form::textArea('content') !!}
</div>
{!! Form::submit($submitButtonText) !!}
php artisan serve
open http://localhost:8000/articles/create
open http://localhost:8000/articles
git add .
git commit -m "Add articles views"
Next step: Association
- 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