skip to Main Content

I’m new to laravel and created a mini program using a bit of help of twitter bootstrap.

I’m trying to access this route:

http://127.0.0.1:8000/posts/create

And I get these errors:

2/2 ErrorException in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4:
Trying to get property of non-object (View: C:xampphtdocslaravelalquirozlaravelresourcesviewsshow-solo.blade.php)

in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\xampp\htdocs\laravel\alquirozlaravel\storage\framework\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in CompilerEngine.php line 59
at CompilerEngine->get('C:\xampp\htdocs\laravel\alquirozlaravel\resources\views/show-solo.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->IlluminateRouting{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->IlluminateFoundationHttp{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->IlluminateRouting{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\xampp\htdocs\laravel\alquirozlaravel\public\index.php') in server.php line 21

1/2 ErrorException in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4:
Trying to get property of non-object

in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at HandleExceptions->handleError(8, 'Trying to get property of non-object', 'C:\xampp\htdocs\laravel\alquirozlaravel\storage\framework\views\73427cb411f683691ba00d0846f7eda3c61bff74.php', 4, array('__path' => 'C:\xampp\htdocs\laravel\alquirozlaravel\storage\framework\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null), 'obLevel' => 1, '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at include('C:\xampp\htdocs\laravel\alquirozlaravel\storage\framework\views\73427cb411f683691ba00d0846f7eda3c61bff74.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\xampp\htdocs\laravel\alquirozlaravel\storage\framework\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in CompilerEngine.php line 59
at CompilerEngine->get('C:\xampp\htdocs\laravel\alquirozlaravel\resources\views/show-solo.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->IlluminateRouting{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->IlluminateFoundationHttp{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->IlluminateRouting{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\xampp\htdocs\laravel\alquirozlaravel\public\index.php') in server.php line 21

Here are my routes at the moment:

Route::get('/posts', 'PostController@index');
Route::get('/posts/{id}', 'PostController@showById');
Route::get('/posts/create', 'PostController@showForm');
Route::post('/posts', 'PostController@store');

Route::post('/posts/{post}/comments', 'CommentController@store');

My Controllers:
(PostController)

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppPost;

class PostController extends Controller
{
    public function index() {
        $posts = Post::latest()->get();

        return view('index', compact('posts'));
    }

    public function showById($id)
    {
        $post = Post::find($id);

        return view('show-solo', compact('post'));
    }

    public function showForm()
    {
        return view('new-post');
    }

    public function store(Request $request) {
        $this->validate(request(), [
            'title' => 'required|min:2',
            'body' => 'required|min:10'
        ]);

        Post::create([
            'title' => $request->title,
            'body' => $request->body
        ]);

        return redirect('/posts');
    }
}

(CommentController)

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppPost;
use AppComment;

class CommentController extends Controller
{
    // Convention to POST ADD
    public function store(Post $post)
    {
        Comment::create([
            'body' => request('body'),
            'post_id' => $post->id
        ]);

        // return back() works too
        return back();
    }
}

The Blades in question i think:
(show-solo.blade.php)

@extends('master')

@section('content')
    <div class="col-sm-8 blog-main">
        <div class="blog-post">
            <h2>Post number: {{ $post->id }}</h2>

            <h2 class="blog-post-title">
                <a class="title-link" href="/posts/{{ $post->id }}">{{ $post->title }}</a>
            </h2>

            <!-- {{ $post->created_at->toFormattedDateString() }} -->
            <p class="blog-post-meta">{{ $post->created_at->diffForHumans() }} by <a href="#">Mark</a></p>
            {{ $post->body }}

            <hr />

            @include('partials.error')

            @include('partials.post-comment')
        </div>
    </div>
@endsection

(new-post.blade.php)

@extends('master')

@section('content')
    <div class="col-sm-8 blog-main">
        <h1>Publish a Post<hr /></h1>

        <form method="POST" action="/posts">
            {{ csrf_field() }}

            <div class="form-group">
                <label for="title">Title:</label>
                <input type="text" class="form-control" id="title" name="title">
            </div>

            <div class="form-group">
                <label for="body">Body</label>
                <textarea name="body" id="body" class="form-control" cols="30" rows="10"></textarea>
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary">Publish</button>
            </div>

            @include('partials.error')
        </form>
    </div>
@endsection

(master.blade.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Blog Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="{{ URL::asset('css/bootstrap.min.css') }}">

    <!-- Custom styles for this template -->
    <link rel="stylesheet" href="{{ URL::asset('css/blog.css') }}">
    <link rel="stylesheet" href="{{ URL::asset('css/style.css') }}">
</head>

<body>      
    @include('partials.nav')

    <div class="container">

        <div class="row">
            @yield('content')

            @include('partials.sidebar')

        </div><!-- /.row -->

    </div><!-- /.container -->

    @include('partials.footer')

</body>
</html>

Here are my models if it helps:
(Post.php)

<?php

namespace App;

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

(Comment.php)

<?php

namespace App;

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

Before the posts/create route works fine and now it won’t. When I try to remove the route Route::get(‘/posts/{id}’, ‘PostController@showById’); the post/create route works properly again. What Am I missing? Any help would be greatly appreciated, Thanks.

3

Answers


  1. The problem is that your create route is listed after the general posts/{id} route. Laravel handles every segment after the /posts/ as an id and ignores the “special” create route.

    Swap those two lines like this:

    Route::get('/posts/create', 'PostController@showForm');
    Route::get('/posts/{id}', 'PostController@showById');
    

    Furthermore I’d suggest you to use resource controllers which makes your life a bit easier with routing!

    Login or Signup to reply.
  2. Change your route

    From

    Route::get('/posts/{id}', 'PostController@showById');
    

    To

    Route::get('/posts/{id}', 'PostController@showById')->where('id', '[0-9]+');
    
    Login or Signup to reply.
  3. If you use resource controllers and still have the same problem , you should check your database because something within the columns may be NULL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search