【问题标题】:Laravel 7 Component causing problem sin blade templateLaravel 7组件导致问题sin刀片模板
【发布时间】:2021-04-24 06:21:20
【问题描述】:

我正在尝试在 Laravel 7 中为表单的文本输入设置一个组件。我有一个很好的国家/地区,并且正确的元素被传递给我的表单。

我的表单开始:

{{ Form::model($country, array('route' => array('countries.basic', $country->id), 'class' => 'form-horizontal','files' => true)) }}

    {{ Form::token() }}
<table class="table">
    <x-textInput :label="country" :name="country" :value="$country->country" required="required" />
    <tr>
        <td>abbreviation</td>
        <td><input type="text" id="abbreviation" name="abbreviation" value="{{ $country->abbreviation }}" data-alias="" class="form-control" required style="width:40px"></td>
    </tr>
    <tr>
        <td>capital:</td>
        <td><input type="text" id="capital" name="capital" value="{{ $country->capital }}" data-alias="" class="form-control" required></td>
    </tr>
    <tr>
        <td>main cities:</td>
        <td><textarea id="textarea_1" name="mainCities" rows="3" data-alias="" class="form-control">{{ $ct->mainCities }}</textarea></td>
    </tr>

我的课没问题,组件 php 文件显示:

<div>
    <tr>
        <td>{{ $label }}:</td>
        <td><input type="text" id="{{ $name }}" name="{{ $name }}" value="{{ $value }}" data-alias="" class="form-control" {{ $required }}></td>
    </tr>
</div>

当我尝试运行表单时,出现以下错误:

Use of undefined constant country - assumed 'country'

表单源代码呈现为:

肯定有一列“资本”。

【问题讨论】:

    标签: laravel components


    【解决方案1】:

    答案就在类声明和调用语法中。

    类声明现在是:

    use Illuminate\View\Component;
    
    class textInput extends Component
    {
        public $label;
        public $name;
        public $value;
    
        public function __construct($label,$name,$value)
        {
            $this->label = $label;
            $this->name = $name;
            $this->value = $value;
        }
    
        /**
         * Get the view / contents that represent the component.
         *
         * @return \Illuminate\View\View|string
         */
        public function render()
        {
            return view('components.textInput');
        }
    }
    

    与调用部分:

    <x-textInput :label="__('country')" :name="__('country')" :value="$country->country"/>
    

    以及组件本身

    <div>
        <tr>
            <td>{{  $label }}:</td>
            <td><input type="text" id="{{  $label }}" name="{{  $name }}"  value="{{ $value }}" data-alias="" class="form-control"></td>
        </tr>
    </div>
    

    感谢 Laracasts 的 devingray!

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 2014-10-13
      • 2015-04-16
      • 1970-01-01
      • 2013-04-29
      • 2021-07-04
      • 2017-08-24
      • 2019-12-02
      相关资源
      最近更新 更多