AllThingsBristol
A fully responsive website allowing users to find hidden gems and explore the city of Bristol
During the developement process I expanded my knowledge of the laravel framework as well as usitilising blade components to speed up development of the front end UI. Using my knowledge gained from famouscelebstats.com I was able to make a more organised CRUD which utilised more of the Laravel framework and blade templating.
This website was a CRUD website which used standard Create, Read, Update and Delete Functions. Here is an example of one of the controller classes I made. This controller is for the posts.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Auth;
use App\Http\Controllers\CategoryController;
use App\Models\Category;
use App\Models\Post;
use App\Models\Role;
use App\Models\User;
class PostController extends Controller
{
public function index()
{
return view('index', [
'home_posts' => $this->getCachedHomePosts(),
'posts' => Post::orderBy('id', 'desc')->where('published', '=', '1')->take(12)->get(),
'categories' => CategoryController::getCachedCategories()
]);
}
public function show(Post $post){
if($post->published != 1){
if(!auth()){
return redirect('/');
}
if(Auth::id() != $post->user_id && !(in_array(Auth::user()->role_id, [Role::IS_ADMIN, Role::IS_EDITOR]))){
return redirect('/');
}
}
return view('/post.show', [
'post' => $post,
'categories' => CategoryController::getCachedCategories()
]);
}
public function recent(){
return view('/index', [
'posts' => Post::orderBy('id', 'desc')->take(12)->get(),
'categories' => CategoryController::getCachedCategories()
]);
}
public function create(){
if(!auth()){
return redirect('');
}
if(!in_array(auth()->user()->role_id, [Role::IS_ADMIN, Role::IS_EDITOR, Role::IS_WRITER])) {
return redirect('');
}
return view('/post.create', [
'categories' => CategoryController::getCachedCategories()
]);
}
public function edit(Post $post){
if(!auth()){
return redirect('/post/'. $post->slug);
}
if(Auth::id() != $post->user_id && (!in_array(Auth::user()->role_id, [Role::IS_ADMIN, Role::IS_EDITOR]))){
return redirect('/post/'. $post->slug);
}
return view('/post.edit', [
'post' => $post,
'categories' => CategoryController::getCachedCategories()
]);
}
public function getCachedHomePosts(){
// cache()->forget('home_page');
$homePosts = cache()->remember('home_page', 60*60*24, function(){
return Post::whereIn('home_page', [1, 2, 3, 4, 5, 6])->where('published', '=', '1')->get();
});
return $homePosts;
}
public function store(Request $request){
if(!auth()){
$request = null;
$formFields = [];
return redirect('/');
}
if(!in_array(Auth::user()->role_id, [Role::IS_ADMIN, Role::IS_EDITOR, Role::IS_WRITER])){
$request = null;
$formFields = [];
return redirect('/');
}
$formFields = $request->validate([
'head_title' => 'required',
'title' => 'required',
'slug' => ['required', Rule::unique('posts', 'slug')],
'home_page' => 'required',
'category' => 'required',
'sub_category' => 'nullable',
'content' => 'required',
'credit' => 'nullable',
'description' => 'required'
]);
$formFields['published'] = "0";
$formFields['user_id'] = Auth::id();
if($request->hasFile('img_url')){
$formFields['img_url'] = $request->file('img_url')->store('images', 'public');
}
Post::create($formFields);
return redirect('/post/'. $formFields['slug']);
}
public function update(Request $request, Post $post){
if(!auth()){
return redirect('/post/'. $post->slug);
}
if(Auth::id() != $post->user_id && (!in_array(Auth::user()->role_id, [Role::IS_ADMIN, Role::IS_EDITOR]))){
return redirect('/post/'. $post->slug);
}
$formFields = $request->validate([
'head_title' => 'required',
'title' => 'required',
'slug' => 'required',
'home_page' => 'required',
'category' => 'required',
'sub_category' => 'nullable',
'content' => 'required',
'credit' => 'nullable',
'description' => 'required',
'published' => 'required'
]);
$formFields['user_id'] = Auth::id();
if($request->hasFile('img_url')){
$formFields['img_url'] = $request->file('img_url')->store('images', 'public');
}
$post->update($formFields);
return redirect(url('post/'.$post->slug));
}
}