Skip to content

Instantly share code, notes, and snippets.

@yano
Last active January 21, 2018 05:45
Show Gist options
  • Save yano/fd55282f38b049e0c9c8c6ee804d2923 to your computer and use it in GitHub Desktop.
Save yano/fd55282f38b049e0c9c8c6ee804d2923 to your computer and use it in GitHub Desktop.
PHPはじめてのフレームワーク Laravel 5.5対応 ステップ1 の私的まとめ

asset()

<script srt="{{asset('/js/***.js')}}"></script> -> public/js/***.js

migrationファイルの$tableカラムタイプ一覧

タイプ一覧

$table->increments('id'); //ID 自動採番(主キー)

$table->string('email');      // VARCHARカラム
$table->string('name', 100);  // VARCHAR長さ指定カラム
$table->integer('price');     // INTEGERカラム
$table->text('description');  // TEXTカラム
$table->dateTime('created_at'); // DATETIMEカラム
$table->boolean('confirmed'); // created_atとupdate_atのカラムの追加
$table->json('meta');         // JSONカラム

$table->timestamps();

オプション

// NULLの許可
$table->string('email')->nullable();

// 一意の値に限定する
$table->string('email')->unique();

bladeの話

Laravelの変数の参照

{{ $val_of_laravel }}

Vue.jsなどを利用する場合

@{{ val_of_vue }}

formエラー時に、古い入力内容を残す方法

コントローラ

public function somefunc(Request $request)
{
    // Validation
    $validator = Validator::make($request->all(),[
        'item_name' => 'required|max:5',
    ]);

    // Error時の処理
    if ($validator->fails()){
        return redirect('/')
            ->withInput()
            ->withErrors($validator);
    }
}

ビュー

        {{--Book登録フォーム--}}
        <form action="{{ url('/books') }}" method="POST" class="form-horizontal">
            {{ csrf_field() }}

            {{--Bookのタイトル--}}
            <div class="form-group">

                <label for="book" class="col-sm-3 control-label">
                    Book
                </label>

                <div class="col-sm-6">
                    <input type="text"
                           name="item_name" 
                           id="book-name"
                           class="form-control"
                           value="{{ old('item_name') }}"> # これ。nameの値と一致させる。
                </div>

            </div>

            {{--Book登録ボタン--}}
            <div class="form-group">
                <div class="col-sm-offset-3 col-sm-6">
                    <button type="submit" class="btn btn-default">
                        <i class="glyphicon glyphicon-plus"></i> Save
                    </button>
                </div>
            </div>

        </form>

bladeでのログインチェック

@if (Auth::check())
    ログインしています。
@else
    ログインしていません。
@endif

make:auth 導入

php artisan make:auth
php artisan migrate # laradockのworkspaceで

コントローラー単位で認証後にページを表示するように設定するには、コントローラーに以下のように記載する。

public function __construct()
{
    $this->middleware('auth');
}

ユーザ登録後にリダイレクトするページを設定。

#変更前
Route::get('/home', 'HomeController@index')->name('home');

# 変更後
Route::get('/home', 'BooksController@index')->name('home');

bladeファイルを日本語に切り替える

  • login.blade.php
  • register.blade.php

Userの参照

Auth::user->name
auth()->user->name

Eloquentモデルのtinker上での操作

tinker開始

php artisan tinker
$all = App\Book::all();

$item_number1 = App\Book::where('item_number', 1)->get();

$item_amount1000 = App\Book::where('item_amount', 1000)->get();

$get = App\Book::where('item_number', 1)
    ->orderBy('id', 'desc')
    ->take(10)
    ->get();
    
$get = App\Book::find(1);

$get = App\Boo::where('id', 1)->first();

便利なツール CRUD自動生成 Scaffold

便利なツール Debugツール laravel-debugbar

参考

Kindle Unlimitedで読める。

https://www.amazon.co.jp/gp/product/B06XR2LRZK/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment