undefined variable foreach laravel

undefined variable in laravel blade
laravel foreach
undefined variable error in laravel
undefined variable loop laravel controller
undefined variable laravel
undefined variable laravel controller
laravel foreach loop index in controller
undefined variable response laravel

view

@foreach($post as $ps)
    <div class="card">
        <div class="card-header">
            <img src="{{asset('img/python.png')}}" class="circle" width="20" height="20">&nbsp;<b style="color:black;">{{ Auth::user()->name }}</b>
        </div>
        <div class="card-body">
            <center><img src="{{ str_replace('public/', '', $ps->image) }}" alt="" style="width: 100%;" height="320"></center>
        </div>
        <div class="card-footer text-muted">
            <b style="color:black;">{{ Auth::user()->email }}</b><br>
            {{$ps->caption}}
        </div>
    </div>
    <br><br>
@endforeach

controller

public function index()
{
    $post=Post::all();

    // dd($id);
    return view('home', $post);
}

error

post (View: D:\xampp\htdocs\bima_1202174034\Modul5\resources\views\home.blade.php)

you need to change your code

public function index()
{
        $post = Post::all();

        return view('home', $post)  //This is not perfect way

        return view('home', compact('post'));//This is right way
}

Blade: Undefined Variable Inside Foreach Loop, Blade: Undefined Variable Inside Foreach Loop � php laravel laravel-5 foreach laravel-blade. I have a controller which gets an array of a User's� Laravel : @foreach Undefined variable. 0. Form issue in Laravel 5. See more linked questions. Related. 0. Laravel: Undefined Variable in View when sending mail. 5.

You must be use

public function index()
{
    $post=Post::all();
    return view('home', compact('post'));
}

or

 return view('home')->with('post', $post);

for good practice use

public function index()
{
    $posts = Post::all();
    return view('home', compact('posts'));
}

in view

@foreach($posts as $ps)
     //your logic 
@foreach

Undefined variable Laravel Foreach, You are passing album variable with view Album.blade.php, which is single object, not array of object so you can't iterate in a loop. I think you� Undefined variable: $ Laravel 5. Ask Question Undefined variable: categories. I insert this in the top of my @foreach

There are many ways to pass variable to view from controller but always use compact to pass variable from controller to view e.g

public function index()
{
        $post = Post::all();

        // dd($id);
        return view('home', compact('post'));
}

To know more about this kindly visit docs to read more about Passing Data To Views

Thanks

undefined variable: $loop in @foreach, Newest Reply. 1 of 13 replies August 21, 2019. zerodps's avatar zerodps � Laravel ErrorException (E_NOTICE) Undefined variable: loop. I could not find any Best Answer. @alkut You have that backwards foreach(file as index => img). I am getting this error: undefined variable. I read a lot of posts about it but, none of them helped with the problem i am facing. @tykus here's what

Undefined variable in foreach() but variable valid in dd(), I am using Laravel 5.3 to create a email template feature. It prompts Undefined variable: month (View:� Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Jobs Programming and related technical career opportunities

Undefined variable: data in view Laravel., But, I got this error always - > Undefined variable: data (View: <td> Name </td> <td> Email </td> <td> Images </td> </tr> @foreach($data as $value ) <tr> <td>� {note} Views rendered via @each do not inherit the variables from the parent view. If the child view requires these variables, you should use @foreach and @include instead. Stacks. Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout.

Undefined Variable Error in Laravel 5.2, The room gets saved in the database without a problem, but when I try to retrieve it using the foreach loop, I get the 'undefined variable rooms' error. I am using� The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Comments
  • return view('home', ['posts' => $posts]); the array keys are what the variables will be named
  • you just write return view('home', compact('post'));//This is right way
  • if you get your proper answer so please give upvote this answer. thank you so much..