Laravel 拉雷维尔:为什么是我的;“通知”;数据库中的数据没有显示?
因此,我从控制器、路由和app.blade.view中获得了以下代码: 控制器:Laravel 拉雷维尔:为什么是我的;“通知”;数据库中的数据没有显示?,laravel,laravel-6,Laravel,Laravel 6,因此,我从控制器、路由和app.blade.view中获得了以下代码: 控制器: public function showNotif(){ $dataNotif= Notification::get(); // dd($dataNotif); return view('/',compact('dataNotif')); } 路线: Route::get('/','homeController@showNotif'); 在app.blade.view中,我在显示通知
public function showNotif(){
$dataNotif= Notification::get();
// dd($dataNotif);
return view('/',compact('dataNotif'));
}
路线:
Route::get('/','homeController@showNotif');
在app.blade.view中,我在显示通知的可点击钟形图标中找到了这些代码:
@if(isset($dataNotif))
@foreach($dataNotif as $dn)
<a href="#" class="dropdown-item dropdown-item-unread">
<div class="dropdown-item-desc">
<p>{{$dn->notifikasi}}</p>
</div>
</a>
@endforeach
@else
<a href="#" class="dropdown-item dropdown-item-unread">
<div class="dropdown-item-desc">
<p>No Notifications!</p>
</div>
</a>
@endif
@if(isset($dataNotif))
@foreach($dataNotif作为$dn)
@endforeach
@否则
@恩迪夫
当我尝试在app.blade.view中使用foreach方法时,它总是在$dataNotif error上显示“未定义变量”,但当我首先使用isset时,没有显示任何数据,当我打开主网站时,应该显示两个通知数据
那么,我有没有犯过任何概念错误或小错误
感谢您的回答。如果我们不知道您的通知类是什么样子的话,我们可以提供帮助,上次我检查了通知中的数据是通过
$Notification->data['notifikasi']
访问的,假设您的通知类是这样的:
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'notifikasi' => 'lorem ipsum'
];
}
如果通知支持存储在数据库表中,则应在通知类上定义
toDatabase
方法。此方法将接收一个$notifiable
实体,并应返回一个普通PHP数组。返回的数组将被编码为JSON,并存储在notifications
表的data
列中。让我们来看一个例子:代码>数据库< /COD>方法:
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
return [
'invoice_id' => $this->invoice->id,
'amount' => $this->invoice->amount,
];
}
访问通知
一旦通知存储在数据库中,您需要一种方便的方式从您的应呈报实体访问它们。Laravel的默认App\User
模型中包含的illighted\Notifications\Notifiable
特征包含一个通知
雄辩的关系,该关系返回实体的通知。要获取通知,您可以像访问任何其他有说服力的关系一样访问此方法。默认情况下,通知将按时间戳处创建的排序:
$user = App\User::find(1);
foreach ($user->notifications as $notification) {
echo $notification->type;
}
哦,一个小信息,我没有在laravel上使用任何“通知”方法,我只是简单地进行迁移,并建立一个简单的通知模型。。。所以我很惊讶我对这个类并没有那么不理解…如果是这样的话,试着用all()
代替get()
。你用dd($dataNotif)得到了什么代码>?