如何在 Blade Laravel 中发送会话

我正在尝试在刀片中发送会话数据。在注销之前,我创建一个会话。在刀片中创建会话的数据发送。我该如何做到这一点。


public function userLogout()

{

    if (!empty(session("userLoggedIn")) && session("userLoggedIn") == true) {

        $session = session()->get('userDetails');

        $user_id = $session->id;

        $user_package = UserPackage::all()->where('user_id','=',$user_id);

        foreach($user_package as $package_id) {

            $package_detail = Package::where('id','=',$package_id->package_id)->get();

            foreach( $package_detail as $package ){

                $package_amount = $package->price;

                $package_tagline = $package->tagline;

            }

        }

        session()->flush();

        session()->save();

    }

    return redirect()->route('frontend-home')->with('session', session()->put('package_detail',[$package_tagline,$package_amount]));

}

我尝试后这个结果什么也没有。以及如何将会话的数据打印在刀片上


catspeake
浏览 120回答 2
2回答

波斯汪

您可以通过两种方式执行此操作:如果您已经设置了会话,则无需将其带到另一个页面,它可以在整个项目中使用。您可以在 Blade 中这样访问:<div>&nbsp; &nbsp; {{ Session::get('package_detail') }}</div>您可以在刀片文件中执行以下操作:&nbsp;@foreach($package_detail as $product)&nbsp; &nbsp; &nbsp;{{ $product['title'] }}&nbsp;@endforeach

慕的地6264312

public function userLogout(){&nbsp; &nbsp; if ($request->session()->has("userLoggedIn")) { // has returns true for userLoggedIn is true and not null&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $session = session()->get('userDetails');&nbsp; &nbsp; &nbsp; &nbsp; $user_id = $session->id;&nbsp; &nbsp; &nbsp; &nbsp; $user_package = UserPackage::all()->where('user_id','=',$user_id);&nbsp; &nbsp; &nbsp; &nbsp; foreach($user_package as $package_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $package_detail = Package::where('id','=',$package_id->package_id)->get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $package_detail as $package ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $package_amount[] = $package->price;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $package_tagline[] = $package->tagline;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; session()->flush();&nbsp; &nbsp; &nbsp; &nbsp; session()->save();&nbsp; &nbsp; }&nbsp; &nbsp; $package_detail['package_amount'] = $package_amount;&nbsp; &nbsp; $package_detail['package_tagline'] = $package_tagline;&nbsp; &nbsp; $packageCollection = collect($package_detail);&nbsp; &nbsp; return redirect()->route('frontend-home')->put('package_detail',$packageCollection);}像这样访问刀片@foreach (Session::get('package_detail') as $product_id)&nbsp; &nbsp; {{$product_id}}&nbsp;@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP