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?
Your problem is simple you are "casting" parameter as exception which is wrong;
model
controller
sidenote
you do NOT need this line in your code:
Full example (should work, not tested)
model Post.php
controller PostController.php
For ModelNotFoundException follow this answer or