Skip to content

Commit

Permalink
Queries enhancement for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
YasserElgammal committed Jan 21, 2023
1 parent 6c40d62 commit 2c0f0f2
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 22 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\Post;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CategoryController extends Controller
*/
public function index()
{
$categories = Category::all();
$categories = Category::with('user')->get();
return view('admin.category.index', compact('categories'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PageController extends Controller
{
public function index(){

$pages = Page::orderBy('id', 'desc')->paginate(15);
$pages = Page::with('user')->orderBy('id', 'desc')->paginate(15);
return view('admin.page.index', compact('pages'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()
*/
public function index()
{
$posts = Post::orderBy('id', 'desc')->paginate(15);
$posts = Post::with(['category', 'user', 'tags'])->orderByDesc('id')->paginate(15);
return view('admin.post.index', compact('posts'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TagController extends Controller
*/
public function index()
{
$tags = Tag::all();
$tags = Tag::with('posts')->get();
return view('admin.tag.index', compact('tags'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserController extends Controller
*/
public function index()
{
$users = User::orderBy('id', 'desc')->paginate(15);
$users = User::with('role')->orderBy('id', 'desc')->paginate(15);
return view('admin.user.index', compact('users'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

Expand All @@ -14,7 +15,6 @@ public function index()
{
// Get the active posts with (Category and User) details
$posts = Post::whereStatus(true)->with(['category', 'user'])->orderBy('id','desc')->paginate(10);

return view('index', compact('posts'));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PostController extends Controller
public function getPostBySlug($slug)
{
// I've Pass Slug to Get the Category per it's Slug
$post = Post::with('category')->whereStatus(true)->whereSlug($slug)->firstOrFail();
$post = Post::with(['category', 'user'])->whereStatus(true)->whereSlug($slug)->firstOrFail();

return view('post', compact('post'));
}
Expand Down
4 changes: 0 additions & 4 deletions app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class TagController extends Controller
{
public function getPostsPerTags($tag)
{
// $posts = $tag->posts();
$tags = Tag::whereName($tag)->firstOrFail()->publishedPosts();
// $tag = Tag::whereName($tag)->firstOrFail();

// dd($tags);

return view('tag', compact('tags'));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function user()
// I use this function to get Active Posts in the current category
public function publishedPosts()
{
return SELF::posts()->whereStatus(true)->orderBy('id','desc')->paginate(10);
return SELF::posts()->with('user')->whereStatus(true)->orderBy('id','desc')->paginate(10);
}
}
4 changes: 2 additions & 2 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function posts()
// I use this function to get Active Posts in the current Tag
public function publishedPosts()
{
return SELF::posts()->whereStatus(true)->orderBy('id', 'desc')->paginate(10);
return SELF::posts()->with(['user', 'category'])->whereStatus(true)->orderBy('id','desc')->paginate(10);
}

public function countTagsForPublishedPosts()
{
return SELF::publishedPosts()->count();
return SELF::posts()->whereStatus(true)->count();
}
}
8 changes: 4 additions & 4 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PostPolicy

public function viewAny(User $user)
{
return ($user->getRole('Writer') || $user->getRole('Admin'));
return $user->getRole('Writer');
}
/**
* Determine whether the user can create models.
Expand All @@ -22,7 +22,7 @@ public function viewAny(User $user)
*/
public function create(User $user)
{
return ($user->getRole('Writer') || $user->getRole('Admin'));
return $user->getRole('Writer');
}

/**
Expand All @@ -34,7 +34,7 @@ public function create(User $user)
*/
public function update(User $user, Post $post)
{
return (($user->id == $post->user->id) || $user->getRole('Admin'));
return $user->id == $post->user->id;
}

/**
Expand All @@ -46,6 +46,6 @@ public function update(User $user, Post $post)
*/
public function delete(User $user, Post $post)
{
return (($user->id == $post->user->id) || $user->getRole('Admin'));
return $user->id == $post->user->id;
}
}
15 changes: 13 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ public function boot()
{
$this->registerPolicies();

Gate::before(function ($user) {
if ($user->role->name == 'Admin') {
return true;
}
});

Gate::define('admin-login', function (User $user) {
return $user->getRole('Admin') || $user->getRole('Writer');
if ($user->role->name == 'Writer') {
return true;
}
});

Gate::define('admin-only', function (User $user) {
return $user->getRole('Admin');
if ($user->role->name == 'Admin') {
return true;
}
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"laravel/tinker": "^2.7"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.7",
"fakerphp/faker": "^1.9.1",
"laravel/breeze": "^1.16",
"laravel/pint": "^1.0",
Expand Down
152 changes: 151 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions storage/debugbar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 2c0f0f2

Please sign in to comment.