我有一个奇怪的问题,拉拉和复选框。
我有一组复选框,无法在数据库中保存值。
我的迁徙:
$table->json('send_reminder')->nullable();我的模特:
protected $fillable = [
.....
'send_reminder',
....
];我的index.php
类索引扩展组件{ public .$send_reminder,....
我的表格index.blade.php
<div class="input-group input-group-static my-3">
<div class="form-control @error('reminder') is-invalid @enderror">
Notification interval: <br>
@foreach(\App\Enums\ReminderInterval::options() as $option => $reminder)
<label>
<input name="send_reminder[]" value="{{(str_replace('_','',$option))}}"
type="checkbox">
{{(str_replace('_',' ',$option))}}
</label><br>
@endforeach
</div>当我试图将数据保存到变量和DD数据时,字段总是为null。
$data = [
....other data...
'send_reminder' => $this->send_reminder
];
dd($data);你能帮帮我吗?谢谢!
-函数路径的完整代码: app/Http/Livewire/User/Dashboard/Deadlines/Add/Index.php
public function Add(){
$msg = [
'check_reminder' => 'Select Reminder',
'check_reminder.in' => 'Select Reminder',
'note.required' => 'Please add a public note.',
];
$validated = $this->validate([
'customer_id' => 'required|numeric',
'name' => 'required|string',
'date' => 'required|date',
'amount' => array('required','regex:/^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)*$/'),
], $msg);
// max customers with current subscription
$customer_of_user = count(DB::table("customers")->where("user_id", Auth::user()->id)->get());
$allowed_customers = Admin::ProductAllowedCustomers($this->active_subscription->name);
if (!$allowed_customers) {
$this->allowed_deadlines = "unlimited";
}
try {
//MAX Deadlines created about this user
$deadline_of_customer = DB::table('deadlines')
->where('customer_id', $validated['customer_id'])
->where('user_id',Auth::user()->id)
->get();
$deadline_of_customer = count($deadline_of_customer);
//Allowed Deadlines
$allowed_deadlines = Admin::ProductAllowedDeadlines($this->active_subscription->name);
//If allowed customers are unlimited
if ($this->allowed_deadlines == "unlimited" OR $deadline_of_customer < $allowed_deadlines) {
$data = [
'subscription_id' => $this->active_subscription->id,
'user_id' => Auth::user()->id,
'slug' => strtoupper(Str::random(20)),
'renew_state' => $this->renew_state,
'type_of_renew' => $this->type_of_renew,
'send_reminder' => $this->send_reminder
];
dd($data);
//Create Deadline
Deadline::create(array_merge($data, $validated));
//Get Reminder Values
// switch($this->reminder) {
// case('1_day_before'):$this->reminder = 1; break;
// case('7_days_before'):$this->reminder = 7; break;
// case('30_days_before'):$this->reminder = 30; break;
// case('60_days_before'):$this->reminder = 60; break;
// default: $this->reminder = 0;
// }
//Send Email
/*if($this->reminder):
dispatch(function () {
//Get Renew State Values
switch ($this->renew_state){
case($this->renew_state == "to_renew"): $this->renew_state = "To Renew"; break;
case($this->renew_state == "waiting_cash"): $this->renew_state = "Waiting for Cash"; break;
case($this->renew_state == "renewed"): $this->renew_state = "Renewed"; break;
default:$this->renew_state = "Deleted";
}
//Get Type of Renew Values
switch ($this->type_of_renew){
case($this->type_of_renew == "domain"): $this->type_of_renew = "Domain"; break;
case($this->type_of_renew == "hosting"): $this->type_of_renew = "Hosting"; break;
case($this->type_of_renew == "privacy_cookie"): $this->type_of_renew = "Privacy Cookie"; break;
default:$this->type_of_renew = "Other";
}
//Find Customer
$customer = Customer::find($this->customer_id);
//Data to send
$data = [
'name' => $customer->name,
'type_of_renew' => $this->type_of_renew,
'renew_state' => $this->renew_state,
'date' => date('d-M-Y'),
'note' => $this->note,
'amount' => $this->amount . ' ' . strtoupper(Admin::Currency()),
];
//Mail To
Mail::to($customer->email)->send(new Reminder($data,Auth::user()->id));
//Delay
})->delay(now()->addDays($this->reminder));
endif;*/
session()->flash('success', 'Added Successfully');
return redirect(route('UserDeadlines'));
}
else {
session()->flash('error', 'Your subscription allows you to create only ' . $allowed_deadlines .
' Deadlines for each Customer.Please upgrade your Subscription');
// return redirect(route('UserDeadlines'));
}
} catch (Exception $e) {
return session()->flash('error', $e->getMessage());
}
}发布于 2022-11-06 09:45:03
不确定您的代码的其余部分,如果您发布了所有的代码,您将能够得到更多的帮助。话虽如此,livewire并不只是查找页面上的输入,您需要通过使用wire:model="send_reminder"让它知道
工作示例:示例
test-field.blade.php
<div>
<input wire:model="send_reminder" name="send_reminder[]" value="1" type="checkbox">
<input wire:model="send_reminder" name="send_reminder[]" value="2" type="checkbox">
<input wire:model="send_reminder" name="send_reminder[]" value="3" type="checkbox">
<input wire:model="send_reminder" name="send_reminder[]" value="4" type="checkbox">
<div>{{ print_r($send_reminder) }}</div>
</div>TestField.php
class TestField extends Component
{
public $send_reminder = array();
public function render()
{
return view('livewire.test-field');
}
}https://stackoverflow.com/questions/74287501
复制相似问题