1

I am trying to retrieve data from database and check if the data is empty or not. What problem I am facing is that html is showing even if the data is empty. I want to ignore the html tag like example ul li. Here how i tried is like

@if(!empty($jobseekers->skill_1))
   <li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
@endif

I want to ignore "My Skill is " if the data is empty. I don't want to show anything.

1
  • What is the value of $jobseekers->skill_1? Commented Jul 11, 2017 at 2:34

5 Answers 5

3

When using ->get() you can't simply use any of the below:

if (empty($jobseekers->skill_1)) { }

if (!$jobseekers->skill_1) { }

if ($jobseekers->skill_1) { }

But, When you are getting data with first() method, You can simply use all above methods.

Because if you dd($jobseekers->skill_1); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results.

I think you are using !empty() on data with ->get() method which will always return true even data is empty. You need to use other way.

To determine if there are any results you can do any of the following:

if (!$jobseekers->skill_1->isEmpty()) { }

if ($jobseekers->skill_1->count()) { }

if (count($jobseekers->skill_1)) { }
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to see and congrats
1

If you get $jobseekers with get() method you can not use empty($jobseekers )

instead of empty you can use other conditions :

@if($jobseekers->skill_1 != '')

in this condition you check skill_1 as empty string

also

@if($jobseekers->skill_1)

and etc

replace your code with below code and check it:

@if($jobseekers->skill_1 != '')
   <li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
@endif

Comments

0

you should use isset()

@if(isset($jobseekers->skill_1))
    <li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
@endif

Comments

0

you can use count method

 @if(count($jobseekers->skill_1)>0)
       <li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
    @endif

Comments

0
@if(count($data)>0)
 //write your code which you want to show if data is available
@endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.