YuHo のブログ

Laravel, PHP, JavaScript

Laravel 5.6 でブログサイトを制作る #4

create.blade.php を bootstrap class で整える

<form action="{{ route('posts.store') }}" method="POST">
    @csrf
    <div class="form-group">
        <input id="title" class="form-control" name="title" type="text" />
    </div>
    <div class="form-group">
        <textarea id="content" class="form-control" name="content" row=6></textarea>
    </div>
    <div class="form-group">
        <button class="btn btn-primary" type="submit">Store</button>
    </div>
</form>

Pagination の方法を変更する

<?php

Route::get('/', function () {
    $posts = App\Post::orderBy('id', 'desc')->paginate(10);
    
    return view('welcome', ['posts' => $posts]);
});

edit.blade.php を bootstrap class で整える

<form action="{{ route('posts.update', ['post' => $post->id]) }}" method="POST">
    @csrf
    @method('PUT')
    <div class="form-group">
        <input id="title" class="form-control" name="title" type="text" value="{{ $post->title }}" />
    </div>
    <div class="form-group">
        <textarea id="content" class="form-control" name="content" row=6>{{ $post->content }}</textarea>
    </div>
    <div class="form-group">
        <button class="btn btn-primary" type="submit">Update</button>
    </div>
</form>

簡易ログインをランダムなアカウントに対して実行する

<?php

Route::get('/quick-login', function() {
    $n = App\Flight::->count();
    
    $user = App\User::find($n);
    
    Auth::login($user, true);
    
    return redirect('/');
})->name('quick-login');

Paginator のリンクを貼る

@extends('layouts.app')

@section('content')
<div class="content">
    @foreach($posts as $post)
        <p><a href="{{ route('posts.show', $post->id) }}">{{ $post->title }}</a></p>
    @endforeach
    {{ $posts->links() }}
</div>
@endsection

User 用の Resource Controller を作成する

php artisan make:controller UserController --resource --model=User

User Resourceful Route を作成する

<?php

Route::resource('users', 'UserController', ['only' => [
    'show', 'edit', 'update', 'delete'
]]);

UserController の編集

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return view('users.show', ['user' => $user]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        return view('users.edit', ['post' => $post]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        //
    }
}