0

model Post.php

<?php namespace App\models;

use Illuminate\Database\Eloquent\Model;
use PhpSpec\Exception\Exception;

class Post extends Model
{

    public static function get(Exception $id)
    {
        try {
            $post = Post::where('id', '=', $id)->firstOrFail();
        } catch (isHttpException $e) {
            return abort(404, 'Post Not Found');
        }
        return $post;
    }
}

controller PostController.php

<?php namespace App\Http\Controllers;
use App\Models\Post;
use Psy\Exception\Exception;

class PostController extends Controller
{

    public function getPost(Exception $id)
    {
        $id = (int)$id;
        $post = Post::get($id);
        return view('post.showPost', ['post'=>$post]);
    }
}

Error I get:

BindingResolutionException in Container.php line 785: 
Target [Psy\Exception\Exception] is not instantiable.

I dont understand the error/exception. What is wrong with my code?

Kyslik
  • 8,217
  • 5
  • 54
  • 87

1 Answers1

1

Your problem is simple you are "casting" parameter as exception which is wrong;

model

public static function get($id) {...}

controller

public function getPost($id) {...}

sidenote

you do NOT need this line in your code:

$id = (int)$id;

Full example (should work, not tested)

model Post.php

<?php namespace App\models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts'; //assuming posts table exists...

    public static function getById($id)
    {
        return Post::findOrFail($id);
    }
}

controller PostController.php

<?php namespace App\Http\Controllers;
use App\Models\Post;

class PostController extends Controller
{

    public function getPost($id)
    {
        $post = Post::getById($id);
        return view('post.showPost')->withPost($post);
    }
}

For ModelNotFoundException follow this answer or

With commit from today (9db97c3), all you need to do is add a 404.blade.php file in the /resources/views/errors/ folder and it will find and display it automatically.

Community
  • 1
  • 1
Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • thanks for answer, i removed this code $id = (int)$id; but the old error – AlexanderKim Jun 09 '15 at 15:18
  • Did you also rewrite declarations of functions? The CORE of the answer? – Kyslik Jun 09 '15 at 15:19
  • public function getPost($id), public static function get($id) changed error - ModelNotFoundException in Builder.php line 151: No query results for model [App\models\Post]. – AlexanderKim Jun 09 '15 at 15:23
  • That means that ID you provide as parameter to the function(s) does not exist in your table. You really need to read more [documentation](http://laravel.com/docs/5.0) or watch some Laracasts. – Kyslik Jun 09 '15 at 15:24
  • check to make in the controller? – AlexanderKim Jun 09 '15 at 15:26
  • There is too much asking, what I mean original question has been answered. You have problem understanding the basics of Laravel you need to study documentation first. Work with documentation, its the only friend here. Wait for a few moments I am going to update my answer. – Kyslik Jun 09 '15 at 15:31