Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit 9bf9de2

Browse files
committed
Add tutorials frontend fonctionnalities and update migrations
1 parent bbd961b commit 9bf9de2

15 files changed

+322
-71
lines changed

app/Http/Controllers/BlogController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function category(string $slug)
5555
$total = $this->postRepository->online()->count();
5656
$categories = $this->categoryRepository->categoryType('POST');
5757
$category = $this->categoryRepository->findBySlug($slug);
58-
$posts = $category->posts()->where('status', 'PUBLISHED')->paginate('6');
58+
$posts = $category->posts()->where('status', 'PUBLISHED')->paginate(6);
5959

6060
return view('frontend.blog.home', compact('posts', 'total', 'categories', 'category'));
6161
}

app/Http/Controllers/TutorialController.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
5+
use App\Repositories\CategoryRepository;
6+
use App\Repositories\TutorialRepository;
67

78
class TutorialController extends Controller
89
{
9-
public function __construct()
10+
/**
11+
* @var TutorialRepository
12+
*/
13+
private $repository;
14+
15+
/**
16+
* @var CategoryRepository
17+
*/
18+
private $categoryRepository;
19+
20+
/**
21+
* TutorialController constructor.
22+
* @param TutorialRepository $repository
23+
* @param CategoryRepository $categoryRepository
24+
*/
25+
public function __construct(TutorialRepository $repository, CategoryRepository $categoryRepository)
1026
{
27+
$this->repository = $repository;
28+
$this->categoryRepository = $categoryRepository;
1129
}
1230

1331
/**
@@ -17,19 +35,27 @@ public function __construct()
1735
*/
1836
public function index()
1937
{
20-
$tutorials = [];
38+
$tutorials = $this->repository->paginate();
39+
$populars = $this->repository->popular();
40+
$total = $this->repository->online()->count();
41+
$categories = $this->categoryRepository->categoryType('TUTORIAL');
2142

22-
return view('frontend.tutorials.index', compact('tutorials'));
43+
return view('frontend.tutorials.index', compact('tutorials', 'populars', 'categories', 'total'));
2344
}
2445

2546
/**
2647
* Display tutorial single Category
2748
*
28-
* @param string $category
49+
* @param string $slug
50+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2951
*/
30-
public function category(string $category)
52+
public function category(string $slug)
3153
{
54+
$categories = $this->categoryRepository->categoryType('TUTORIAL');
55+
$category = $this->categoryRepository->findBySlug($slug);
56+
$tutorials = $category->tutorials()->where('is_published', true)->paginate(9);
3257

58+
return view('frontend.tutorials.category', compact('tutorials', 'categories', 'category'));
3359
}
3460

3561
/**
@@ -40,6 +66,8 @@ public function category(string $category)
4066
*/
4167
public function post(string $slug)
4268
{
43-
return view('frontend.tutorials.post');
69+
$tutorial = $this->repository->findBySlug($slug);
70+
71+
return view('frontend.tutorials.post', compact('tutorial'));
4472
}
4573
}

app/Models/Category.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
class Category extends \TCG\Voyager\Models\Category
6+
{
7+
protected $fillable = ['slug', 'name', 'type'];
8+
9+
public function tutorials()
10+
{
11+
return $this->hasMany(Tutorial::class);
12+
}
13+
}

app/Models/Tutorial.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\User;
6+
use Illuminate\Database\Eloquent\Model;
7+
use TCG\Voyager\Facades\Voyager;
8+
9+
class Tutorial extends Model
10+
{
11+
/**
12+
* The attributes that are mass assignable.
13+
*
14+
* @var array
15+
*/
16+
protected $fillable = [
17+
'title', 'slug', 'content', 'is_published', 'image', 'user_id', 'category_id'
18+
];
19+
20+
/**
21+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
22+
*/
23+
public function category()
24+
{
25+
return $this->belongsTo(Voyager::modelClass('Category'));
26+
}
27+
28+
/**
29+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
30+
*/
31+
public function user()
32+
{
33+
return $this->belongsTo(User::class);
34+
}
35+
}

app/Repositories/CategoryRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace App\Repositories;
99

10-
use TCG\Voyager\Models\Category;
10+
use App\Models\Category;
1111

1212
class CategoryRepository
1313
{
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <[email protected]>
5+
* @link https://github.com/Mckenziearts/laravel-command
6+
*/
7+
8+
namespace App\Repositories;
9+
10+
use App\Models\Tutorial;
11+
12+
class TutorialRepository
13+
{
14+
/**
15+
* @var Tutorial
16+
*/
17+
private $model;
18+
19+
/**
20+
* TutorialRepository constructor.
21+
* @param Tutorial $model
22+
*/
23+
public function __construct(Tutorial $model)
24+
{
25+
$this->model = $model;
26+
}
27+
28+
/**
29+
* Return a new instance of Tutorial Model
30+
*
31+
* @return Tutorial
32+
*/
33+
public function newInstance()
34+
{
35+
return $this->model->newInstance();
36+
}
37+
38+
/**
39+
* Get all book categories from the database
40+
*
41+
* @return \Illuminate\Database\Eloquent\Collection|static[]
42+
*/
43+
public function online()
44+
{
45+
return $this->model
46+
->newQuery()
47+
->where('is_published', true)
48+
->get();
49+
}
50+
51+
/**
52+
* Get last post from the database
53+
*
54+
* @param int $limit
55+
* @return \Illuminate\Database\Eloquent\Collection|static[]
56+
*/
57+
public function popular(int $limit = 2)
58+
{
59+
return $this->model->newQuery()
60+
->with('user')
61+
->where('is_published', true)
62+
->orderBy('created_at', 'DESC')
63+
->limit($limit)
64+
->get();
65+
}
66+
67+
/**
68+
* Get all books categories by count pagination
69+
*
70+
* @param int $results
71+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
72+
*/
73+
public function paginate(int $results = 6)
74+
{
75+
return $this->model->newQuery()
76+
->with('user')
77+
->where('is_published', true)
78+
->orderBy('created_at', 'DESC')
79+
->paginate($results);
80+
}
81+
82+
/**
83+
* Return a model with the id set in parameter
84+
*
85+
* @param int $id
86+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
87+
*/
88+
public function find(int $id)
89+
{
90+
return $this->model->newQuery()
91+
->findOrFail($id);
92+
}
93+
94+
/**
95+
* Return a model with the slug set in parameter
96+
*
97+
* @param string $slug
98+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
99+
*/
100+
public function findBySlug(string $slug)
101+
{
102+
return $this->model->newQuery()
103+
->with('category')
104+
->where('slug', $slug)
105+
->firstOrFail();
106+
}
107+
}

app/User.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App;
44

5+
use App\Models\Tutorial;
56
use Illuminate\Notifications\Notifiable;
6-
use Illuminate\Foundation\Auth\User as Authenticatable;
77
use TCG\Voyager\Models\Post;
88

99
class User extends \TCG\Voyager\Models\User
@@ -35,4 +35,12 @@ public function posts()
3535
{
3636
return $this->hasMany(Post::class);
3737
}
38+
39+
/**
40+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
41+
*/
42+
public function tutorials()
43+
{
44+
return $this->hasMany(Tutorial::class);
45+
}
3846
}

database/migrations/2018_07_01_142616_create_packages_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->string('title')->unique();
1919
$table->string('slug')->unique();
2020
$table->text('content');
21+
$table->text('resume');
2122
$table->boolean('is_approved')->default(false);
2223
$table->string('image');
2324
$table->integer('category_id')->unsigned();

database/migrations/2018_07_01_142616_create_tutorials_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->string('title')->unique();
1919
$table->string('slug')->unique();
2020
$table->text('content');
21+
$table->text('resume');
2122
$table->boolean('is_published')->default(false);
2223
$table->string('image')->nullable();
2324
$table->integer('user_id')->unsigned();

database/migrations/2018_07_01_150123_add_type_collumn_to_categories_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AddTypeCollumnToCategoriesTable extends Migration
1414
public function up()
1515
{
1616
Schema::table('categories', function (Blueprint $table) {
17-
$table->enum('type', ['POST', 'TUTORIAL', 'PACKAGES'])->default('POST');
17+
$table->string('type')->default('POST');
1818
});
1919
}
2020

0 commit comments

Comments
 (0)