Home > database >  How to get value into a Blade input in Laravel8?
How to get value into a Blade input in Laravel8?

Time:01-12

Into my edit.blade.php, i can display the value into my input like this for prepare my update:

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

This works but that code not :

  <label name="name" id="name" >Name</label>
  <input name="name" id="name" {{ $user->name }} autocomplete="false" tabindex="0" type="text" >

I want just to use input and not x-input.

What are the differents ?

How can i display my value into my input ?

I found that but for 9 years it has to evolve, no ?

How to Set Variables in a Laravel Blade Template

CodePudding user response:

Here how it will work Laravel blade.

 <label name="name" id="name" >Name</label>
 <input name="name" id="name" value="{{ $user->name }}" autocomplete="false" tabindex="0" type="text" >

CodePudding user response:

Solution

<label name="name" id="name" >Name</label>
<input name="name" id="name" value="{{ $user->name }}" type="text" >

CodePudding user response:

1: create component

php artisan make:component InputComponent

2: InputComponent

<?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: in input-component.blade.php

<div >
   <label  for="{{$name}}">{{$title}}</label>
   <input type="{{$type ?? 'text'}}"  name="{{$name}}"      id="{{$name}}" @isset($value) value="{{$value}}" @endisset>
   <small >{{$errors->first($name)}}</small>
</div>

4: in AppServiceProvider and boot method

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

5: use in blades:

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