Skip to content

Commit

Permalink
fixed post categorization
Browse files Browse the repository at this point in the history
  • Loading branch information
chetans9 committed Oct 16, 2022
1 parent d8e9a36 commit 0ba6aeb
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 96 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 14,11 @@ RUN apk add busybox-extras
RUN apk add nano
RUN apk add curl
RUN apk add libxml2 libxslt-dev
RUN apk add jpeg-dev libpng-dev

# COPY ./docker/php/config/php.ini /usr/local/etc/php

#RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-configure gd --with-jpeg
RUN docker-php-ext-install pdo pdo_mysql gd zip soap


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 5,7 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\PostCategoriesModel;
use App\Models\CategoriesModel;

class AdminPostsCategoriesController extends Controller
{
Expand All @@ -23,7 23,7 @@ public function __construct()
public function index()
{

$list = PostCategoriesModel::paginate(50);
$list = CategoriesModel::paginate(50);


return view('admin.post_categories.list', compact('list'));
Expand Down Expand Up @@ -54,7 54,7 @@ public function store(Request $request)
]);
$inputs = $request->all();

$status = PostCategoriesModel::create($inputs);
$status = CategoriesModel::create($inputs);
$request->session()->flash('success', 'New Category created Successfully!');
return redirect("admin/posts-categories");

Expand All @@ -70,7 70,7 @@ public function store(Request $request)
public function edit($id)
{

$post_category = PostCategoriesModel::findOrFail($id);
$post_category = CategoriesModel::findOrFail($id);
$data['post_category'] = $post_category;
return view('admin.post_categories.edit', $data);

Expand All @@ -89,7 89,7 @@ public function update(Request $request, $id)
'name' => 'required',
]);
$inputs = $request->all();
$postCategory = PostCategoriesModel::findOrFail($id);
$postCategory = CategoriesModel::findOrFail($id);
$postCategory->fill($inputs);
$postCategory->save();
$request->session()->flash('success', 'Category Updated Successfully!');
Expand All @@ -104,7 104,7 @@ public function update(Request $request, $id)
*/
public function destroy($id)
{
$postCategory = PostCategoriesModel::findOrFail($id);
$postCategory = CategoriesModel::findOrFail($id);
$postCategory->delete($id);
$request->session()->flash('info','Category deleted Successfully');
return redirect(route('posts-categories.index'));
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/Admin/Posts/AdminPostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@
use Illuminate\Support\Str;
use App\Models\PostsModel;
use App\Repositories\UserRepository;
use App\Models\PostCategoriesModel;
use App\Models\CategoriesModel;
use App\Repositories\TagsRepository;
use Auth;
use Yajra\Datatables\Datatables;
Expand Down Expand Up @@ -64,7 64,8 @@ public function index(Request $request)
public function create()
{
$data = array();
$categories = PostCategoriesModel::pluck("name","id");
//Get key value pair data from categories table for populating in dropdown:
$categories = CategoriesModel::pluck("name","id");
$data['post_tags'] = array();
$data['categories'] = $categories;
return view("admin.posts.create",$data);
Expand All @@ -83,7 84,7 @@ public function store(Request $request)
'title' => 'required',
'content' => 'required',
'active' => 'required',
'categories'=>'required|array|min:1',
'category_id'=>'required|numeric',
'featured_image'=>'required'
]);
$inputs = $request->all();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Blog/PostCategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\PostCategoriesModel;
use App\Models\CategoriesModel;


class PostCategoriesController extends Controller
Expand All @@ -18,7 18,7 @@ class PostCategoriesController extends Controller
*/
public function show($id)
{
$category = PostCategoriesModel::findOrFail($id);
$category = CategoriesModel::findOrFail($id);
$posts = $category->posts()->where('posts.active','1')->paginate(10);
$data['category'] = $category;
$data['posts'] = $posts;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Blog/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\PostCategoriesModel;
use App\Models\CategoriesModel;
use App\Models\PostsModel;
use App\Models\User;
use App\Models\CommentsModel;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Home/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,7 @@
use App\Http\Controllers\Controller;
use App\Models\PostsModel;
use App\Models\User;
use App\Models\PostCategoriesModel;
use App\Models\CategoriesModel;
use App\Models\TagsModel;
use Auth;

Expand All @@ -25,7 25,7 @@ public function index()

$data = array();
$data['recent_posts'] = PostsModel::OrderBy('created_at', 'Desc')->where('active', '1')->limit(6)->get();
$data['post_categories'] = PostCategoriesModel::all();
$data['post_categories'] = CategoriesModel::latest()->limit(25)->get();
$data['tags'] = TagsModel::has('posts')->get();
$data['featured_posts'] = PostsModel::where('featured_post', '1')->get();
return view('home',$data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@

use Illuminate\Database\Eloquent\Model;

class PostCategoriesModel extends Model
class CategoriesModel extends Model
{
protected $table = "categories";

Expand All @@ -17,7 17,7 @@ class PostCategoriesModel extends Model
*/
public function posts()
{
return $this->belongsToMany('App\Models\PostsModel','post_category','category_id','post_id');
return $this->hasMany('App\Models\PostsModel','category_id');
}

}
4 changes: 2 additions & 2 deletions app/Models/PostsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 26,9 @@ class PostsModel extends Model
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories()
public function category()
{
return $this->belongsToMany('App\Models\PostCategoriesModel','post_category','post_id','category_id');
return $this->belongsTo('App\Models\CategoriesModel','category_id');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,7 @@ public function up()
$table->text('content');
$table->string('featured_image');
$table->boolean('active')->default(false);
$table->integer('category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
});
Expand Down
34 changes: 0 additions & 34 deletions database/migrations/2018_01_09_064324_post_category_table.php

This file was deleted.

37 changes: 0 additions & 37 deletions database/migrations/2018_01_10_075753_Drop_unique_slug_posts.php

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions resources/views/admin/posts/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 11,12 @@
</span>
@endif
</div>
<div class="form-group{{ $errors->has('categories') ? ' has-error' : '' }}">
{{Form::label('categories', 'Category *')}}
{{Form::select("categories[]",$categories,null, array("class"=>"form-control","id"=>"categories","multiple"=>"multiple"))}}
@if ($errors->has('categories'))
<div class="form-group{{ $errors->has('category') ? ' has-error' : '' }}">
{{Form::label('category_id', 'Category *')}}
{{Form::select("category_id",$categories,null, array("class"=>"form-control","id"=>"category_id"))}}
@if ($errors->has('category'))
<span class="help-block">
<strong>{{ $errors->first('categories') }}</strong>
<strong>{{ $errors->first('category') }}</strong>
</span>
@endif
</div>
Expand Down Expand Up @@ -111,7 111,7 @@ function readURL(input) {
$(document).ready(function() {
$("#categories").select2();
$("#category_id").select2();
$("#tags").select2({
minimumInputLength: 2,
Expand Down

0 comments on commit 0ba6aeb

Please sign in to comment.