こちらの内容はブログにアップしています。
はじめに
https://ksysnote.com/php/laravel-api/こちらの続きとなっています。
今回はLaravelでCRUDを作成していきたいと思います。特に難しい機能はありませんが、登録・一覧・詳細・編集画面と削除の動作するところまでを目標とします。
無駄にカラムを多くしてしまったため、コード量は多くなりましたが内容は対しことがないのでコピペで対応して下さい。
環境:
OS : macOS High Sierra 10.13.6MAMP : 5.1
Laravel : 5.7.6
モデルの作成
まずは、CRUDで使用するテーブルを作成します。特なんでもいいのですがユーザー(Client)がそれぞれ顧客を管理するという想定で顧客(Customer)テーブルを作成していきたいと思います。モデル・マイグレーションファイルを作成する
$ php artisan make:model Customer -m
マイグレーション
マイグレーションファイルに必要なカラムを設定していきます。「/database/migrations/DATETIME_create_customers_table.php」
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->increments('id');
$table->integer('client_id');
$table->string('country')->nullable(true); // 国名
$table->string('postcode')->nullable(true); // 郵便番号
$table->string('prefecture')->nullable(true); // 都道府県
$table->string('city')->nullable(true); // 市
$table->string('ward')->nullable(true); // 区
$table->string('streetAddress')->nullable(true); // 町以下
$table->string('secondaryAddress')->nullable(true); // マンション名
$table->string('company')->nullable(true); // 会社名
$table->string('userName')->nullable(true); // ユーザ名
$table->string('domainName')->nullable(true); // ドメイン名
$table->string('email')->nullable(true); // メールアドレス
$table->string('lastName')->nullable(true); // 姓
$table->string('firstName')->nullable(true); // 名
$table->string('lastKanaName')->nullable(true); // 姓カナ
$table->string('firstKanaName')->nullable(true); // 名カナ
$table->string('phoneNumber')->nullable(true); // 電話番号
$table->string('realText')->nullable(true); // 日本語文章
$table->timestamps();
});
}
マイグレーションの実行
$ php artisan migrate
テストデータの作成
まずはSeederを作成して50件のテストデータを作成するようにします。$ php artisan make:seeder CustomerTableSeeder
public function run()
{
factory(App\Customer::class, 50)->create();
}
Modle Factoryを作成する
Fakerを使ってカラムごとのテストデータを設定していきます。$ php artisan make:factory CustomerFactory
$factory->define(App\Customer::class, function (Faker $faker) {
// 削除する
DB::table('customers')->delete();
$clients = App\Client::pluck('id')->toArray();
return [
'client_id' => $faker->randomElement($clients),
'country' => $faker->country(), // 国名
'postcode' => $faker->postcode(), // 郵便番号
'prefecture' => $faker->prefecture(), // 都道府県
'city' => $faker->city(), // 市
'ward' => $faker->ward(), // 区
'streetAddress' => $faker->streetAddress(), // 町以下
'secondaryAddress' => $faker->secondaryAddress(), // マンション名
'company' => $faker->company(), // 会社名
'userName' => $faker->userName(), // ユーザ名
'domainName' => $faker->domainName(), // ドメイン名
'email' => $faker->email(), // メールアドレス
'lastName' => $faker->lastName(), // 姓
'firstName' => $faker->firstName(), // 名
'lastKanaName' => $faker->lastKanaName(), // 姓カナ
'firstKanaName' => $faker->firstKanaName(), // 名カナ
'phoneNumber' => $faker->phoneNumber(), // 電話番号
'realText' => $faker->realText(), // 日本語文章
];
});
Seederを実行してテーブルの中身を確認してみます。
$ php artisan db:seed --class=CustomerTableSeeder
テーブルの中身を見ると住所等も入っています。
コントローラの作成
$ php artisan make:controller CustomerController -r
後でアクションについては説明しますが、一覧、追加、編集、削除の基本的なものが用意されています。
「app/Http/Controller/CustomerController.php」
class CustomerController 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 int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
ルーティングを設定する
「routes/web.php」// 新しくルーティングを追加
Route::resource('customer', 'CustomerController');
「php artisan route:list」で設定されているルーティングを確認することができます。以下はcustomerの箇所だけ抜粋しています。
$ php artisan route:list
+--------+-----------+--------------------------------------+-----------------------------------+-------------------------------------------------
| Domain | Method | URI | Name | Action
+--------+-----------+--------------------------------------+-----------------------------------+-------------------------------------------------
| | GET|HEAD | customer | customer.index | App\Http\Controllers\CustomerController@index
| | POST | customer | customer.store | App\Http\Controllers\CustomerController@store
| | GET|HEAD | customer/create | customer.create | App\Http\Controllers\CustomerController@create
| | DELETE | customer/{customer} | customer.destroy | App\Http\Controllers\CustomerController@destroy
| | PUT|PATCH | customer/{customer} | customer.update | App\Http\Controllers\CustomerController@update
| | GET|HEAD | customer/{customer} | customer.show | App\Http\Controllers\CustomerController@show
| | GET|HEAD | customer/{customer}/edit | customer.edit | App\Http\Controllers\CustomerController@edit
+--------+-----------+--------------------------------------+-----------------------------------+-------------------------------------------------
認証を追加
ログインユーザーだけ表示できるようにmiddlewareをCustomerControllerに追加します。「app/Http/Controller/CustomerController.php」
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
一覧ページを作成する
まず最初に一覧ベージから作成していきます。「/customer」にGETでアクセスすると「CustomerController@index」が呼ばれます。
ビューを作成する
今から作成するCustomerのViewは全てCustomerディレクトリ内にまとめますので、「resources/view/」内にディレクトリを作成して下さい。一覧を表示するだけの「index.blade.php」を作成します。まだ使用しませんが、追加・編集・詳細・削除のボタンも用意しておきます。
「resources/views/customer/index.blade.php」
@extends('layouts.app')
@section('content')
<div class="container">
<div class="page-content browse container-fluid">
<div class="row">
<div class="card">
<div class="card-header">{{__('Customer List')}}
<button type="button" class="btn btn-sm btn-primary ml-1" onclick="location.href='/customer/create'">
{{ __('Add') }}
</button>
</div>
<div class="panel panel-bordered">
<div class="panel-body">
<div class="table-responsive">
<div id="dataTable_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
<div class="row">
<div class="col-sm-12">
<table class="table table-hover dataTable">
<thead>
<tr role="row">
<th>Id</th>
<th>Prefecture</th>
<th>City</th>
<th>Ward</th>
<th>StreetAddress</th>
<th>SecondaryAddress</th>
<th>Company</th>
<th>Email</th>
<th>Name</th>
<th>PhoneNumber</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($customers as $customer)
<tr role="row" class="odd">
<td>{{$customer->id}}</td>
<td>{{$customer->prefecture}}</td>
<td>{{$customer->city}}</td>
<td>{{$customer->ward}}</td>
<td>{{$customer->streetAddress}}</td>
<td>{{$customer->secondaryAddress}}</td>
<td>{{$customer->company}}</td>
<td>{{$customer->email}}</td>
<td>{{$customer->lastName}} {{$customer->firstName}}</td>
<td>{{$customer->phoneNumber}}</td>
<td>
<form action="/customer/{{$customer->id}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete">
<button type="submit" class="btn btn-sm btn-danger">
{{ __('Delete') }}
</button>
</form>
<a href="/customer/{{$customer->id}}/edit" title="Edit" class="btn btn-sm btn-primary pull-right edit">
<i class="voyager-edit"></i> <span class="hidden-xs hidden-sm">{{__('Edit')}}</span>
</a>
<a href="/customer/{{$customer->id}}" title="View" class="btn btn-sm btn-warning pull-right view">
<i class="voyager-eye"></i> <span class="hidden-xs hidden-sm">{{__('View')}}</span>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
コントローラ
index内で顧客情報を取得してビューを呼び出す処理を追加します。ログイン情報からIDを取得して、クライアントの顧客情報を取得するようにしています。
「app/Http/Controllers/CustomerController.php」
public function index()
{
// ログイン情報
$user = \Auth::user();
$customers = Customer::where('client_id', $user->id)->get();
return View('customer.index', ['customers' => $customers]);
}
詳細ページを作成する
次に顧客情報の詳細画面を作成していきます。「/customer/{customer}」をGETでアクセスするとルーティングで「CustomerController@show」が呼ばれます。
ビュー
顧客情報を表示する詳細画面のビューを作成します。「resources/views/customer/show.blade.php」
@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">{{ __('Custeomer Detail') }}</div>
<div class="card-body">
<div class="form-group row">
<label for="country" class="col-md-4 col-form-label text-md-right">{{ __('country') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->country}}</label>
</div>
<div class="form-group row">
<label for="postcode" class="col-md-4 col-form-label text-md-right">{{ __('postcode') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->postcode}}</label>
</div>
<div class="form-group row">
<label for="prefecture" class="col-md-4 col-form-label text-md-right">{{ __('prefecture') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->prefecture}}</label>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">{{ __('city') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->city}}</label>
</div>
<div class="form-group row">
<label for="ward" class="col-md-4 col-form-label text-md-right">{{ __('ward') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->ward}}</label>
</div>
<div class="form-group row">
<label for="streetAddress" class="col-md-4 col-form-label text-md-right">{{ __('streetAddress') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->streetAddress}}</label>
</div>
<div class="form-group row">
<label for="secondaryAddress" class="col-md-4 col-form-label text-md-right">{{ __('secondaryAddress') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->secondaryAddress}}</label>
</div>
<div class="form-group row">
<label for="company" class="col-md-4 col-form-label text-md-right">{{ __('company') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->company}}</label>
</div>
<div class="form-group row">
<label for="userName" class="col-md-4 col-form-label text-md-right">{{ __('userName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->userName}}</label>
</div>
<div class="form-group row">
<label for="domainName" class="col-md-4 col-form-label text-md-right">{{ __('domainName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->domainName}}</label>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('email') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->email}}</label>
</div>
<div class="form-group row">
<label for="lastName" class="col-md-4 col-form-label text-md-right">{{ __('lastName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->lastName}}</label>
</div>
<div class="form-group row">
<label for="firstName" class="col-md-4 col-form-label text-md-right">{{ __('firstName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->firstName}}</label>
</div>
<div class="form-group row">
<label for="lastKanaName" class="col-md-4 col-form-label text-md-right">{{ __('lastKanaName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->lastKanaName}}</label>
</div>
<div class="form-group row">
<label for="firstKanaName" class="col-md-4 col-form-label text-md-right">{{ __('firstKanaName') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->firstKanaName}}</label>
</div>
<div class="form-group row">
<label for="phoneNumber" class="col-md-4 col-form-label text-md-right">{{ __('phoneNumber') }} :</label>
<label class="col-md-4 col-form-label text-md-left">{{$customer->phoneNumber}}</label>
</div>
<div class="form-group row">
<label for="realText" class="col-md-4 col-form-label text-md-right">{{ __('realText') }} :</label>
<label class="col-md-7 col-form-label text-md-left">{{$customer->realText}}</label>
</div>
<div class="row mb-0 btn-toolbar">
<div class="btn-group offset-md-4">
<button type="submit" class="btn btn-default" onclick="location.href='/customer/'">
{{ __('List Back') }}
</button>
<button type="submit" class="btn btn-primary ml-1" onclick="location.href='/customer/{{$customer->id}}/edit'">
{{ __('Edit') }}
</button>
<form action="/customer/{{$customer->id}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete">
<button type="submit" class="btn btn-danger ml-5">
{{ __('Delete') }}
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
コントローラ
顧客情報を取得してビューを呼び出す処理を追加します。「app/Http/Controllers/CustomerController.php」
public function show($id)
{
// ログイン情報
$user = \Auth::user();
// 引数で受け取った$idを元にfindでレコードを取得
$send = Send::where(['id' => $id, 'client_id' => $user->id])->first();
// viewにデータを渡す
return view('send.show', ['send' => $send]);
}
登録画面を作成する
顧客を登録する画面を作成します。細かいバリデーションは無しで登録するだけの画面です。「/customer/create}」にGETでアクセスするとルーティングで「CustomerController@create」が呼ばれて登録画面が表示されます。
登録画面から「/customer」にPOSTでデータを送りますとルーティングで「CustomerController@store」が呼ばれデータが登録されます。
ビュー
登録画面のビューを作成します。「resources/views/customer/create.blade.php」
@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">{{ __('Custeomer Edit') }}</div>
<div class="card-body">
<form action="/customer" method="post">
@csrf
<div class="form-group row">
<label for="country" class="col-md-4 col-form-label text-md-right">{{ __('country') }} :</label>
<div class="col-md-6">
<input id="country" type="text" class="form-control{{ $errors->has('country') ? ' is-invalid' : '' }}" name="country" value="{{old('country')}}" required autofocus>
@if ($errors->has('country'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('country') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="postcode" class="col-md-4 col-form-label text-md-right">{{ __('postcode') }} :</label>
<div class="col-md-6">
<input id="postcode" type="text" class="form-control{{ $errors->has('postcode') ? ' is-invalid' : '' }}" name="postcode" value="{{old('postcode')}}" required autofocus>
@if ($errors->has('postcode'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('postcode') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="prefecture" class="col-md-4 col-form-label text-md-right">{{ __('prefecture') }} :</label>
<div class="col-md-6">
<input id="prefecture" type="text" class="form-control{{ $errors->has('prefecture') ? ' is-invalid' : '' }}" name="prefecture" value="{{old('prefecture')}}" required autofocus>
@if ($errors->has('prefecture'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('prefecture') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">{{ __('city') }} :</label>
<div class="col-md-6">
<input id="city" type="text" class="form-control{{ $errors->has('city') ? ' is-invalid' : '' }}" name="city" value="{{old('city')}}" required autofocus>
@if ($errors->has('city'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('city') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="ward" class="col-md-4 col-form-label text-md-right">{{ __('ward') }} :</label>
<div class="col-md-6">
<input id="ward" type="text" class="form-control{{ $errors->has('ward') ? ' is-invalid' : '' }}" name="ward" value="{{old('ward')}}" required autofocus>
@if ($errors->has('ward'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('ward') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="streetAddress" class="col-md-4 col-form-label text-md-right">{{ __('streetAddress') }} :</label>
<div class="col-md-6">
<input id="streetAddress" type="text" class="form-control{{ $errors->has('streetAddress') ? ' is-invalid' : '' }}" name="streetAddress" value="{{old('streetAddress')}}" required autofocus>
@if ($errors->has('streetAddress'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('streetAddress') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="secondaryAddress" class="col-md-4 col-form-label text-md-right">{{ __('secondaryAddress') }} :</label>
<div class="col-md-6">
<input id="secondaryAddress" type="text" class="form-control{{ $errors->has('secondaryAddress') ? ' is-invalid' : '' }}" name="secondaryAddress" value="{{old('secondaryAddress')}}" required autofocus>
@if ($errors->has('secondaryAddress'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('secondaryAddress') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="company" class="col-md-4 col-form-label text-md-right">{{ __('company') }} :</label>
<div class="col-md-6">
<input id="company" type="text" class="form-control{{ $errors->has('company') ? ' is-invalid' : '' }}" name="company" value="{{old('company')}}" required autofocus>
@if ($errors->has('company'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('company') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="userName" class="col-md-4 col-form-label text-md-right">{{ __('userName') }} :</label>
<div class="col-md-6">
<input id="userName" type="text" class="form-control{{ $errors->has('userName') ? ' is-invalid' : '' }}" name="userName" value="{{old('userName')}}" required autofocus>
@if ($errors->has('userName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('userName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="domainName" class="col-md-4 col-form-label text-md-right">{{ __('domainName') }} :</label>
<div class="col-md-6">
<input id="domainName" type="text" class="form-control{{ $errors->has('domainName') ? ' is-invalid' : '' }}" name="domainName" value="{{old('domainName')}}" required autofocus>
@if ($errors->has('domainName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('domainName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('email') }} :</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{old('email')}}" required autofocus>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lastName" class="col-md-4 col-form-label text-md-right">{{ __('lastName') }} :</label>
<div class="col-md-6">
<input id="lastName" type="text" class="form-control{{ $errors->has('lastName') ? ' is-invalid' : '' }}" name="lastName" value="{{old('lastName')}}" required autofocus>
@if ($errors->has('lastName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lastName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="firstName" class="col-md-4 col-form-label text-md-right">{{ __('firstName') }} :</label>
<div class="col-md-6">
<input id="firstName" type="text" class="form-control{{ $errors->has('firstName') ? ' is-invalid' : '' }}" name="firstName" value="{{old('firstName')}}" required autofocus>
@if ($errors->has('firstName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lastKanaName" class="col-md-4 col-form-label text-md-right">{{ __('lastKanaName') }} :</label>
<div class="col-md-6">
<input id="lastKanaName" type="text" class="form-control{{ $errors->has('lastKanaName') ? ' is-invalid' : '' }}" name="lastKanaName" value="{{old('lastKanaName')}}" required autofocus>
@if ($errors->has('lastKanaName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lastKanaName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="firstKanaName" class="col-md-4 col-form-label text-md-right">{{ __('firstKanaName') }} :</label>
<div class="col-md-6">
<input id="firstKanaName" type="text" class="form-control{{ $errors->has('firstKanaName') ? ' is-invalid' : '' }}" name="firstKanaName" value="{{old('firstKanaName')}}" required autofocus>
@if ($errors->has('firstKanaName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('firstKanaName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="phoneNumber" class="col-md-4 col-form-label text-md-right">{{ __('phoneNumber') }} :</label>
<div class="col-md-6">
<input id="phoneNumber" type="text" class="form-control{{ $errors->has('phoneNumber') ? ' is-invalid' : '' }}" name="phoneNumber" value="{{old('phoneNumber')}}" required autofocus>
@if ($errors->has('phoneNumber'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('phoneNumber') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="realText" class="col-md-4 col-form-label text-md-right">{{ __('realText') }} :</label>
<div class="col-md-6">
<textarea id="realText" class="form-control{{ $errors->has('realText') ? ' is-invalid' : '' }}" name="realText" required autofocus>{{old('realText')}}</textarea>
@if ($errors->has('realText'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('realText') }}</strong>
</span>
@endif
</div>
</div>
<div class="row mb-0 btn-toolbar">
<div class="btn-group offset-md-4">
<button type="button" class="btn btn-default" onclick="location.href='/customer/'">
{{ __('Back') }}
</button>
<button type="submit" class="btn btn-success ml-5">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
コントローラ
createはビューを呼出すだけです。「app/Http/Controllers/CustomerController.php」
public function create()
{
return view('send.create');
}
実際の登録はstore内に記述します。
public function store(Request $request)
{
// ログイン情報
$user = \Auth::user();
$customer = new Customer;
$customer->country = $request->country;
$customer->postcode = $request->postcode;
$customer->prefecture = $request->prefecture;
$customer->city = $request->city;
$customer->ward = $request->ward;
$customer->streetAddress = $request->streetAddress;
$customer->secondaryAddress = $request->secondaryAddress;
$customer->company = $request->company;
$customer->userName = $request->userName;
$customer->domainName = $request->domainName;
$customer->email = $request->email;
$customer->lastName = $request->lastName;
$customer->firstName = $request->firstName;
$customer->lastKanaName = $request->lastKanaName;
$customer->firstKanaName = $request->firstKanaName;
$customer->phoneNumber = $request->phoneNumber;
$customer->realText = $request->realText;
$customer->client_id = $user->id;
// 保存
$customer->save();
return redirect('/customer');
}
編集ページを作成する
編集をする画面を作成します。「/customer/{customer}/edit}」にGETでアクセスするとルーティングで「CustomerController@edit」が呼ばれ編集画面が表示されます。
編集画面から「/customer/{customer} 」にPUTでデータを送るルーティングで「CustomerController@update」が呼ばれてデータのアップデータがされます。
HTMLのformからはPUTメソッドが呼べないので実際には以下のようにします。
<form action="/customer/{{$customer->id}}" method="post">
<input type="hidden" name="_method" value="put">
ビュー
顧客情報を編集する画面のビューを作成します。「resources/views/customer/edit.blade.php」
@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">{{ __('Custeomer Edit') }}</div>
<div class="card-body">
<form action="/customer/{{$customer->id}}" method="post">
<input type="hidden" name="_method" value="put">
@csrf
<div class="form-group row">
<label for="country" class="col-md-4 col-form-label text-md-right">{{ __('country') }} :</label>
<div class="col-md-6">
<input id="country" type="text" class="form-control{{ $errors->has('country') ? ' is-invalid' : '' }}" name="country" value="{{ old('country')? old('country') : $customer->country }}" required autofocus>
@if ($errors->has('country'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('country') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="postcode" class="col-md-4 col-form-label text-md-right">{{ __('postcode') }} :</label>
<div class="col-md-6">
<input id="postcode" type="text" class="form-control{{ $errors->has('postcode') ? ' is-invalid' : '' }}" name="postcode" value="{{ old('postcode')? old('postcode') : $customer->postcode }}" required autofocus>
@if ($errors->has('postcode'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('postcode') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="prefecture" class="col-md-4 col-form-label text-md-right">{{ __('prefecture') }} :</label>
<div class="col-md-6">
<input id="prefecture" type="text" class="form-control{{ $errors->has('prefecture') ? ' is-invalid' : '' }}" name="prefecture" value="{{ old('prefecture')? old('prefecture') : $customer->prefecture }}" required autofocus>
@if ($errors->has('prefecture'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('prefecture') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">{{ __('city') }} :</label>
<div class="col-md-6">
<input id="city" type="text" class="form-control{{ $errors->has('city') ? ' is-invalid' : '' }}" name="city" value="{{ old('city')? old('city') : $customer->city }}" required autofocus>
@if ($errors->has('city'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('city') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="ward" class="col-md-4 col-form-label text-md-right">{{ __('ward') }} :</label>
<div class="col-md-6">
<input id="ward" type="text" class="form-control{{ $errors->has('ward') ? ' is-invalid' : '' }}" name="ward" value="{{ old('ward')? old('ward') : $customer->ward }}" required autofocus>
@if ($errors->has('ward'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('ward') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="streetAddress" class="col-md-4 col-form-label text-md-right">{{ __('streetAddress') }} :</label>
<div class="col-md-6">
<input id="streetAddress" type="text" class="form-control{{ $errors->has('streetAddress') ? ' is-invalid' : '' }}" name="streetAddress" value="{{ old('streetAddress')? old('streetAddress') : $customer->streetAddress }}" required autofocus>
@if ($errors->has('streetAddress'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('streetAddress') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="secondaryAddress" class="col-md-4 col-form-label text-md-right">{{ __('secondaryAddress') }} :</label>
<div class="col-md-6">
<input id="secondaryAddress" type="text" class="form-control{{ $errors->has('secondaryAddress') ? ' is-invalid' : '' }}" name="secondaryAddress" value="{{ old('secondaryAddress')? old('secondaryAddress') : $customer->secondaryAddress }}" required autofocus>
@if ($errors->has('secondaryAddress'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('secondaryAddress') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="company" class="col-md-4 col-form-label text-md-right">{{ __('company') }} :</label>
<div class="col-md-6">
<input id="company" type="text" class="form-control{{ $errors->has('company') ? ' is-invalid' : '' }}" name="company" value="{{ old('company')? old('company') : $customer->company }}" required autofocus>
@if ($errors->has('company'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('company') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="userName" class="col-md-4 col-form-label text-md-right">{{ __('userName') }} :</label>
<div class="col-md-6">
<input id="userName" type="text" class="form-control{{ $errors->has('userName') ? ' is-invalid' : '' }}" name="userName" value="{{ old('userName')? old('userName') : $customer->userName }}" required autofocus>
@if ($errors->has('userName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('userName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="domainName" class="col-md-4 col-form-label text-md-right">{{ __('domainName') }} :</label>
<div class="col-md-6">
<input id="domainName" type="text" class="form-control{{ $errors->has('domainName') ? ' is-invalid' : '' }}" name="domainName" value="{{ old('domainName')? old('domainName') : $customer->domainName }}" required autofocus>
@if ($errors->has('domainName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('domainName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('email') }} :</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email')? old('email') : $customer->email }}" required autofocus>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lastName" class="col-md-4 col-form-label text-md-right">{{ __('lastName') }} :</label>
<div class="col-md-6">
<input id="lastName" type="text" class="form-control{{ $errors->has('lastName') ? ' is-invalid' : '' }}" name="lastName" value="{{ old('lastName')? old('lastName') : $customer->lastName }}" required autofocus>
@if ($errors->has('lastName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lastName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="firstName" class="col-md-4 col-form-label text-md-right">{{ __('firstName') }} :</label>
<div class="col-md-6">
<input id="firstName" type="text" class="form-control{{ $errors->has('firstName') ? ' is-invalid' : '' }}" name="firstName" value="{{ old('firstName')? old('firstName') : $customer->firstName }}" required autofocus>
@if ($errors->has('firstName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lastKanaName" class="col-md-4 col-form-label text-md-right">{{ __('lastKanaName') }} :</label>
<div class="col-md-6">
<input id="lastKanaName" type="text" class="form-control{{ $errors->has('lastKanaName') ? ' is-invalid' : '' }}" name="lastKanaName" value="{{ old('lastKanaName')? old('lastKanaName') : $customer->lastKanaName }}" required autofocus>
@if ($errors->has('lastKanaName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lastKanaName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="firstKanaName" class="col-md-4 col-form-label text-md-right">{{ __('firstKanaName') }} :</label>
<div class="col-md-6">
<input id="firstKanaName" type="text" class="form-control{{ $errors->has('firstKanaName') ? ' is-invalid' : '' }}" name="firstKanaName" value="{{ old('firstKanaName')? old('firstKanaName') : $customer->firstKanaName }}" required autofocus>
@if ($errors->has('firstKanaName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('firstKanaName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="phoneNumber" class="col-md-4 col-form-label text-md-right">{{ __('phoneNumber') }} :</label>
<div class="col-md-6">
<input id="phoneNumber" type="text" class="form-control{{ $errors->has('phoneNumber') ? ' is-invalid' : '' }}" name="phoneNumber" value="{{ old('phoneNumber')? old('phoneNumber') : $customer->phoneNumber }}" required autofocus>
@if ($errors->has('phoneNumber'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('phoneNumber') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="realText" class="col-md-4 col-form-label text-md-right">{{ __('realText') }} :</label>
<div class="col-md-6">
<input id="realText" type="text" class="form-control{{ $errors->has('realText') ? ' is-invalid' : '' }}" name="realText" value="{{ old('realText')? old('realText') : $customer->realText }}" required autofocus>
@if ($errors->has('realText'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('realText') }}</strong>
</span>
@endif
</div>
</div>
<div class="row mb-0 btn-toolbar">
<div class="btn-group offset-md-4">
<button type="submit" class="btn btn-default" onclick="location.href='/customer/{{$customer->id}}'">
{{ __('Back') }}
</button>
<button type="submit" class="btn btn-success ml-5">
{{ __('Edit') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
コントローラ
editはクライアント情報を取得してビューを呼出しています。「app/Http/Controllers/CustomerController.php」
public function edit($id)
{
$customer = Customer::find($id);
return view('customer.edit', ['customer' => $customer]);
}
編集はupdate内に記述します。
public function update(Request $request, $id)
{
$customer = Customer::find($id);
$customer->country = $request->country;
$customer->postcode = $request->postcode;
$customer->prefecture = $request->prefecture;
$customer->city = $request->city;
$customer->ward = $request->ward;
$customer->streetAddress = $request->streetAddress;
$customer->secondaryAddress = $request->secondaryAddress;
$customer->company = $request->company;
$customer->userName = $request->userName;
$customer->domainName = $request->domainName;
$customer->email = $request->email;
$customer->lastName = $request->lastName;
$customer->firstName = $request->firstName;
$customer->lastKanaName = $request->lastKanaName;
$customer->firstKanaName = $request->firstKanaName;
$customer->phoneNumber = $request->phoneNumber;
$customer->realText = $request->realText;
// 保存
$customer->save();
// 詳細ページへリダイレクト
return redirect("/customer/".$id);
}
削除機能を追加する
削除機能を作成します。「/customer/{customer}}」をDELETEでアクセスするとルーティングで「CustomerController@destroy」が呼ばれデータを削除します。
HTMLのformではdleteメソッドが呼ぶことができないので、以下のようにします。
<form action="/customer/{customer}" method="post">
<input type="hidden" name="_method" value="delete">
コントローラ
削除はdestory内に記述します。public function destroy($id)
{
$customer = Customer::find($id);
// 保存
$customer->delete();
// 詳細ページへリダイレクト
return redirect("/customer/");
}
参考
https://minory.org/laravel-admin.html