はじめに
・**Laravelを初めて触る方。
・Laravel**でのエラーに遭遇している方。
※Laravelを使用するにあたり、
最低限のMySQLの知識を記載しています。
Laravelとは
・2011年にリリースされた、
**PHPのWebフレームワーク**です。
・**ルーティング、コントローラ、ビュー**などの、
基本的な機能を備えています。
※ルーティング、コントローラ、ビューは後程紹介します。
・**Composer**を使用して、ライブラリ管理が可能です。
※この記事では、Composerを使用したインストールになります。
では、早速インストールしていきましょう。
Composerのインストール
**Composer**は、PHPのライブラリ管理ツールです。
※依存関係にあるパッケージやライブリを、
一括インストールしてくれます。
こちらからダウンロード
macの場合はHomeBrewでインストール可能
コマンド: brew install composer
Laravelのインストール
create-projectコマンドで、
インストールと同時にプロジェクトを作成されます。
% composer create-project laravel/laravel laravel-sample
※laravel-sampleという、ディレクトリが作成されます。
cdコマンドでlaravel-sampleまで移動してください。
% cd laravel-sample
DBの設定(MySQL)
まず、**MySQL**がインストールされているか確認。
確認コマンドmysql --version
※**MySQL**がインストールされている場合は
バージョンが表示される。
筆者の場合は以下のようになる↓
% mysql --version
mysql Ver 8.0.23 for osx10.16 on x86_64 (Homebrew)
・今回使用するコマンド一覧
| コマンド | 詳細 |
|---|---|
| mysql.server start | MySQLサーバーの起動 |
| mysql.server stop | MySQLサーバーの停止 |
| mysql -u root -p | rootユーザーでログイン |
| mysql -u ユーザー名 -p | 指定したユーザーでログイン |
| create database DB名; | DBの作成 |
| show databases; | DBの確認 |
| show tables from DB名; | テーブルの確認 |
| create user 'ユーザ名'@'localhost' identified by 'パスワード'; | ユーザーの作成 |
| select host, user from mysql.user; | ユーザーの確認 |
| grant all DB名.* to ユーザー名@ホスト名; | DBレベルでの権限付与 |
| show grants for ユーザー名@localhost; | ユーザーの権限の確認 |
※テーブルの作成は、マイグレーションで行います。
・ユーザーの作成
今回は以下のように設定。
↓
| DB | ユーザー名 |
|---|---|
| hogedb | hoge_user |
では、やっていきましょう。
mysql.server startコマンドを実行。
Starting MySQL . SUCCESS! がでればOK。
・DBに接続
% mysql -u root -p
Enter password: // ここでパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.23 Homebrew
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
・ユーザーの作成と確認
ユーザー名は**hoge_user**、パスワードは各自で設定してください。
mysql> create user 'hoge_user'@'localhost' identified by 'パスワード';
mysql> select host, user from mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | hoge_user |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
・DBの作成と確認
mysql> create database hogedb;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hogedb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
・ユーザーへの権限付与
mysql< grant all on hogedb.* to hoge_user@localhost;
※ここで、**mysql> exitコマンドで一旦root**ユーザーから抜けてください。
先程作成した**hoge_user**でDBに接続します。
% mysql -u hoge_user -p
※rootで接続した時と同じように、
設定したパスワードを入力します。
mysql> show grants for hoge_user@localhost;
+---------------------------------------------------------------+
| Grants for hoge_user@localhost |
+---------------------------------------------------------------+
| GRANT USAGE ON *.* TO `hoge_user`@`localhost` |
| GRANT ALL PRIVILEGES ON `hogedb`.* TO `hoge_user`@`localhost` |
+---------------------------------------------------------------+
2 rows in set (0.00 sec)
最後に**mysql> exit**コマンドの後、
% mysql.server stopでDBサーバーとの接続を切ります。
※今回の場合は、hoge_userにDBレベルでの権限付与です。
その他の権限付与についてはこちら
・show tablesだとエラーになる?
※MySQLでログイン後、
**show tables**だとエラーになり、
**ERROR 1046 (3D000): No database selected**となる。
これは、DBを指定しないため起きる。
**use**コマンド→ **show tablses**で解決。
また、show tables from DB名;
の場合は**use**コマンドを使用しなくてOK。
環境設定ファイルの設定
環境設定設定は**.envファイル**に記載します。
ここでは、DBの接続情報を設定します。
※**.envファイルはlaravle-sample**ディレクト直下に存在。
viコマンドで編集。
※viについてはこちら
% vi .env
今回編集したい場所↓
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
キーボードの**i**を押して編集画面へ。
※先ほど作成しDBを元に、
以下のように書き換え↓
DB_DATABASE=laravel → DB_DATABASE=hogedb
DB_USERNAME= → DB_USERNAME=hoge_user
DB_PASSWORD= → DB_PASSWORD=各自で設定したパスワード
**esc**を押し、編集画面を終了。
**:wq!を入力し、Entner**で編集内容を保存。
マイグレーション
**マイグレーション**とは、テーブルの操作などをSQL文ではなく、
プログラミング言語で管理する仕組みです。(今回の場合はPHP)
・マイグレーションファイルの作成
マイグレーションファイルの作成には
**Artisanコマンド**の
**make:migration**を使用します。
% php artisan make:migration create_clients_table --create=clients
Created Migration: 2021_03_04_063231_create_clients_table
これで**/database/migrations**配下に、マイグレーションファイルが作成されます。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('clients');
}
}
・マイグレーションファイルの詳細
| メソッド名 | 役割 |
|---|---|
| up | テーブル作成時の情報 |
| down | マイグレーション取り消し時の情報 |
次に、**upメソッド**を書き換えます。
書き換えるにあたり、
以下のような、テーブルを作成します。
| カラム | 型 | 制約 |
|---|---|---|
| id | AUTO_INCREMENT | PRIMARY KEY |
| inc | VARCHAR(50) | NOT NULL |
| adress | VARCHAR(50) | NOT NULL |
| capital | INT | NOT NULL |
| ceo | VARCHAR(50) | NOT NULL |
| created_at | timestamp | |
| updated_at | timestamp |
書き換え後↓
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('inc', 50);
$table->string('adress', 50);
$table->integer('capital');
$table->string('ceo', 50);
$table->timestamps();
});
}
・マイグレーションの実行
実行には**Artisanコマンドのmigrateコマンド**を使用します。
% php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (15.31ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (8.72ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (9.54ms)
Migrating: 2021_03_04_063231_create_clients_table
Migrated: 2021_03_04_063231_create_clients_table (6.60ms)
・カラムのデータ型について
・increments(id)...「INT」を使用した、自動増分ID
・string('カラム名', '長さ')... 長さ指定の「VARCHAカラム」
・integer('カラム名')... 数値データカラム
※「VARCHAカラム」は、可変長の文字列を意味するデータ型です。
モデルの作成
モデルはテーブルとマッピングされたオブジェクトで、
DBの操作が可能なクラスになります。
※モデルは**/app/Models**直下に存在。
**Artisanコマンドのmake:model**で作成します。
% php artisan make:model Client
Model created successfully.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use HasFactory;
}
・モデルの命名規則
テーブル名を単数形にしたものが**Model名**となります。
clients(テーブル名) → Clinent(モデル名)
と、いった感じです。
自動的にテーブルを操作することが可能です。
※Modelsはバージョンによって、
手動で追加する必要があるみたいなので注意が必要。
シティーング
**シーティング**とは、
テストデータやマスタデータなどの必要なレコードを
コマンドで登録する仕組みになります。
・シーティングファイルの作成
Artisanコマンドの**make:seeder**を使用します。
% php artisan make:seeder ClientsTableSeeder
% Seeder created successfully.
※シーティングファイルは**database/seeds**直下に存在。
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class ClientsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
次に、**upメソッド**を書き換えます。
書き換えるにあたり、
以下のような、レコードを登録します。
|ID|INC|ADRESS|CAPITAL|CEO|CREATED_AT|UPDATED_AT|
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|1|株式会社テック|東京都〇〇区1丁目の1番地|10000000|伊藤一郎|現在時刻|現在時刻|
|2|株式会社ハイテクソリューション|東京都△△区3丁目の13番地|13000000|鈴木修|現在時刻|現在時刻|
|3|株式会社会社丸井システム|東京都□□区5丁目の26番地|26000000|丸井慎二|現在時刻|現在時刻|
書き変え後↓
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB; // ←これを追加
class ClientsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// テーブルのクリア
DB::table('clients')->truncate();
// 初期データ用意(列名をキーとする連想配列)
$clients = [
['inc' => '株式会社テック',
'adress' => '東京都〇〇区1丁目の1番地',
'capital' => 10000000,
'ceo' => '伊藤一郎'],
['inc' => '株式会社ハイテクソリューション',
'adress' => '東京都△△区3丁目の13番地',
'capital' => 113000000,
'ceo' => '鈴木修'],
['inc' => '株式会社会社丸井システム',
'adress' => '東京都□□区5丁目の26番地',
'capital' => 26000000,
'ceo' => '丸井慎二']
];
// 登録
foreach($clients as $client) {
\App\Models\Client::create($client);
}
}
}
Seeding: Database\Seeders\ClientsTableSeeder
Seeded: Database\Seeders\ClientsTableSeeder (49.20ms)
Database seeding completed successfully.
テーブルの中身を確認↓
mysql> use
mysql> select * from clients;
ルーティング
**ルーティングとは、
クライアントのリクエストによって
処理を振り分ける仕組みです。
※ルーティングは、routes/web.php**ファイルに記述します。
コードで詳細を見ていきましょう。
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
デフォルトでルーティングが定義されています。
この場合、**ルート(/)にGETリクエスト**がきたら
welcomeというレスポンスを返す意味になります。
※welcome→/resources/views/welcome.blade.php
詳しく説明すると、
getメソッドの第1引数にはURI、**第2引数にはクロージャ(無名関数)**を定義します。
Route::get('/', function () { return view('welcome'); });
と定義されているので、
getメソッドを呼び出しルート(/)(第1引数)にGETがきたら、
クロージャ(function( ))(第2引数)を実行するので、
**return view(welcome)のwelcome**を返すということになります。
では、viewを見てましょう。
**Artisanコマンドのphp artisan serve**を実行してください。
% php artisan serve
以下にアクセスしてください
↓
http://localhost:8000/
/resources/views/welcome.blade.php
の内容が表示されると思います。
※デフォルトのルーティンでは
直接viewを返していましたが、
以降はコントローラを経由したルーティンを作成します。
コントローラの作成
コントローラはルーティングされたリクエストを受け取り、
レスポンスを作成する役割になります。
**HTMLを返す場合はview**に処理を依頼します。
**Artisanコマンドのmake:controllerで作成します。
※オプションで--resource**を使用。
**--resource**オプションを使用することで、
**CRUD機能を搭載したRESTful**なメソッドが生成される。
※**CRUD機能**を簡単にまとめると
| CDUR | 機能 |
|---|---|
| Create | 新規作成 |
| Read | 読み取り |
| Update | 更新 |
| Destroy | 消去 |
**--resource**で生成されるメソッドの詳細
| メソッド名 | 役割 |
|---|---|
| index | 一覧画面の表示 |
| show | 詳細画面の表示 |
| create | 登録画面の表示 |
| store | 編集画面の表示 |
| edit | update編集処理 |
| destroy | 消去処理 |
% php artisan make:controller ClientsController --resource
Controller created successfully.
**コントローラ**は、
**app/Http/Controllers**ディレクトリに作成されます。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ClientController 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)
{
//
}
}
※**ClientsController.php **は、後ほど編集。
Restfulリソースコントローラ
**--resource**で作成したコントローラを
経由してルーティングします。
**routes/web.php**を編集していきます。
以下のように編集↓
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ClientController;
Route::resource('/client', 'App\Http\Controllers\ClientController');
Route::get('/search-result', 'App\Http\Controllers\ClientController@find');
次は先程作成した、
app/Http/Controllers/ClientsController.php
のメソッドの中身を記述していきます。
以下のように書き換え↓
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Client;
use Illuminate\Support\Facades\Schema;
class ClientController extends Controller
{
public function index()
{
//DBよりClientテーブルの値を全て取得
$clients = Client::all();
// 取得した値をビュー 「client/indexに値を渡す」
return view('client/index', compact('clients'));
}
public function create()
{
//空の$clientを渡す
$client = new Client();
return view('client/create', compact('client'));
}
public function store(Request $request)
{
// データの保存処理
$client = new Client();
$client->inc = $request->inc;
$client->adress = $request->adress;
$client->capital = $request->capital;
$client->ceo = $request->ceo;
$client->save();
return redirect('/client');
}
public function edit($id)
{
//DBよりURLパラメータと同じIDのClientの情報を取得
$client = Client::findOrFail($id);
// 取得した値をビュー「client/edit」に渡す
return view('client/edit', compact('client'));
}
public function update(Request $request, $id)
{
// データの更新処理
$client = Client::findOrFail($id);
$client->inc = $request->inc;
$client->adress = $request->adress;
$client->capital = $request->capital;
$client->ceo = $request->ceo;
$client->save();
return redirect('/client');
}
public function destroy($id)
{
// データの消去処理
$client = Client::findOrFail($id);
$client->delete('/client');
}
public function find(Request $request)
{
// 検索フォームで入力された値をGETし、DBから値を取得
$search = $request->input('search-result');
$searchs = Client::where('inc', 'like', '%'.$search.'%')->get();
return view('client/find', compact('searchs'));
}
}
※**fidnメソッドは、--resource**では、
作成されないので注意。
ビューの作成
**ビューは、resources/views**に作成されます。
**コントローラで渡された内容をhtml**で表示することができます。
**resources/views**配下に
**clientディレクトリ**を作成してください。
では、**indexメソッドで渡したview**を作成します。
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Laravel Sample</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<div class="container ops-main">
<div class="row">
<div class="col-md-12">
<div class="d-flex justify-content-center">
<h3 class="ops-title">顧客管理</h3>
</div>
<div class="d-flex justify-content-end">
<form action="/search-result/">
<input id="search-input" placeholder="会社名を入力" type="text" name="search-result">
<input id="search-buttom" class="fas" type="submit" value="" method="get">
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-11 col-md-offset-1">
<table class="table text-center">
<tr>
<th class="text-center">ID</th>
<th class="text-center">会社名</th>
<th class="text-center">住所</th>
<th class="text-center">資本金</th>
<th class="text-center">代表取締役</th>
</tr>
@foreach($clients as $client)
<tr>
<td>
<a href="/client/{{ $client->id }}/edit">{{ $client->id }}</a>
</td>
<td>{{ $client->inc }}</td>
<td>{{ $client->adress }}</td>
<td>{{ $client->capital }}</td>
<td>{{ $client->ceo }}</td>
<td>
<form action="/client/{{ $client->id }}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-xs btn-danger" aria-label="Left Align"><span class="glyphicon glyphicon-trash"></span></button>
</form>
</td>
</tr>
@endforeach
</table>
<button type="button" class="btn btn-secondary btn-sm"><a href="/client/create"><font color="white">新規作成</font></a></button>
</div>
</div>
</div>
</html>
**editメソッドで渡したview**を作成します。
※更新画面になります。
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Laravel Sample</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<div class="container ops-main">
<div class="row">
<div class="col-md-6">
<h2>顧客登録</h2>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-1">
<form action="/book/{{ $book->id }}" method="post">
<!-- updateメソッドにはPUTメソッドがルーティングされているのでPUTにする -->
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="inc">会社名</label>
<input type="text" class="form-control" name="inc" value="{{ $client->inc }}">
</div>
<div class="form-group">
<label for="adress">住所</label>
<input type="text" class="form-control" name="adress" value="{{ $client->adress }}">
</div>
<div class="form-group">
<label for="capital">資本金</label>
<input type="text" class="form-control" name="capital" value="{{ $client->capital }}">
</div>
<div class="form-group">
<label for="ceo">代表取締役</label>
<input type="text" class="form-control" name="ceo" value="{{ $book->client }}">
</div>
<button type="submit" class="btn btn-default">登録</button>
<a href="/book">戻る</a>
</form>
</div>
</div>
</div>
</html>
ビューの継承
ビューの継承は親ビューになる**layout.blade.php**を作成し、
共通する要素を書きます。
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Laravel Sample</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
@yield('content')
</html>
**index.blade.php**を書き換えます。
@extends('client/layout');
@section(content);
<div class="container ops-main">
<div class="row">
<div class="col-md-12">
<div class="d-flex justify-content-center">
<h3 class="ops-title">顧客一覧</h3>
</div>
<div class="d-flex justify-content-end">
<form action="/search-result/">
<input id="search-input" placeholder="会社名を入力" type="text" name="search-result">
<input id="search-buttom" class="fas" type="submit" value="" method="get">
</form>
</div>
</div>
</div>
<div class="row">
<!-- 中略 -->
</div>
</div>
@endsection
共通要素の代わりに**@extends('client/layout')を記述します。
これで、layout.blade.php**を継承することを宣言します。
**views/book/layout.blade.php**の
**@yield('content');**部分に、
**@section('content')~@endsection**で囲った部分が
挿入されます。
同じ容量で**edit.blade.php**も継承しましょう。
@extends('client/layout');
@section(content);
<div class="container ops-main">
<div class="row">
<!-- 中略 -->
</div>
</div>
@endsection
CRUDの機能の実装
最後に、コントローラーで実装した**CRUDの機能**を
**viweで表示されるようにします。
画面は、resources/viwes/client/edit.blade.php**を再利用しましょう。
**edit.blade.phpの@section**内部を移動させます。
まずは**resources/views**配下に
**form.blade.php**を作成します。
**target変数にstoreメソッドと、updateメソッド**を渡し、
**@if**で分岐処理をします。
<div class="container ops-main">
<div class="row">
<div class="col-md-6">
<h2>顧客登録</h2>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-1">
@if($target == 'store')
<form action="/client" method="post">
@elseif($target == 'update')
<form action="/client/{{ $client->id }}" method="post">
<input type="hidden" name="_method" value="PUT">
@endif
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="inc">会社名</label>
<input type="text" class="form-control" name="inc" value="{{ $client->inc }}">
</div>
<div class="form-group">
<label for="adress">住所</label>
<input type="text" class="form-control" name="adress" value="{{ $client->adress }}">
</div>
<div class="form-group">
<label for="capital">資本金</label>
<input type="text" class="form-control" name="capital" value="{{ $book->capital }}">
</div>
<button type="submit" class="btn btn-default">登録</button>
<a href="/client">戻る</a>
</form>
</div>
</div>
</div>
次は、**edit.blade.php**を書き換えます。
**@include**を使用して、
**form.blade.phpを@section**内に挿入し、
**updateメソッド**を渡します。
@extends('client/layout')
@section('content')
@include('client/form', ['target' => 'update'])
@endsection
では、最後になります。
**resources/views/client**配下に、
**create.blade.php**を作成します。
**form.blade.phpと同じ容量で
ここでは、storeメソッド**を渡します。
@extends('client/layout')
@section('content')
@include('client/form', ['target' => 'store'])
@endsection
エラーまとめ
・Could not open input file: artisan
・**Artisanコマンドを実行した場所を見直してください。
※artisanディレクトリがある場所で実行しなとエラーになります。
今回の場合は、laravel-sample**ディレクトリになります。
・SQLSTATE[HY000] [2002] Connection refused (SQL: select * from テーブル名)
・**MySQL**が起動してない時に起きるエラーです。
※MySQLを起動してください。
・404エラー
・**ルーティング**の設定を見直してください。
※ルーティングで呼び出している
コントローラのメソッドがない時に起きるので、
**@メソッド**を確認してください。
まとめ
・マイグレーション
テーブルの操作などをSQL文ではなく、
プログラミング言語で管理する仕組み。
・モデル
テーブルとマッピングされたオブジェクトで、
DBの操作が可能なクラス。
・シーティング
テストデータやマスタデータなどの必要なレコードを
コマンドで登録する仕組み。
・ルーティング
クライアントのリクエストによって
処理を振り分ける仕組み。
・コントローラ
ルーティングされたリクエストを受け取り、
レスポンスを作成する役割。
・ビュー
コントローラーで渡された内容をhtmlで表示する。
※余談
モデルを作成する際に、Laravel8だと
**/app/Models/モデル名となるが、
他のバージョンでapp/モデル名**となっている場合があると
見受けられます。
※念入りに調査していないので、
間違っていた場合はコメントまでお願い致します。

