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 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题