Answer the question
In order to leave comments, you need to log in
How to check instanceOf in blade?
How to make a check in the View similar to that for the controller
foreach($collection as $object){
if($object instanceof ModelName1){ //code goes here.. }
elseif($object instanceof ModelName2){ //code goes here.. }
else($object instanceof ModelName3){ //code goes here.. }
}
@if(count($collection))
@foreach($collection as $object)
@if($object instanceof ModelName1)
//
@elseif($object instanceof ModelName2)
//
@elseif($object instanceof ModelName2)
//
@endif
@endforeach
@endif
dd($object); //ModelName1
dd($object instanceof ModelName1); //false
@if(count($newPreorderDetails))
@foreach($newPreorderDetails as $newPreorderDetail)
@if($newPreorderDetail instanceof BaseDetail)
go go go
@endif
@endforeach
@endif
class BaseDetail extends Model
{
// конструкторы, связи, группы и много всего
}
dd($newPreorderDetail);
BaseDetail {#267 ▼
#table: "base_detail"
#guarded: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:19 [▶]
#original: array:19 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
dd($newPreorderDetail instanceof BaseDetail);
false
Answer the question
In order to leave comments, you need to log in
Show original code. When checking, do you indicate the namespace of the model?
Does it make sense to complicate the code and not use standard blade constructs @if и @foreach
?
@if(count($collection))
@foreach($collection as $object)
@if($object instanceof ModelName1)
//
@elseif($object instanceof ModelName2)
//
@elseif($object instanceof ModelName2)
//
@endif
@endforeach
@endif
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question