public function index(Request $request) { $blogs = Blog::published(); if ($request->has('published_at')) { $published_at = $request->input('published_at'); $blogs = $blogs->whereBetween('published_at', array(Carbon::parse($published_at)->startOfMonth(), Carbon::parse($published_at)->endOfMonth())); } if ($request->has('tag')) { $tag = $request->input('tag'); $blogs = $blogs->whereHas('tags', function ($query) use($tag) { $query->where('id', $tag); }); } if ($request->has('sort')) { $sort = $request->input('sort'); $blogs = $blogs->orderBy($sort); } else { $blogs = $blogs->latest('published_at'); } $blogs = $blogs->simplePaginate(2); // $blogs = Blog::whereHas('tags', function ($query) { // $query->where('name', 'general'); // })->latest('published_at')->published()->simplePaginate(2); //$blogs = \App\Tag::where('name', 'general')->firstOrFail()->blogs()->latest('published_at')->published()->simplePaginate(2); //$blogs = Auth::user()->blogs()->latest('published_at')->published()->simplePaginate(2); //$blogs = Blog::latest('published_at')->published()->simplePaginate(2); return view('blogs/index', compact('blogs')); }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { // parent::boot($router); $router->bind('blogs', function ($id) { return \App\Blog::published()->findOrFail($id); }); }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { // parent::boot($router); $router->bind('blog', function ($id) { return Blog::published()->findOrFail($id); }); $router->bind('tags', function ($name) { return \App\Tag::where('name', $name)->firstOrFail(); }); $router->bind('projects', function ($id) { return \App\Project::findOrFail($id); }); $router->bind('pages', function ($name) { return \App\Page::where('location', $name)->firstOrFail(); }); }