-1

I am new to laravel in here i am just creating a simple app in which user can login and register and after that write post in dashboard after logging in, I am trying to display user posts data in dashboard which is saved in database, But i am getting this error:

Undefined variable: posts (View: C:\xampp\htdocs\practiseapp\resources\views\dashboard.blade.php)

i think i have defined the posts,I can't figure out what's wrong,Any help would be appreciated Thanks....

My dashboard:

@extends('layout.master')

@section('content')
<div class="col-sm-6 col-sm-offset-3">
    <header><h3>What do you wanna say</h3></header>
    <form method="post" action="{{route('post.create')}}">
        {{csrf_field()}}
        <div class="panel-body">
        <div class="form-group">
            <textarea class="form-control" name="body" id="body" rows="5" placeholder="Enter your post">

            </textarea>
        </div>
        <button type="submit" class="btn btn-primary">Create post</button>

    </form>
    </div>

</div>

<section class="row-posts">
    <div class="col-md-6 col-sm-offset-3">
        <header><h3>Other people posts</h3></header>

        @foreach($posts as $post)
        <article class="post">
            <p>{{ $post->body }}</p>
            <div class="info">
                posted by {{ $post->user->name}} on {{$post->created_at}}
            </div>
            <div class="interaction">
                <a href="#">Like</a>|
                <a href="#">Disike</a>|
                <a href="#">Edit</a>|
                <a href="#">Delete</a>  
            </div>
        </article>
        @endforeach


    </div>

</section>
@endsection

PostController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Post;
use App\UserTypes;

use Auth;
use Hashids;
use Redirect;
use Illuminate\Http\Request;
use Hash;

class PostController extends Controller
{

    public function show()
    {
        //Fetching all the posts from the database
        $posts = Post::all();
        return view('dashboard',['posts'=> $posts]);
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'body' => 'required'

        ]);

        $post = new Post;
        $post->body = $request->body;
        $request->user()->posts()->save($post);

        return redirect()->route('dashboard');      
    }
}
  • 4
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – treyBake May 21 '19 at 15:17
  • @treyBake None of this helped Thanks –  May 21 '19 at 15:25
  • Yeah, that question is for PHP in general. Doesn't really address the issue with Laravel. I think `return view('dashboard',['posts'=> $posts]);` is valid syntax, but try `return view("dashboard")->with(["posts" => $posts]);`; see if alternative syntax makes a difference. If not, might be a different issue. – Tim Lewis May 21 '19 at 15:29

2 Answers2

1

I think you're pretty close, but I have a few ideas that might help you.

First things first, have you checked that your routes are set correctly in routes/web.php? If you've used some of the examples in the Laravel documentation, it's possible that your route is returning the view without using the Controller you've written. If you've got something like this:

Route::get('/', function () {
    return view('dashboard');
});

...then you'll probably want to replace it with something like this:

Route::get( '/', 'PostController@show );

There are many different ways of managing your routing - the Laravel Docs will do a good job at explaining some of these.

Also, when passing things from the Controller over to the View, I like assigning my objects to an associative array and then passing that array when using the view method. It's entirely personal preference, but you might find it useful. Something a bit like this:

public function show()
{
    // Create output array - store things in here...
    $output = [];

    $output[ "posts" ] = Post::all();

    // Render the Dashboard view with data...
    return view( 'dashboard', $output );
}

Hope some of this helps!

Maisy
  • 11
  • 3
0

Try the below code,

<?php

    namespace App\Http\Controllers;

    use App\Http\Requests;
    use App\Post;
    use App\UserTypes;

    use Auth;
    use Hashids;
    use Redirect;
    use Illuminate\Http\Request;
    use Hash;

    class PostController extends Controller
    {

        public function show()
        {
            //Fetching all the posts from the database
            $posts = Post::get();
            return view('dashboard', compact('posts'));
        }

        public function store(Request $request)
        {
            $this->validate($request,[
                'body' => 'required'

            ]);

            $post = new Post;
            $post->body = $request->body;
            $request->user()->posts()->save($post);

            return redirect()->route('dashboard');      
        }
    }