実現したいこと
チェックされたら、true。
未チェックならfalseにしたいです。
発生している問題・分からないこと
breezeに項目(チェックボックス)を追加したのですが、チェック・未チェックに関わらずDBにゼロが格納されます。
該当のソースコード
```コンポーネント(input-checkbox.blade.php) @props(['label']) <label {{ $attributes->merge(['class' => 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm']) }}> <input type="checkbox" id="{{ $attributes['id'] }}"> {{ $label }} </label>
register.blade.php
1<div class="mt-4"> 2 <x-input-label for="authority" :value="__('authority')" /> 3 <x-input-checkbox name="administrator" id="administrator" class="block mt-1" :label="'管理者権限'" /> 4 <x-input-error :messages="$errors->get('administrator')" class="mt-2" /> 5</div>
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 'administrator', 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 48 public function workplace() 49 { 50 return $this->belongsTo(Workplace::class); 51 } 52} 53```RegisteredUserController.php 54<?php 55 56namespace App\Http\Controllers\Auth; 57 58use App\Http\Controllers\Controller; 59use App\Models\User; 60use App\Providers\RouteServiceProvider; 61use Illuminate\Auth\Events\Registered; 62use Illuminate\Http\RedirectResponse; 63use Illuminate\Http\Request; 64use Illuminate\Support\Facades\Auth; 65use Illuminate\Support\Facades\Hash; 66use Illuminate\Validation\Rules; 67use Illuminate\View\View; 68use App\Models\Workplace; 69 70class RegisteredUserController extends Controller 71{ 72 /** 73 * Display the registration view. 74 */ 75 public function create(): View 76 { 77 $workplaces = Workplace::select('id', 'name')->get(); 78 return view('auth.register', compact('workplaces')); 79 } 80 81 /** 82 * Handle an incoming registration request. 83 * 84 * @throws \Illuminate\Validation\ValidationException 85 */ 86 public function store(Request $request): RedirectResponse 87 { 88 $request->validate([ 89 'name' => ['required', 'string', 'max:255'], 90 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class], 91 'password' => ['required', 'confirmed', Rules\Password::defaults()], 92 'workplace_id' => ['required', 'numeric'], 93 ]); 94 95 \Log::info($request->input('administrator')); 96 \Log::info($request->input('administrator') == 'on' ? 'true' : 'false'); 97 98 $user = User::create([ 99 'name' => $request->name, 100 'email' => $request->email, 101 'password' => Hash::make($request->password), 102 'workplace_id' => $request->workplace_id, 103 'administrator' => $request->input('administrator') == 'on', 104 ]); 105 106 event(new Registered($user)); 107 108 Auth::login($user); 109 110 return redirect(RouteServiceProvider::HOME); 111 } 112}
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('workplaces');; 21 $table->boolean('administrator'); 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('administrator'); 35 } 36};
### 試したこと・調べたこと - [x] teratailやGoogle等で検索した - [ ] ソースコードを自分なりに変更した - [ ] 知人に聞いた - [ ] その他 ##### 上記の詳細・結果 chatGPTを使い、コントローラーのリクエストの転送部分をいろいろためしましたが、チェック・未チェックに関わらずゼロになります。 ### 補足 AWSLightsail+Laravel10です。
ちなみに、チェック・未チェックをおりまぜてログをしたのですが・・・
[2024-02-22 10:55:53] local.INFO:
[2024-02-22 10:55:53] local.INFO: false
[2024-02-22 10:57:49] local.INFO:
[2024-02-22 10:57:49] local.INFO: false
[2024-02-22 10:59:10] local.INFO:
[2024-02-22 10:59:10] local.INFO: false
[2024-02-22 10:59:35] local.INFO:
[2024-02-22 10:59:35] local.INFO: false
[2024-02-22 11:04:03] local.INFO:
[2024-02-22 11:04:03] local.INFO: false
[2024-02-22 11:04:31] local.INFO:
[2024-02-22 11:04:31] local.INFO: false
こうなりました。
無理やり
update `laravel`.`users` set administrator = true where id= 13;
としたところ、1になりました。
![guest](/img/icon/icnUserSample.jpg)
回答1件
あなたの回答
tips
プレビュー