首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel livewire中复选框中的错误

laravel livewire中复选框中的错误
EN

Stack Overflow用户
提问于 2022-10-25 11:42:04
回答 3查看 75关注 0票数 0

我在livewire组件上的复选框出现了多头问题。

我正试着保存在表单的第二张复选框中。此复选框在另一个名为"Garde_type“的表中获取它们的值。复选框"home“保存在我表单的表ID 1,复选框”访问“保存ID 2。

拉勒维尔直播要求我的财产规则保存我的数据。好的!但是,当我输入规则时,我的2个复选框:在我的形式中,复选框不能是checked..when,我会选中它们,它们会立即/取消选中=第一个问题。第二个问题:无论是选中还是取消……每次我提交我的表单时,这两个复选框都会考虑“选中”,然后保存在我的数据库中。

在这里,您可以查看我的代码,我尝试了很多东西,但我已经不知道了。

这是带电组件“控制器”:

代码语言:javascript
复制
class VilleSelect extends Component {     

public $visit;
public $home;
public $user_id;

protected function rules() {

 return [     
    
   'visit' => 'nullable',
   'home' => 'nullable',
];
    }


public function submit() {

   $annonces=annonces::create([
        'home' => $this->home->id,
        'visit' => $this->visit->id,
    ]);
    
    $annonces->save();
       
}

以下是复选框:

代码语言:javascript
复制
 <div class="mt-4 space-y-4">
     <div class="flex items-start">
          <div class="flex h-5 items-center">
            <x-jet-input wire:model="home" value="{{$home->id}}" type="checkbox"/>
          </div>
          <div class="ml-3 text-sm">
            <x-jet-label for="home" value="{{$home->garde_type}}"/>
          </div>
      </div>
      <div class="flex items-start">
          <div class="flex h-5 items-center">
            <x-jet-input wire:model='visit' value="{{$visit->id}}" type="checkbox"/>
          </div>
                
          <div class="ml-3 text-sm">
            <x-jet-label for="visit" value="{{$visit->garde_type}}"/>
          </div>
       </div>               
  </div>
EN

回答 3

Stack Overflow用户

发布于 2022-10-26 05:11:06

在调用属性时,没有理由具体地调用id,因为您已经设置了主页并访问了ID。

试一试

代码语言:javascript
复制
$annonces=annonces::create([
    'home' => $this->home,
    'visit' => $this->visit,
]);
票数 0
EN

Stack Overflow用户

发布于 2022-10-26 12:06:23

好了,现在我的复选框不再有问题了。但它不能像我想的那样起作用。

这是我的复选框:

代码语言:javascript
复制
   <x-jet-input wire:model="home"  value="1" type="checkbox"/>
                                
   <x-jet-input wire:model="visit" value="2" type="checkbox"/>

这里我的控制者:

代码语言:javascript
复制
public $visit;
public $home;



public function submit()
{
 

   $annonces=annonces::create([
        
        'home' => $this->home,
        'visit' => $this->visit,
      
    ]);
   
   
   
    $annonces->save();

   }

但我不想在表单中传递值"1“和"2”。

我尝试的是:

代码语言:javascript
复制
public function submit()
{
    $this->visit = Garde_type::find(2)->id;
    $this->home = Garde_type::find(1)->id;

   $annonces=annonces::create([
        
        'home' => $this->home,
        'visit' => $this->visit,
      
    ]);
   
   
    $annonces->save();

   }

和表单:

代码语言:javascript
复制
  <x-jet-input wire:model="home"  value="{{home->id}}" type="checkbox"/>
                                
   <x-jet-input wire:model="visit" value="{{visit->id}}" type="checkbox"/>

但我试过的都没用。

有什么想法吗?

票数 0
EN

Stack Overflow用户

发布于 2022-10-26 21:39:06

我被你的密码弄糊涂了。如果您已经知道了Garde_type的ID,为什么还要再次查找它?

例如,将值设置为ID,然后在Garde_type中搜索您已经拥有的ID,这是多余的。

代码语言:javascript
复制
// Set the value with the variable $home, $visit not just home and visit.
<x-jet-input wire:model="home"  value="{{ $home->id }}" type="checkbox"/>
<x-jet-input wire:model="visit" value="{{ $visit->id }}" type="checkbox"/>

既然您已经有了Garde_type,为什么要在这里获取它的ID呢?

代码语言:javascript
复制
// The ID will be 2 since you're searching for a record with the ID of 2
$this->visit = Garde_type::find(2)->id;
// The ID will be 1 since you're searching for a record with the ID of 1
$this->home = Garde_type::find(1)->id;

你到底想做什么?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74193524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档