php - Laravel:根据 cookie 值有条件地渲染模式
问题描述
我试图根据是否设置了 cookie 来显示(或不显示)时事通讯模式,由于某种原因,$show_modal 总是返回 false。
主页控制器:
public function inicio()
{
$show_modal = Modal::checkIfShowModal();
//dd($show_modal);
return view('inicio.index', compact( 'show_modal'));
}
这是我检查模态是否应该显示的方法:
模态的.php
public static function checkIfShowModal(){
$modal = Modal::first();
if($modal->isActive && Cookie::get('cookie_modal_1') !== false)
{
//cookie is set, don't show modal
return false;
}
else if($modal->isActive && Cookie::get('cookie_modal_1') == true){
//cookie isn't set, show modal then
Cookie::queue( Cookie::make('cookie_modal_1', true, 60*24*7));
return true;
}
}
在我的刀片模板中,我使用条件来显示渲染模式
@if($show_modal == true)
@include('partials/modals/modal_fir')
@endif
模态迁移:
Schema::create('modals', function (Blueprint $table) {
$table->increments('id');
$table->longText('body');
$table->boolean('isActive')->default(false);
$table->timestamps();
});
//FILL MODALS TABLE WITH ONE MODAL
DB::table('modals')->insert([
'body' => 'Subscribete a nuestro boletín noticiario, recibe ofertas, noticias, eventos y articulos de nosotros',
'isActive' => true,
]);
知道为什么模态永远不会出现
解决方案
您不必在Modal.php
. 尝试这个:
public static function checkIfShowModal(){
$modal = Modal::first();
//use != instead of !==
if($modal->isActive && Cookie::get('cookie_modal_1') != false)
{
//cookie is set, don't show modal
return false;
}
else if($modal->isActive && Cookie::get('cookie_modal_1') == true){
//cookie isn't set, show modal then
Cookie::queue( Cookie::make('cookie_modal_1', true, 60*24*7));
return true;
}
//return default response, true or false (according to preference)
return true;
}
推荐阅读
- python - Python-pptx - 将图片插入形状和动画
- java - 如何在Android parse.com Java中获取类的所有列名
- java - 当两个 logback.xml 在 Websphere 服务器上运行时,日志被合并
- javascript - 无法弄清楚为什么应用程序没有部署到heroku
- python - 熊猫数据框是从右到左?
- azure - Azure 管道未运行 - 属性参数为空
- python-3.x - 匀称的循环不创建线串
- ios - 在 macOS 10.13(或 iOS 12)及更早版本上将 WKWebView 的继承外观设置为 InterfaceStyle: .Dark
- c - 递归在这段代码中究竟是如何工作的?
- python - 如何在 django 中只运行一次 celery 任务?