Reputation:
I need some help. I have separate sections in my blade template which I would like not to be shown if there are no values to display.
This is the section I want to hide if there are no entries.
<div class="card-incident-details-other card-incident-details ">
<h4>Other Skills Used</h4>
<ul>@foreach ($skillsused as $skillused)
@if ($skillused->SkillName->gid === 4)
<li>{{ $skillused->SkillName->skills_name }}</li>
@endif
@endforeach</ul>
</div>
However I'm struggling and need some help please.
Essentially, I need to do something like:
@if (count($skillused->SkillName->gid == 4) > 0)
<div class="card-incident-details-other card-incident-details ">
<h4>Other Skills Used</h4>
<ul>@foreach ($skillsused as $skillused)
@if ($skillused->SkillName->gid === 4)
<li>{{ $skillused->SkillName->skills_name }}</li>
@endif
@endforeach</ul>
</div>
@endif
But this returns an error: count(): Parameter must be an array or an object that implements Countable (View: /var/www/html/basics/resources/views/incidents/viewincident.blade.php)
dd($skillsused):
Illuminate\Database\Eloquent\Collection {#366 ?
#items: array:2 [?
0 => App\SkillUsed {#367 ?
#table: "skills_used"
#primaryKey: "usage_id"
+timestamps: false
#fillable: array:3 [?]
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [?
"usage_id" => 104
"sid" => 1
"incident_id" => 66
]
#original: array:3 [?
"usage_id" => 104
"sid" => 1
"incident_id" => 66
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [?]
}
1 => App\SkillUsed {#368 ?
#table: "skills_used"
#primaryKey: "usage_id"
+timestamps: false
#fillable: array:3 [?]
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [?
"usage_id" => 105
"sid" => 3
"incident_id" => 66
]
#original: array:3 [?
"usage_id" => 105
"sid" => 3
"incident_id" => 66
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [?]
}
]
}
dd($skillused)
App\SkillUsed {#366 ▼
#table: "skills_used"
#primaryKey: "usage_id"
+timestamps: false
#fillable: array:3 [▶]
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"usage_id" => 85
"sid" => 1
"incident_id" => 55
]
#original: array:3 [▼
"usage_id" => 85
"sid" => 1
"incident_id" => 55
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"SkillName" => App\Skill {#381 ▼
#table: "skills"
#primaryKey: "did"
+timestamps: false
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"sid" => 1
"gid" => 2
"skills_name" => "Patient Assessment"
"deleted" => 0
]
#original: array:4 [▼
"sid" => 1
"gid" => 2
"skills_name" => "Patient Assessment"
"deleted" => 0
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
}
]
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}
Upvotes: 0
Views: 124
Reputation: 4684
You can use a simple filter to do this:
if($skillsused->filter(function ($v) {
return $v->SkillName->gid === 4;
})->count() > 0)
If you have PHP 7.4 and above:
if($skillsused->filter(fn ($v) => $v->SkillName->gid === 4)->count() > 0)
Upvotes: 0
Reputation: 510
The error message is quite clear, you can only pass an array or a collection of models into the count
helper function. This $skillused->SkillName->gid
is a single instance of a model not to mention your accessing gid
so technically your passing an integer
into the count
function. To fix your error refactor your code to look like this:
// I believe this `$skillsused` variable is the collection given your looping through it.
// Model collections comes with a count method for convenience so we use that instead.
@if ($skillsused->count() > 0)
<div class="card-incident-details-other card-incident-details">
<h4>Other Skills Used</h4>
<ul>
@foreach ($skillsused as $skillused)
@if ($skillused->SkillName->gid === 4)
<li>{{ $skillused->SkillName->skills_name }}</li>
@endif
@endforeach
</ul>
</div>
@endif
Following your comment below, this should yield the desired result:
@if ($skillsused->count() > 0)
@php $displayBlock = false; @endphp
@foreach ($skillsused as $skillused)
@if ($skillused->SkillName->gid === 4) $displayBlock = true @endif
@endforeach
@if ($displayBlock)
<div class="card-incident-details-other card-incident-details">
<h4>Other Skills Used</h4>
<ul>
@foreach ($skillsused as $skillused)
@if ($skillused->SkillName->gid === 4)
<li>{{ $skillused->SkillName->skills_name }}</li>
@endif
@endforeach
</ul>
</div>
@endif
@endif
This is clunky if you ask me but if it solves your problem then fine, consider moving it to a blade component.
Upvotes: 0