每当我运行一个循环时,似乎会将我的对象变为null。

huangapple go评论57阅读模式
英文:

whenever I run a loop it seems to turn my object null after

问题

我以前从未遇到过这种情况。在 Laravel 8 中,每当我遍历一个集合时,它之后就变成了null。

示例:

$loops = Loops::get();

在循环内部,我有以下代码:

public function loopNotifications()
{
    return $this->hasMany(LoopNotifications::class, 'loop_id', 'id');
}

@if($loop->loopNotifications->count()) 
    @foreach($loop->loopNotifications as $loopNotification)
     <li class="px-6 py-2 border-b border-gray-200 w-full">
         <div class="text-lg font-medium">{!!$loopNotification->convertToReadable()!!}</div>
     </li>
    @endforeach
@endif

在运行这段代码之前,我会使用dd()函数输出循环的内容,而一切都正常。

但在运行相同的dd()函数之后,循环变成了null。这对我运行的每个foreach循环都发生了。我开始编写for循环,只是为了绕过这个问题,但现在编写所有这些额外的代码变得很令人沮丧。

有人遇到过这个问题吗?

英文:

I have never encountered this before. In Laravel 8, every time I iterate through a collection, it becomes null after.

Example:

$loops = Loops::get();

inside of loops I have

public function loopNotifications()
    {
        return $this-&gt;hasMany(LoopNotifications::class, &#39;loop_id&#39;, &#39;id&#39;);
    }

@if($loop-&gt;loopNotifications-&gt;count()) 
    @foreach($loop-&gt;loopNotifications as $loopNotification)
     &lt;li class=&quot;px-6 py-2 border-b border-gray-200 w-full&quot;&gt;
         &lt;div class=&quot;text-lg font-medium&quot;&gt;{!!$loopNotification-&gt;convertToReadable()!!} 
         &lt;/div&gt;
     &lt;/li&gt;
    @endforeach
@endif

Before I run this I dd() the loop, and it's all there

After I run the same dd() and it's null. This is happening to every single foreach I run. I started to write for statements just to get past this, but at this point It's frustrating to write all the extra code.

Has anyone had this issue.

答案1

得分: 0

不要在Blade中使用$loop作为变量名。Laravel使用这个变量名来提供关于循环本身的信息,正如您猜得正确的那样。请尝试更改变量名。

另外,你可以考虑使用??操作符来代替将整个内容放在@if块中(假设你已经将$loop改为$l):

@foreach($l->loopNotifications??[] as $loopNotification)
    <li class="px-6 py-2 border-b border-gray-200 w-full">
        <div class="text-lg font-medium">{!! $loopNotification->convertToReadable() !!} </div>
    </li>
@endforeach

这将在$loop->loopNotifications为空时返回一个空数组。这不是必需的,你的方法也可以正常工作,但在我看来,这样更清晰。

英文:

Don't use $loop as a variable in blade.
Laravel uses this variable name to give you insights about the loop itself as you were guessing correctly.
Renaming the variable and try again.

Also on a sidenote you could use the ?? operator instead of putting the whole inside a @if block: (considering you named $loop to $l)

@foreach($l-&gt;loopNotifications??[] as $loopNotification)
 &lt;li class=&quot;px-6 py-2 border-b border-gray-200 w-full&quot;&gt;
     &lt;div class=&quot;text-lg font-medium&quot;&gt;{!!$loopNotification-&gt;convertToReadable()!!} 
     &lt;/div&gt;
 &lt;/li&gt;
@endforeach

This fallsback to an empty array if $loop->loopNotifications is null. It's not required, yours works as well, but IMHO it's cleaner.

huangapple
  • 本文由 发表于 2023年2月18日 08:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定