Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

Laravel 7 Component causing problem sin blade template

I am trying to set up a component for a text input for forms in Laravel 7. I have a Country which is fine and the correct element is pass to my form.

My form starts:

{{ 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>

I have the class OK and the component php file reads:

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

When I try to run the form I get the following error:

Use of undefined constant country - assumed 'country'

The source code of the form renders as:

enter image description here

There is definitely a column "capital".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer comes in the class declaration and calling syntax.

The class declaration is now:

use IlluminateViewComponent;

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 IlluminateViewView|string
     */
    public function render()
    {
        return view('components.textInput');
    }
}

with the calling part :

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

and the component itself

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

Thanks to devingray at Laracasts!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...