質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

192閲覧

Laravel10のbreezeに項目を追加し、入力しているのに、未入力エラーになる

ratezou

総合スコア67

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2024/02/19 03:51

編集2024/02/19 04:19

実現したいこと

追加した項目を含めてusersテーブルに登録したい。

発生している問題・分からないこと

自作のコンポーネントでリストボックスを実装したけど、選択入力しているにもかかわらず、
「**は必須項目です。」となり、登録されない。

該当のソースコード

```RegisteredUserController.php <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Models\User; use App\Providers\RouteServiceProvider; use Illuminate\Auth\Events\Registered; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules; use Illuminate\View\View; use App\Models\Workplace; class RegisteredUserController extends Controller { /** * Display the registration view. */ public function create(): View { $authorities = [ ['id' => '1', 'name' => '一般'], ['id' => '2', 'name' => '管理者'], ]; $workplaces = Workplace::select('id', 'name')->get(); return view('auth.register', compact('workplaces', 'authorities')); } /** * Handle an incoming registration request. * * @throws \Illuminate\Validation\ValidationException */ public function store(Request $request): RedirectResponse { $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class], 'password' => ['required', 'confirmed', Rules\Password::defaults()], 'workplace_id' => ['required', 'integer'], 'admin_flg' => ['required', 'integer'], ]); $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), 'workplace_id' => $request->workplace_id, 'admin_flg' => $request->admin_flg, ]); event(new Registered($user)); Auth::login($user); return redirect(RouteServiceProvider::HOME); } }

input

1@props(['options']) 2 3<select {{ $attributes->merge(['class' => 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm']) }}> 4 <option value="">選択してください</option> 5 @foreach ($options as $option) 6 <option value="{{ $option['id'] }}">{{ $option['name'] }}</option> 7 @endforeach 8</select>

register.blade.phpの一部

1<div class="mt-4"> 2 <x-input-label for="workplace_id" :value="__('workplace_id')" /> 3 <x-input-select :options="$workplaces" class="block mt-1 w-full" /> 4 <x-input-error :messages="$errors->get('workplace_id')" class="mt-2" /> 5</div> 6 7<div class="mt-4"> 8 <x-input-label for="admin_flg" :value="__('admin_flg')" /> 9 <x-input-select :options="$authorities" class="block mt-1 w-full" /> 10 <x-input-error :messages="$errors->get('admin_flg')" class="mt-2" /> 11</div>

2014_10_12_000000_create_users_table.php

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 */ 12 public function up(): void 13 { 14 Schema::create('users', function (Blueprint $table) { 15 $table->id(); 16 $table->string('name'); 17 $table->string('email')->unique(); 18 $table->timestamp('email_verified_at')->nullable(); 19 $table->string('password'); 20 $table->integer('workplace_id')->references('id')->on('workforces');; 21 $table->integer('admin_flg'); 22 $table->rememberToken(); 23 $table->timestamps(); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 */ 30 public function down(): void 31 { 32 Schema::dropIfExists('users'); 33 Schema::dropCloumn('workplace_id'); 34 Schema::dropCloumn('admin_flg'); 35 } 36};

User.php

1<?php 2 3namespace App\Models; 4 5// use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Database\Eloquent\Factories\HasFactory; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8use Illuminate\Notifications\Notifiable; 9use Laravel\Sanctum\HasApiTokens; 10 11class User extends Authenticatable 12{ 13 use HasApiTokens, HasFactory, Notifiable; 14 15 /** 16 * The attributes that are mass assignable. 17 * 18 * @var array<int, string> 19 */ 20 protected $fillable = [ 21 'name', 22 'email', 23 'password', 24 'workplace_id', 25 'admin_flg', 26 ]; 27 28 /** 29 * The attributes that should be hidden for serialization. 30 * 31 * @var array<int, string> 32 */ 33 protected $hidden = [ 34 'password', 35 'remember_token', 36 ]; 37 38 /** 39 * The attributes that should be cast. 40 * 41 * @var array<string, string> 42 */ 43 protected $casts = [ 44 'email_verified_at' => 'datetime', 45 'password' => 'hashed', 46 ]; 47}

input

1@props(['options']) 2 3<select {{ $attributes->merge(['class' => 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm']) }}> 4 <option value="">選択してください</option> 5 @foreach ($options as $option) 6 <option value="{{ $option['id'] }}">{{ $option['name'] }}</option> 7 @endforeach 8</select>

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

https://qiita.com/csigesnpb/items/5cad27e06a0cb5e427fb
この記事に沿って作業を進めました。
(テーブルカラムが追加されているのは確認しました)
updateのコードはまだ書いていません。

補足

AWSLightsail+Laravel10です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ratezou

2024/02/19 04:12

validationを 'workplace_id' => ['required', 'numeric'], 'admin_flg' => ['required', 'numeric'], としてもダメでした。
tabuu

2024/02/19 05:32

inputのselectにname属性は設定されていますか?
guest

回答1

0

自己解決

ご指摘の通りname属性を設定したところうまく行きました。
ありがとうございます。
<x-input-select :options="$workplaces" name="workplace_id" class="block mt-1 w-full" />

投稿2024/02/19 23:50

編集2024/02/19 23:52
ratezou

総合スコア67

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問