【问题标题】:Undefined property: Illuminate\Pagination\LengthAwarePaginator::$created_at未定义属性:Illuminate\Pagination\LengthAwarePaginator::$created_at
【发布时间】:2019-07-31 20:31:55
【问题描述】:

我遇到了一个可能很多人已经在这里遇到过的问题,但我似乎无法弄清楚。

我尝试在同一个变量中使用 Carbon 和 Paginate,但不断收到标题中显示的错误。

我尝试使用它的代码是:

控制器:

 public function index()
    {
        $announcements = Announcement::withCount('replies')->orderBy('created_at', 'desc')->paginate(5);
        $birthdays = User::whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(birthday) AND DAYOFYEAR(curdate()) + 365 >=  dayofyear(birthday)')
            ->orderByRaw('DAYOFYEAR(birthday)')
            ->get();
        $date = Carbon::create($announcements->created_at)->locale('nl');

        return view('home', compact('announcements', 'birthdays', 'date'));
    }

查看:

@foreach($announcements as $announcement)
                    <div class="announcement">
                        <div class="row">
                        <div class="col-lg-11">
                        <h2 style="font-size:1.5rem;" class="text-capitalize">{{$announcement->post_title}}</h2>
                        @if($announcement->post_image == null)
                            @else
                        <img src="{{$announcement->post_image}}" style="width:100%;">
                        @endif
                        <p style="font-size: 0.8rem;">{{$birthday->isoFormat('LL')}} | Geplaatst door <span>{{$announcement->username}}</span> | <span style="color:#007ac3">{{$announcement->replies_count}} reacties</span></p>
                        <p style="margin-top: -10px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">{!! Str::words($announcement->post_content, 20) !!}</p>
                        </div>
                        <div class="col-lg-1">
                            @if(Auth::user()->admin == 1)
                            <a href="/mededeling/destroy/{{$announcement->id}}"><i class="fal fa-dumpster" style="text-align: center;position: relative;font-size: 20px;"></i></a>
                                @endif
                        </div>
                        </div>
                        <p><a href="/mededeling/{{$announcement->slug}}">Meer lezen <i class="fas fa-angle-double-right"></i></a></p>
                        <hr>
                    </div>
                    @endforeach

我希望它将日期翻译成荷兰语,但现在我得到的只是错误。

【问题讨论】:

  • $announcement 变量是 Paginator 变量。您已经获得了第一个元素(或选择您想要的元素),然后您可以获得 created_at 属性

标签: php laravel laravel-5.8


【解决方案1】:

->paginate() 方法返回一个 LengthAwarePaginator 对象,而不是单个模型。您需要将日期函数映射到 Collection 对象中的每个元素。

【讨论】:

  • 我该怎么做呢?我还是 Laravel 的新手,呵呵
  • @SpookyCode 只需删除行 $date = Carbon::create($announcements-&gt;created_at)-&gt;locale('nl'); 我没有看到您在循环中使用 $datecreated_at 属性。如果你还需要 $date :$date = Carbon::create($announcements-&gt;items[0]-&gt;created_at)-&gt;locale('nl');
  • 实际上只需删除 $date 行,在刀片模板中,您可以在循环内翻译日期:{{ \Carbon\Carbon::parse($announcement-&gt;created_at)-&gt;locale('nl')-&gt;format('your format')}}
  • 谢谢 :) 它成功了,知道如何使用 $birthdays 变量来做到这一点吗??
  • 嗯,它是一个集合/数组。因此,您必须对它们进行迭代才能获得相同的结果。
猜你喜欢
  • 2019-07-11
  • 2016-11-23
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多