1

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)

1
  • 1
    return view('home', ['posts' => $posts]); the array keys are what the variables will be named Commented Nov 1, 2019 at 10:00

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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
}

2 Comments

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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.