Blade: 匿名组件传递数据时不显示

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

Blade: anonymous component doesn't show when passing data

问题

Controller:

public function edit(Workingtime $workingtime)
{
  $user = User::find(Auth::id());
  $workingtime_array = $user->getWorkingtimesOfThisDay($workingtime->id);
        
  return view('workingtime-edit', compact('workingtime_array', 'workingtime'));     
}

workingtime-edit.blade.php:

<p>Overview of Workingtimes</p>

@if(count($workingtime_array) > 0)
    <x-workingtime-timeline :workingtimeArray="$workingtime_array"/>
@endif

workingtime-timeline.blade.php (component):

@props(['workingtimeArray']) 

@php

if(isset($workingtimeArray) AND count($workingtimeArray) > 0){
    $start = strtotime($workingtimeArray[0]->time_begin);
    $end = strtotime($workingtimeArray[count($workingtimeArray)-1]->time_end);
} 

@endphp

@foreach ($workingtimeArray as $worktime)

<p>Start: {{ $worktime->time_begin }} </p>
<p>End: {{ $worktime->time_end }} </p>

@endforeach
英文:

I'm new to Blade and tried to use anonymous components for sections that I will use frequently.
The problem is that when I'm trying to pass down data to the component, it won't show anything.

Here is my Code:

Controller:

public function edit(Workingtime $workingtime)
    {
      $user = User::find(Auth::id());
      $workingtime_array = $user-&gt;getWorkingtimesOfThisDay($workingtime-&gt;id);
            
      return view(&#39;workingtime-edit&#39;, compact(&#39;workingtime_array&#39;, &#39;workingtime&#39;));     
    }

workingtime-edit.blade.php:

&lt;p&gt;Overview of Workingtimes&lt;/p&gt;

@if(count($workingtime_array) &gt; 0)
    &lt;x-workingtime-timeline :workingtimeArray = &quot;$workingtime_array&quot;/&gt;
@endif

workingtime-timeline.blade.php (component):

@props([&#39;workingtimeArray&#39;]) 

@php

    if(isset($workingtimeArray) AND count ($workingtimeArray) &gt; 0){
        $start = strtotime($workingtimeArray[0]-&gt;time_begin);
        $end = strtotime($workingtimeArray[count($workingtimeArray)-1]-&gt;time_end);
    } 

@endphp

@foreach ($workingtimeArray as $worktime)

    &lt;p&gt;Start: {{ $worktime-&gt;time_begin }} &lt;/p&gt;
    &lt;p&gt;End: {{ $worktime-&gt;time_end }} &lt;/p&gt;

@endforeach;

When I don't use variables and @props, it shows me the content of the component.

I tried to change the &lt;x-workingtime-timeline :workingtimeArray = &quot;$workingtime_array&quot;/&gt; line but nothing worked.

Things I tried:

&lt;x-workingtime-timeline :workingtimeArray = &quot;$workingtime_array&quot;&gt; &lt;/x-workingtime-timeline&gt;

&lt;x-workingtime-timeline workingtimeArray = &quot;$workingtime_array&quot;/&gt;

答案1

得分: 1

workingtime-edit.blade.php 中,您需要去除您设置的属性周围的空格:

&lt;x-workingtime-timeline :workingtimeArray=&quot;$workingtime_array&quot;/&gt;

我认为这是 Laravel 解析组件属性时的限制,因为这不是 HTML 属性的一般要求。

英文:

In workingtime-edit.blade.php you need to remove the spaces around the = for the attribute you are setting:

&lt;x-workingtime-timeline :workingtimeArray=&quot;$workingtime_array&quot;/&gt;

I think this is a limitation of Laravel's parsing of component attributes, since this is not a general requirement of HTML attributes.

huangapple
  • 本文由 发表于 2023年6月1日 21:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382247.html
匿名

发表评论

匿名网友

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

确定