简体   繁体   中英

how to send data from controller to include/header blade file but return another blade file in laravel

I want to include send my data from my controller to my header.blade.php file in view but i want to return the other view in the laravel as mentioned below:-

public function handleProviderCallback()
{
    $user = Socialite::driver('google')->stateless()->user(); 
    $finduser = User::where('google_id', $user->id)->first();

    if($finduser) {
        Auth::login($finduser);
        $google_avatar = $user->getAvatar();
        view('pages.home',['google_avatar', $google_avatar]);  
        return redirect()->route('home',);      
    }
}

By extending a layout is the cleanest way.

From the docs:

<!-- Stored in resources/views/layouts/app.blade.php -->

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

The child:

@extends('layouts.app')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

By just returning the "child" view from the controller, the data will be available in the header as well. If you pass it in the @extends declaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM