YuHo のブログ

Laravel, PHP, JavaScript

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

users.show Blade を作成する

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <div class="row">
                        <div class="col">
                            {{ $user->name }}
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    <div class="row"><div class="col">{{ $user->email }}</div></div>
                </div>

            </div>
        </div>
    </div>
</div>
@endsection

users.edit Blade を作成する

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <form action="{{ route('users.update', ['user' => $user->id]) }}" method="POST">
                    @csrf
                    @method('PUT')
                    <div class="form-group">
                        <input id="name" class="form-control" name="name" type="text" value="{{ $user->title }}" />
                    </div>
                    <div class="form-group">
                        <input id="email" class="form-control" name="email" type="text" value="{{ $user->email }}" />
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="submit">Update</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

UserController を編集する

<?php

public function update(Request $request, User $user)
{
    $user->name = $request->input('name');
    $user->email = $request->input('email');

    $user->save();

    return redirect('/');
}