Home > Mobile >  The intended value is not being displayed in Laravel
The intended value is not being displayed in Laravel

Time:12-06

I have a form where a user can choose his/her civil status. And I have a separate table for the civil status that I queried and displayed the option through the controller into the blade file. The problem is, I cant display the selected value of the $user->civil_status in the blade file. Ill provide the code down below

Controller.php file

    public function get_civil_status($id)
    {
        $statuses = CivilStatusModel::all();
        $opt="<option>Select Civil Status</option>";

        foreach($statuses as $status)
        {
            if($status->id > 0) {
                if($id == $status->id){
              
                    $opt.="<option value={$status->id} selected>{$status->complete_name}</option>";
                  
                } else {
                    $opt.="<option value={$status->id}>{$status->complete_name}</option>";
                }
            }
        }
        return $opt;
    }

    public function dashboard($id){
        $data = UserModel::where('seq_id','=',Session::get('loginId'))->first();
        $data['optStatus']=$this->get_civil_status($id);
        return view ("home.dashboard", $data);
        
    }

This is my civil_status table

Blade.php file

  <div >
   <div >
    <label for="civilStatus" >Civil Status</label>
      <div >
        <select  type="date" id='civilStatus' >
         {{!! $optStatus !!}}
        </select>
       </div>
      </div>
     </div>

You might provide links or show the solution in the answer, anything that will help me improve will be appreciated

CodePudding user response:

It looks like you're trying to access the variable $optStatus in your blade template. However, in your controller you only pass the variable $data which does contain the first variable. You can access the data by {{!! $data['optStatus'] !!}}

Also, as you asked about showing you refactoring in your comment, here's how to use blade's conditional and looping patterns:

Controller.php:

 public function get_civil_status()
    {
        $statuses = CivilStatusModel::all();
        return $statuses;
    }

    public function dashboard($id){
        $data = UserModel::where('seq_id','=',Session::get('loginId'))->first();
        $optStatus = $this->get_civil_status();
        return view ("home.dashboard", ['optStatus'=>$optStatus,'id'=>$id]);        
    }

template.blade.php:

  <div >
   <div >
    <label for="civilStatus" >Civil Status</label>
      <div >
        <select  type="date" id='civilStatus' >
        @foreach($optStatus as $status)
             @if($status->id > 0)
                @if($id == $status->id)
                   <option value={$status->id} selected>{$status->complete_name}</option>                  
                @else
                   <option value={$status->id}>{$status->complete_name}</option>
                @endif
            @endif
        @endforeach
        </select>
       </div>
      </div>
     </div>

I would further refactor it to say the selected user in one line rather than your conditional @if - @else - @endif

It would look something like this:

<option value={$status->id} @if($status->id == $id) selected @endif>{$status->complete_name}</option>                  

With the line above, you would be able to drop your most inner if else loop as the selected attribute is the only change in both elements.

Here's a reference to the blade templating library in laravel.

https://laravel.com/docs/9.x/blade

Lastly, as a hint to letting Laravel do the heavy lifting, check out Auth::user() Rather than accessing your session and then querying a user, it should be done for you already in most cases unless you're using a very barebones instance of Laravel

  • Related