0

I get this error: Undefined variable: user (View: ...)

public function edit(){
    $user = User::where('id', Auth::user()->id)->first();
    return view('layouts.app', ['user' => $user]);
}

I am trying to user the object in @yield('content') in my view: layouts/app.blade.php

    <main class="">
        @yield('content')
        {{dd($user->email)}}

    </main>
user150328
  • 41
  • 6
  • Your question is not clear, would you put a picture of the error, we may get more details about the issue.. – PsyLogic Jul 13 '21 at 23:42
  • Can you add the code of `app.blade.php` that uses the `$user` object? – Abishek Jul 14 '21 at 00:01
  • @Abishek I just edited the post can you check please? – user150328 Jul 14 '21 at 00:21
  • The controller looks good to me. But typically your don't `return view('layouts.app') usually you return another blade file which `@extends('layouts.app')`, is that what you are doing? – Azeame Jul 14 '21 at 01:36
  • 1
    Does this answer your question? [How to pass data to view in Laravel?](https://stackoverflow.com/questions/18341792/how-to-pass-data-to-view-in-laravel) – Ankita Dobariya Jul 14 '21 at 03:15

1 Answers1

1

You can pass the variable to the blade in any of the way, like the one you are doing now, or using compact

compact('variable name')

Or using with

with('what name you need in blade', 'name in controller')

In your case, I feel the issue is of blade. Your name is correct

layout.app

It will call out the correct file. Please check out about yield. As far as my knowledge it is used in the parent blade (then extending to child blade). Maybe try using section in place of yield. Or better print it out directly.

Dharman
  • 30,962
  • 25
  • 85
  • 135