【问题标题】:How to get value into a Blade input in Laravel8?如何在 Laravel 8 中获取 Blade 输入的价值?
【发布时间】:2022-01-12 11:09:08
【问题描述】:

进入我的edit.blade.php,我可以像这样在我的输入中显示值以准备我的更新:

<div>
     <x-label for="name" value="name" />
     <x-input id="name" name="name" :value="$user->name" />
     <x-label for="email" value="email" />
     <x-input id="email" value="email" :value="$user->email" />
</div>

这有效,但该代码无效:

  <label name="name" id="name" class="bg-white text-gray-600 px-1">Name</label>
  <input name="name" id="name" {{ $user->name }} autocomplete="false" tabindex="0" type="text" class="py-1 px-1 text-gray-900 outline-none block h-full w-full">

我只想使用输入而不是 x-input。

有什么不同?

如何在输入中显示我的值?

我发现它必须进化 9 年,不是吗?

How to Set Variables in a Laravel Blade Template

【问题讨论】:

    标签: laravel laravel-blade


    【解决方案1】:

    Laravel Blade 的工作原理如下。

     <label name="name" id="name" class="bg-white text-gray-600 px-1">Name</label>
     <input name="name" id="name" value="{{ $user->name }}" autocomplete="false" tabindex="0" type="text" class="py-1 px-1 text-gray-900 outline-none block h-full w-full">
    

    【讨论】:

      【解决方案2】:

      解决方案

      <label name="name" id="name" class="bg-white text-gray-600 px-1">Name</label>
      <input name="name" id="name" value="{{ $user->name }}" type="text" >
      

      【讨论】:

        【解决方案3】:

        1:创建组件

        php artisan make:component InputComponent

        2:输入组件

        <?php
        
        namespace App\View\Components\Form;
        
        use Illuminate\View\Component;
        
        class InputComponent extends Component
        {
        
        public $name;
        public $title;
        public $value;
        public $type;
        
        public function __construct($name,$title,$value=null,$type = null)
        {
            $this->name = $name;
            $this->title = $title;
            $this->value = $value;
            $this->type = $type;
        }
        
        public function render()
        {
            return view('components.form.input-component');
        }
        }
        

        3:在 input-component.blade.php 中

        <div class="form-group my-2">
           <label class="py-2" for="{{$name}}">{{$title}}</label>
           <input type="{{$type ?? 'text'}}" class="form-control" name="{{$name}}"      id="{{$name}}" @isset($value) value="{{$value}}" @endisset>
           <small class="form-text text-danger">{{$errors->first($name)}}</small>
        </div>
        

        4:在 AppServiceProvider 和启动方法中

        Blade::component('form.input-component', InputComponent::class);
        

        5:在刀片中使用:

        <x-form.input-component name="title" value="{{old('title')}}" title="title"/>
        

        【讨论】:

          【解决方案4】:

          这应该可行。

          <label name="name" id="name" class="bg-white text-gray-600 px-1">Name</label>
          <input name="name" id="name" value="{{ isset($user->name) ? $user->name : '' }}" autocomplete="false" tabindex="0" type="text" class="py-1 px-1 text-gray-900 outline-none block h-full w-full">
          

          【讨论】:

            猜你喜欢
            • 2021-06-02
            • 2020-11-14
            • 2021-09-17
            • 2017-01-11
            • 2018-11-15
            • 2019-04-17
            • 2018-03-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多