Home > Net >  How to display the value in a readonly input field in laravel
How to display the value in a readonly input field in laravel

Time:11-22

So, in my project, a user needs to register first before logging-in. I am trying to display their first, middle and last name. But, I am having a problem since multiple data are displayed on my input field. What is the correct query? Here is my controller code

     public function get_first_name()
{
    $first_names = UserModel::all();
    $input="<input></input>";
    foreach($first_names as $first_name)
    {
        $input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
    }
    return $input;
}

    public function step1()
{
    $users = UserModel::all();
    $data['optStatus']=$this->get_civil_status();
    $data['displayFirstName']=$this->get_first_name();
    return view ("enrollment-steps.step1", $data);
}

Here is the blade file

    <div >
     <div >
      <label for="firstName" >First 
       Name</label>
      <div >
      <input  type="text" id='firstName' readonly value="{!! $displayFirstName !!}" >
      </div>
     </div>
    </div>

this is the data that it displays

CodePudding user response:

In you controller bind the data's in a single variable, then show it in the blade

public function get_first_name()
    {
        $first_names = UserModel::all();
        $input="<input></input>";
        foreach($first_names as $first_name)
        {
            $input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
        }
        return $input;
    }

Instead of this, do something like this:

public function get_first_name()
    {
        $first_names = UserModel::all();
        $input="<input></input>";
        foreach($first_names as $first_name)
        {
            $displayname = $first_name->seq_id . $first_name->first_name;
            $input.="<input value="{$displayname}"</input>";
        }
        return $input;
    }

CodePudding user response:

There is some confusing stuff going on there, but you probably want

<input  type="text" id='firstName' readonly value="{{ $data['displayFirstName'] }}" >

which will display the value with the key 'displayFirstName' in the $data array.

dd is your friend, use this in your blade file to see what you have in your $data variable.

{{dd($data)}}

CodePudding user response:

You can achieve your goal by using Accessor

Note: plz watch docs of appropriate laravel version that is used in your project.

some code snippet I'm showing you in blow code to below snippet:

// In your User model:

public getFullNameAttribute(){
   return $this->first_name . " " . $this->middle_name. " " . $this->last_name 
}

then in you can use it anywhere through User model instance, like that In your case

foreach($users as $user){
  dump($user->full_name);
}
  • Related