2023-06-29から1日間の記事一覧

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…

public フォルダからのパスを使って URL を指定する

<link rel="stylesheet" href="css/style.css">↓<link rel="stylesheet" href="{{ url('css/style.css') }}"> 参考:ドットインストール「Laravel 8入門 基本機能編」</link></link>

共通部分を切り出す

【/components/layout.blade.php】<html lang="ja"><head> <meta charset="utf-8"> <title>{{ $title }}</title> <link rel="stylesheet" href="{{ url('css/style.css') }}"></head><body> <div class="container"> {{ $slot }} </div></body></html> 【/views/index.blade.php】<x-layout> </x-layout>

ルーティングに名前を付ける

【web.php】例)Route::get('/', [PostController::class, 'index']) ->name('posts.index'); Route::get('/posts/{id}', [PostController::class, 'show']) ->name('posts.show'); 【view】例) &laquo; <a href="/">Back</a>↓ &laquo; <a href="{{ route('posts.index') }}">Back</a> <a href="/posts/{{ $index }}">↓ </a>

ページリンクを作る

①Controllerで配列データをwithで渡す例) private $posts = [ 'Title A', 'Title B', 'Title C', ]; public function index() { return view('index') ->with(['posts' => $this->posts]); } ②viewで受け取ったデータを元にリンクを生成例)@forelse ($post…