Support the ongoing development of Laravel.io →
posted 11 years ago
Views

http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/

I followed this great tutorial, however i'm running into some problems now.

Im getting the following error message:

ErrorException
Creating default object from empty value

I think it has something to do with my routes, however I am not sure. Ill post the code in question below:

PostController

    public function showPost(Post $post)
{
    $comments = $post->comments()->where('approved', '=', 1)->get();
    $this->layout->title = $post->title;
    $this->layout->main = View::make('home')->nest('content', 'posts.single', compact('post', 'comments'));
}

public function newPost()
{
    $this->layout->title = 'New Post';
    $this->layout->main = View::make('dash')->nest('content', 'posts.new');
}

public function editPost(Post $post)
{
    $this->layout->title = 'Edit Post';
    $this->layout->main = View::make('dash')->nest('content', 'posts.edit', compact('post'));
}

public function deletePost(Post $post)
{
    $post->delete();
    return Redirect::route('post.list')->with('success', 'Post is deleted!');
}

routes.php:

/* Model Bindings */
Route::model('post', 'Post');
Route::model('comment', 'Comment');

/* User routes */
Route::get('/post/{post}/show', ['as' => 'post.show', 'uses' => 'PostController@showPost']);
Route::post('/post/{post}/comment', ['as' => 'comment.new', 'uses' => 'CommentController@newComment']);

/* Admin routes */
Route::group(['prefix' => 'admin', 'before' => 'auth'], function () {
/*get routes*/
Route::get('dash-board', function () {
    $layout = View::make('master');
    $layout->title = 'DashBoard';
    $layout->main = View::make('dash')->with('content', 'Hi admin, Welcome to Dashboard!');
    return $layout;

});
Route::get('/post/list', ['as' => 'post.list', 'uses' => 'PostController@listPost']);
Route::get('/post/new', ['as' => 'post.new', 'uses' => 'PostController@newPost']);
Route::get('/post/{post}/edit', ['as' => 'post.edit', 'uses' => 'PostController@editPost']);
Route::get('/post/{post}/delete', ['as' => 'post.delete', 'uses' => 'PostController@deletePost']);
Route::get('/comment/list', ['as' => 'comment.list', 'uses' => 'CommentController@listComment']);
Route::get('/comment/{comment}/show', ['as' => 'comment.show', 'uses' => 'CommentController@showComment']);
Route::get('/comment/{comment}/delete', ['as' => 'comment.delete', 'uses' =>     'CommentController@deleteComment']);

/*post routes*/
Route::post('/post/save', ['as' => 'post.save', 'uses' => 'PostController@savePost']);
Route::post('/post/{post}/update', ['as' => 'post.update', 'uses' => 'PostController@updatePost']);
Route::post('/comment/{comment}/update', ['as' => 'comment.update', 'uses' => 'CommentController@updateComment']);

});

/* Home routes */
Route::controller('pages.home', 'BlogController');

/* View Composer */
View::composer('sidebar', function ($view) {
$view->recentPosts = Post::orderBy('id', 'desc')->take(5)->get();
});

Im very well aware that this might be like looking for a needle in a haystack, but I hope someone might be able to help.

Thanks!

Last updated 3 years ago.
0

Make sure:

 protected $layout='master';

is set in your BaseController, provided that your master layout is named as master.blade.php. This will solve your problem!

Last updated 3 years ago.
0

Thank you very much Usman! It worked as you described, and solved the problem, only to reveal a new one.

In my view, I get the following error when I try to save a post to the database:

ErrorException
Undefined variable: content (View: /var/www/app/views/dash.blade.php)

I cannot seem to find the $content variable anywhere?

Last updated 3 years ago.
0

I get the undefined variable $content error, however it still saves to my database?

Last updated 3 years ago.
0

can you share your savePost controller action? And $content variable is used inside dash.blade.php see for the line:

    <div class="content">
        @if(Session::has('success'))
        <div data-alert class="alert-box round">
            {{Session::get('success')}}
            <a href="#" class="close">&times;</a>
        </div>
        @endif
        {{$content}}
    </div>
Last updated 3 years ago.
0

Hey usm4n! Sorry for getting back to you this late, I didnt see that you had replied to me.

This is part of the controller:

public function showPost(Post $post) { $comments = $post->comments()->where('approved', '=', 1)->get(); $this->layout->title = $post->title; $this->layout->main = View::make('home')->nest('content', 'post.single', compact('post', 'comments')); }

public function newPost()
{
    $this->layout->title = 'New Post';
    $this->layout->main = View::make('dash')->nest('content', 'post.new');
}

public function editPost(Post $post)
{
    $this->layout->title = 'Edit Post';
    $this->layout->main = View::make('dash')->nest('content', 'post.edit', compact('post'));
}

public function deletePost(Post $post)
{
    $post->delete();
    return Redirect::route('post.list')->with('success', 'Post is deleted!');
}

/* post functions */
public function savePost()
{
    $post = [
        'title' => Input::get('title'),
        'content' => Input::get('content'),
    ];
    $rules = [
        'title' => 'required',
        'content' => 'required',
    ];
    $valid = Validator::make($post, $rules);
    if ($valid->passes()) {
        $post = new Post($post);
        $post->comment_count = 0;
        $post->read_more = (strlen($post->content) > 120) ? substr($post->content, 0, 120) : $post->content;
        $post->save();
        return Redirect::to('dash')->with('success', 'Post is saved!');
    } else
        return Redirect::back()->withErrors($valid)->withInput();
}
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.