/** * Show the application dashboard. * * @param null $category_alias * @return \Illuminate\Http\Response */ public function index($category_alias = null) { if (is_null($category_alias) || empty($category_alias) || $category_alias == '/') { $posts = Post::wherePublished(true)->orderBy('created_at', 'desc')->paginate(10); } else { // get category posts $category = Category::whereAlias($category_alias)->get()->first(); $posts = $category->posts()->wherePublished(true)->paginate(10); } return view('index')->with('posts', $posts); }
public function grid(Request $request) { if ($request->ajax() && $request->exists('req')) { $req = json_decode($request->get('req')); $perPage = $req->page->perPage; $from = $perPage * ($req->page->currentPage - 1); $query = Post::join('users', 'user_id', '=', 'users.id')->select(['posts.id as posts.id', 'users.name as users.name', 'posts.title as posts.title', 'posts.published as posts.published', 'posts.created_at as posts.created_at', 'posts.updated_at as posts.updated_at']); if (!is_null($req->sort)) { foreach ($req->sort as $key => $value) { $query->orderBy($key, $value); } } if (!is_null($req->filter)) { foreach ($req->filter as $key => $value) { switch ($value->operator) { case 'IsEqualTo': $query->where($key, '=', $value->operand1); break; case 'IsNotEqualTo': $query->where($key, '<>', $value->operand1); break; case 'StartWith': $query->where($key, 'LIKE', $value->operand1 . '%'); break; case 'Contains': $query->where($key, 'LIKE', '%' . $value->operand1 . '%'); break; case 'DoesNotContains': $query->where($key, 'NOT LIKE', '%' . $value->operand1 . '%'); break; case 'EndsWith': $query->where($key, 'LIKE', '%' . $value->operand1); break; case 'Between': $query->whereBetween($key, array($value->operand1, $value->operand2)); break; } } } $total = $query->count(); $query->take($perPage)->skip($from); $data = $query->get(); $totalPage = ceil($total / $perPage); $countDataPerPage = count($data); $page = array("currentPage" => $req->page->currentPage, "lastPage" => $totalPage, "total" => $total, "from" => $from + 1, "count" => $countDataPerPage, "perPage" => $perPage); $result = ['data' => $data, 'page' => $page]; return json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $post = Post::find($id); $title = $post->title; return view('posts.show')->with(['title' => $title, 'post' => $post]); }
public function run() { \StartPoint\Post::updateOrCreate(['id' => 1, 'title' => 'test', 'body' => 'test content', 'user_id' => 1]); \StartPoint\Post::updateOrCreate(['id' => 2, 'title' => 'test2', 'body' => 'test2 content', 'user_id' => 1]); }