英文:
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->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;
When I don't use variables and @props, it shows me the content of the component.
I tried to change the <x-workingtime-timeline :workingtimeArray = "$workingtime_array"/>
line but nothing worked.
Things I tried:
<x-workingtime-timeline :workingtimeArray = "$workingtime_array"> </x-workingtime-timeline>
<x-workingtime-timeline workingtimeArray = "$workingtime_array"/>
答案1
得分: 1
在 workingtime-edit.blade.php
中,您需要去除您设置的属性周围的空格:
<x-workingtime-timeline :workingtimeArray="$workingtime_array"/>
我认为这是 Laravel 解析组件属性时的限制,因为这不是 HTML 属性的一般要求。
英文:
In workingtime-edit.blade.php
you need to remove the spaces around the =
for the attribute you are setting:
<x-workingtime-timeline :workingtimeArray="$workingtime_array"/>
I think this is a limitation of Laravel's parsing of component attributes, since this is not a general requirement of HTML attributes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论