为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 eloquent 从数据库中检索 created_at 数据

如何使用 eloquent 从数据库中检索 created_at 数据

PHP
婷婷同学_ 2022-07-22 10:31:37
我如何在 laravel 中用 eloquent 从数据库中检索 created_data?,我使用的是属于多个关系,我想显示借书数据和借阅日期,我已成功显示借书数据,但我无法获取 created_at 数据来自数据库中另一个名为 borrow_history 的表书本.phppublic function borrowed(){    return $this->belongsToMany(User::class, 'borrow_history')    ->withTimestamps();}用户.phppublic function borrow(){    return $this->belongsToMany(Book::class, 'borrow_history')    ->withTimestamps();}这是我当前的 HomeController.php 索引public function index(){    $books = auth()->user()->borrow;    return view('home', [        'books' => $books,    ]);}我想在 home.blade.php 显示 created_at 数据@foreach($borrow_history as $borrow)   <p>      <i class="material-icons">date_range</i> Borrow_date : {{ ???? }}   </p>@endforeach谁能帮我?
查看完整描述

1 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

当您belongsToMany使用 Laravel 加载(多对多)关系时,它将在该关系上包含一个枢轴属性。由于您有withTimestamps()关系,它将包括created_atandupdated_at值。

您应该能够通过以下方式实现您所追求的目标:

$borrow->pivot->created_at;

有关更多信息,您可以参考文档(向下滚动到检索中间表列


查看完整回答
反对 回复 2022-07-22
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号