dtutlamjasblef7982 2018-10-22 15:51
浏览 45
已采纳

Laravel:根据cookie值有条件地渲染模态

I'm trying to display (or not) a newsletter modal according to if a cookie is set or not, for some reason $show_modal always returns false.

Main Page Controller:

public function inicio()
    {

        $show_modal = Modal::checkIfShowModal();

        //dd($show_modal);
        
        return view('inicio.index', compact( 'show_modal'));
    }

This is my method for checking if the modal should be shown or not:

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;
        }



    }

Inside my blade template I use conditional to show render modal

@if($show_modal == true)
    @include('partials/modals/modal_fir')
@endif

Modals migration:

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,
        ]);

Any idea why the modal never shows up

</div>
  • 写回答

1条回答 默认 最新

  • dongtiao0279 2018-10-22 16:13
    关注

    You don't have to do a strict comparison in Modal.php. Try this:

    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;
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 潜在扩散模型的Unet特征提取