PHP documentation for conditional polymorphic calls
P粉127901279
P粉127901279 2023-08-06 15:31:19
0
1
536
<p>For $item, there is a polymorphic recordable relationship. In the database, this is stored in the loggable_type and loggable_id fields in the items table (for PHP 8 and Laravel). </p> <pre class="brush:php;toolbar:false;">for($items as $item) { // ... if ($item->loggable_type === Comment::class) { $item->loggable->resetDates(); } // ... } </pre> <p>I'm trying to type-hint a loggable in a condition, specifying it as a Comment type. I tried using @var, but writing it like /* @var $item->loggable Comment */ doesn't work, and I can't use /* @var $item Comment */ because that sets the type hint for $item rather than its properties.
P粉127901279
P粉127901279

reply all(1)
P粉063039990

Assign it to a variable

for($items as $item) {
    if ($item->loggable_type === Comment::class) {
        /** @var Comment $loggable */
        $loggable = $item->loggable;
        
        $loggable->resetDates();
    }
} 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template