Skip to content

Basic CRUD Views

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

CRUD Views

Add HTML returns in articles controller
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');
        }
    }
}
Create articles views folder
mkdir resources/views/articles
Create view for index action
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>
Create view for show action
nano resources/views/articles/show.blade.php
<article>
    <h1>{{ $article->title }}</h1>
    <div>{{ $article->content }}</div>
</article>
{!! link_to_route('articles.index', 'Articles') !!}
Create view for create action
nano resources/views/articles/create.blade.php
{!! Form::open(['route' => 'articles.store', 'id' => 'articles-form']) !!}
    @include ('articles.form', ['submitButtonText' => 'Add Article'])
{!! Form::close() !!}
Create view for edit action
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() !!}
Create form partial
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) !!}
Start the server
php artisan serve
Create article in the browser using HTML forms
open http://localhost:8000/articles/create
Open articles in the browser and check if the new record was created
open http://localhost:8000/articles
Add basic CRUD views to Git
git add .
git commit -m "Add articles views"
Next step: Association
Clone this wiki locally