/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { $router->bind('posts', function ($slug) { return Post::whereSlug($slug)->first(); }); parent::boot($router); }
/** @test */ public function it_assigns_category() { $post = factory(App\Gazette\Models\Post::class)->create(); $expectedCategory = factory(App\Gazette\Models\Category::class)->create(); $this->assertNotEquals($post->category->name, $expectedCategory->name); $post->assignCategory($expectedCategory)->save(); $actualPost = Post::find($post->id); $this->assertEquals($expectedCategory->name, $actualPost->category->name); }
/** * Display a listing of the resource. * * @return Response */ public function index() { $data['limit'] = Input::get('limit'); $data['page'] = Input::get('page'); $validator = Validator::make($data, ['limit' => 'numeric', 'page' => 'numeric']); if ($validator->fails()) { return $this->respondUnprocessableEntity(); } $limit = Input::get('limit') ?: 50; $limit = $limit < 1 || $limit > 100 ? 10 : $limit; $posts = Post::paginate($limit); return $this->respondWithPagination($posts, ['posts' => $this->transformer->transformCollection($posts->all())]); }
/** * Get latest posts * * @param $total * @return mixed */ public function getLatest($total = 10) { $total = $total > 0 && $total <= 100 ? $total : 10; return Post::with('author', 'category')->orderBy('created_at')->take($total)->get(); }