Laravel

編集するファイルの在りか

ログイン後の画面app/Providers/RouteServiceProvide.php メニューの追加resources/views/layoutsの中のnavigation.blade.php ログインと登録ページへのリンクを追加resources/views/layouts/guest.blade.php ログイン画面・登録画面のロゴresources/views/c…

投稿の削除

【view】 <x-primary-button class="bg-red-700 float-right ml-4" onClick="return confirm('本当に削除しますか?');">削除</x-primary-button> 【Controller】 public function destroy(Post $post) { $post->delete(); return redirect()->route('post.index')->with('message', '投稿を削除しました'); }

データベース

●データベースの中から、条件に合うデータを取得する モデル名::where('テーブルのカラム名', 条件)->get(); 例) $posts=Post::where('user_id', $user)->get();

全文表示させない

{{$post->body}}↓{{Str::limit($post->body, 100, '...')}}

アプリ名を変更

envファイルの1行目の APP_NAME=の後を変更する。

画像の挿入

<img src="{{asset('○○/○○.png')}}" style="max-height:80px;"> asset関数を使う。asset関数を用いることで、publicにアクセスできる。

Componentの作成

Componentの作成:php artisan make:component コンポーネント名 ☆活用例 【app/View/Components/コンポーネント名.php】classコンポーネント名 extends Component{ public $○○; public function __construct($○○) { $this->○○ = $○○; } 【resources/views/c…

日付表示

○スラッシュで区切る{{$post->created_at->format('Y/m/d')}} ○日本語表示{{$post->created_at->format('Y年m月d日')}} ちなみに、 データを日付として表示すにには、date型にして保存しておく。 マイグレーションファイルで、あらかじめdate型に設定してお…

バリデーション

readouble.com ○例 'title'=>'required|max:255','body'=>'required|max:1000', 'image'=>'image|max:1024' ○'end_date' => 'after:start_date' 'end_date' フィールドの値は、'start_date' フィールドの値の後になる。例えば、終了日が開始日の後になるかど…

Attempt to read property "name" on null

resources/views/layouts/app.blade.php 【navigation.blade.php】Auth:: で始まるコード3か所を以下のように修正 <div>@if(Auth::check()) {{ Auth::user()->name }} @endif</div> <div class="font-medium text-base text-gray-800">@if(Auth::check()) {{ Auth::user()->name }} @endif</div>

リソースコントローラー

Route::resource('post', PostController::class);↓ Route::get('post', [PostController::class, 'index'])->name('post.index');Route::get('post/create', [PostController::class, 'create'])->name('post.create');Route::post('post', [PostController…

migrate操作一覧

《基本の流れ》 【ターミナル】 php artisan make:migration add_column_カラム名_to_テーブル名_table --table=テーブル名 【マイグレーションファイル】 public function up() { と public function down() { に希望の操作を記述 【ターミナル】 php artis…

ロゴの変更

①public/img内にロゴを配置 ②resources/views/layouts/navigation.blade.php コメントを探す ③ロゴのリンク先変更とコードの変更追加 <a hret ="{{ route('○○.index') }}"> <x-application-logo width="30" class="block text-gray-800" /> ④resources/views/auth/login.blade.php <x-guest-layout>とある ⑤resources/views/layouts/guest.blade.php…</x-guest-layout></x-application-logo></a>

ダミーデータを作る

【config/app.php】 'faker_locale' => 'ja_JP', 【ターミナル】 php artisan make:factory ○○Factory --model=○○ 【database/factories/YoyakuFactory.php】 例)use App\Models\User; public function definition() { return [ '○○'=>$this->faker->dateTi…

ペジネーション

【Controller.php】 public function index() { $this->authorize('admin'); // 変更箇所 $○○=○○::orderBy('date1', 'Desc')->paginate(10); return view('○○.index', compact('○○')); } 【index.blade.php】 {{ $○○->links() }} ※デザインに手を加えたい場…

カレンダーで来月の初めから再来月の終わりまで表示

【Controller】use Carbon\Carbon; public function create() { // 追加 $min=Carbon::now()->addMonthsWithOverflow(1)->firstOfMonth()->toDateString(); $max=Carbon::now()->addMonthsWithOverflow(2)->endOfMonth()->toDateString(); if(auth()->user()…

return redirect()->route()とreturn view()の違いについて

return view()を使用すると、 ルーティングを通さずに直接指定したビューにアクセスできます。POSTリクエストで渡されたパラメータは次のビューにそのまま渡される。 redirect()を使用すると、 ルーティングを通してビューにアクセスする。リダイレクト先の…

ログイン後、役割に応じてページを変更

【routes/web.php】 // Route::get('/dashboard', function () {// return view('dashboard');// })->middleware(['auth'])->name('dashboard'); Route::get('/dashboard', [○○Controller::class, 'dashboard'])->middleware(['auth'])->name('dashboard'); …

改行が反映されるようにする

【show.blade.php】 <p>{!! nl2br(e($post->body)) !!}</p> 参考:ドットインストール『Laravel 8入門 CRUD処理編』

独自クラスを作り、重複コードをなくす

./vendor/bin/sail artisan make:request ○○Request 【○○Request.php】 public function authorize() { return true; } public function rules() { return [ 'title' => 'required|min:3', 'body' => 'required', ]; } public function messages() { return …

入力した値を検証

【Controller.php】 public function store(Request $request) { $request->validate([ 'title' => 'required|min:3', 'body' => 'required', ], [ 'title.required' => 'タイトルは必須です', 'title.min' => ':min 文字以上入力してください', 'body.requi…

削除前にダイアログ表示

【show.blade.php】①<form method="post" action="{{ route('posts.destroy', $post) }}" id="delete_post"> ② <script> 'use strict'; { document.getElementById('delete_post').addEventListener('submit', e => { e.preventDefault(); if (!confirm('Sure to delete?')) { return; } e.target.submit(); }); } </script></form>

削除

【Controller.php】 public function destroy(Post $post) { $post->delete(); return redirect() ->route('posts.index'); } 【show.blade.php】 <form method="post" action="{{ route('posts.destroy', $post) }}"> @method('DELETE') @csrf 【routes/web.php】Route::delete('/posts/{post}/destroy', [PostController::cla</form>…

編集

【show.blade.php】 <h1> <span>{{ $post->title }}</span> <a href="{{ route('posts.edit', $post) }}">[Edit]</a> </h1> 【edit.blade.php】<form method="post" action="{{ route('posts.update', $post) }}"> @method('PATCH') <input type="text" name="title" value="{{ old('title', $post->title) }}"></form>

保存

【create.blade.php】<form method="post" action="{{ route('posts.store') }}"> 【Controller.php】 public function store(Request $request) { $post = new Post(); $post->title = $request->title; $post->body = $request->body; $post->save(); return redirect() ->route('posts.index'); } 【routes/web.php</form>…

CSRF対策

<form method="post" action=""> @csrf 参考:ドットインストール『Laravel 8入門 CRUD処理編』</form>

パラメーターとして数値しか受け付けないという制限をつける

Route::get('/posts/{post}', [PostController::class, 'show']) ->name('posts.show') ->where('post', '[0-9]+'); 参考:ドットインストール『Laravel 8入門 CRUD処理編』

ログイン後のページ遷移(Breeze)

○Breezeを使用した場合ログイン後のページ遷移はapp/Providers/RouteServiceProvider.phpで変更できる。 例) public const HOME = '/dashboard'; ↓public const HOME = '/club';" ○ログイン後、役割に応じでページ遷移を変更したい場合例)[web.php]Route::…

Implicit Binding

【view】→モデルのデータを直接渡せば、自動的に適当なidがセットされる <a href="{{ route('posts.show', $post->id) }}"> ↓ <a href="{{ route('posts.show', $post) }}"> 【/routes/web.php】 Route::get('/posts/{id}', [PostController::class, 'show'])->name('posts.index'); ↓ Route::get('/posts/{post}', [PostController::class, 'show'])->na</a></a>…

tinkerとmysql

【tinker】./vendor/bin/sail tinkerパターン①$post = new App\Models\Post();$post->title = '○○';$post->body = '○○';$post->save();exit; パターン②App\Models\Post::create(['title'=>'○○','body'=>'○○']);App\Models\Post::all();App\Models\Post::find…