Tifa Ahmed
Tifa Ahmed

Reputation: 81

how to get columns name by columns value in laravel

table(skill) enter image description here

have many skills, columns type boolean(0,1),by laravel i want to get the names of columns if skills value equal 1 and user_id equal 1, without say the name of column becouse they 50 skills ,i tried this on sublime

<?php
$variable2=App\Skill::where('user_id',Auth::user()->id)
       ->where('job_id','null')
       ->get();
?>


@foreach($variable2 as $key1 => $value1)

      @if($value1='1')
          <span class="tags">{{ $key1 }}</span>
      @endif 
@endforeach

Upvotes: 2

Views: 811

Answers (2)

Dry7
Dry7

Reputation: 881

<?php    $variable2=App\Skill::where('user_id',Auth::user()->id)
           ->where('job_id','null')
           ->where('user_id', 1)
           ->get();

    $skills = $variable2->filter(function ($item) { return $item === 1; })->keys();
?>

    @foreach($skills as $key)
    <span class="tags">{{ $key }}</span>
    @endforeach

Upvotes: 1

shukshin.ivan
shukshin.ivan

Reputation: 11340

Well, looks like you forgot, that get returns a collection of skills. You must use two loops.

<?php
$userSkills = App\Skill::where('user_id',Auth::user()->id)
       ->where('job_id','null')
       ->get();
?>
<!-- loop through a list of user skills (get gives us a set of skills -->
@foreach($userSkills as $skill)
      <!-- loop through skill properties -->
      @foreach($skill->toArray() as $key => $value)
          @if($value)
              <span class="tags">{{ $key }}</span>
          @endif 
      @endforeach
@endforeach

Upvotes: 1

Related Questions