Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/Admin/PostCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public function update(Request $request, PostCategoryRepository $postCategoryRep
'meta_title' => 'nullable|string',
'meta_description' => 'nullable|string'
]);

$postCategory = PostCategory::findOrFail($id);
$postCategory->fill($request->all());
$postCategory->save();
$postCategoryRepository->uploadImage($postCategory, $request, 'image');

return redirect()->route('admin.postCategories.edit', $id)->with(['flash_type' => 'success', 'flash_message' => 'Post category updated successfully', 'flash_description' => $postCategory->name]);
}
}
10 changes: 8 additions & 2 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ public function store(Request $request, PostRepository $postRepository)
'meta_description' => 'nullable|string',
'image' => 'nullable|image',
'category_ids' => 'required|array',
'category_ids.*' => 'exists:post_categories,id'
'category_ids.*' => 'exists:post_categories,id',
'tag_ids' => 'sometimes|required|array',
'tag_ids.*' => 'required|integer|exists:tags,id'
]);

$request->merge(['user_id' => auth()->user()->id]);

$post = Post::create($request->all());
$postRepository->uploadImage($post, $request, 'image');
$post->categories()->sync($request->get('category_ids'));
$post->tags()->sync($request->get('tag_ids'));

return redirect()->route('admin.posts.edit', $post->id)->with(['flash_type' => 'success', 'flash_message' => 'Post created successfully', 'flash_description' => $post->title]);
}
Expand All @@ -88,13 +91,16 @@ public function update(Request $request, PostRepository $postRepository, $id)
'meta_description' => 'nullable|string',
'image' => 'nullable|image',
'category_ids' => 'required|array',
'category_ids.*' => 'exists:post_categories,id'
'category_ids.*' => 'exists:post_categories,id',
'tag_ids' => 'sometimes|required|array',
'tag_ids.*' => 'required|integer|exists:tags,id'
]);
$post = Post::findOrFail($id);
$post->fill($request->all());
$post->save();
$postRepository->uploadImage($post, $request, 'image');
$post->categories()->sync($request->get('category_ids'));
$post->tags()->sync($request->get('tag_ids'));
return redirect()->route('admin.posts.edit', $id)->with(['flash_type' => 'success', 'flash_message' => 'Post updated successfully', 'flash_description' => $post->title]);
}
}
43 changes: 43 additions & 0 deletions app/Http/Controllers/Admin/TagController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Resources\TagResource;
use App\Models\Tag;
use Illuminate\Http\Request;
use Inertia\Inertia;

class TagController extends Controller
{

public function index(Request $request)
{
$tags = Tag::latest()->paginate($request->get('limit', config('app.pagination_limit')))->withQueryString();
return Inertia::render('Admin/Tags/Tags', ['tags' => TagResource::collection($tags)]);
}

public function store(Request $request)
{
$request->validate([
'name' => 'required|string',
]);

$tag = Tag::create($request->all());

return redirect()->route('admin.tags.index', $tag->id)->with(['flash_type' => 'success', 'flash_message' => 'Tag created successfully', 'flash_description' => $tag->name]);
}

public function update(Request $request, $id)
{
$request->validate([
'name' => 'required|string',
]);

$tag = Tag::findOrFail($id);
$tag->fill($request->all());
$tag->save();

return redirect()->route('admin.tags.index', $tag->id)->with(['flash_type' => 'success', 'flash_message' => 'Tag updated successfully', 'flash_description' => $tag->name]);
}
}
8 changes: 7 additions & 1 deletion app/Http/Controllers/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
namespace App\Http\Controllers;

use App\Models\Page;
use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Inertia;

class HomepageController extends Controller
{
public function __invoke()
{
$post = Post::latest()->first();

//$post->tags()->sync([1]);

$page = Page::slug('home')->firstOrNew();
activity()->withoutLogs(function () use($page) {

activity()->withoutLogs(function () use ($page) {
$page->increment('views');
});

Expand Down
20 changes: 20 additions & 0 deletions app/Http/Resources/TagResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TagResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$data = parent::toArray($request);
return $data;
}
}
3 changes: 2 additions & 1 deletion app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use App\Traits\ActivityLoggable;
use App\Traits\HasSlug;
use App\Traits\TaggableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
use HasFactory, ActivityLoggable, SoftDeletes, HasSlug;
use HasFactory, ActivityLoggable, SoftDeletes, HasSlug, TaggableTrait;

protected $guarded = ['id'];

Expand Down
15 changes: 15 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models;

use App\Traits\ActivityLoggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tag extends Model
{
use HasFactory, SoftDeletes, ActivityLoggable;

protected $guarded = ['id'];
}
20 changes: 20 additions & 0 deletions app/Models/Taggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Taggable extends Model
{
use HasFactory, SoftDeletes;

protected $guarded = ['id'];

public function taggable(): MorphTo
{
return $this->morphTo();
}
}
16 changes: 16 additions & 0 deletions app/Repositories/TagRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Repositories;

use App\Models\Tag;
use Illuminate\Support\Facades\Log;

class TagRepository extends BaseRepository
{
public $model;

function __construct(Tag $model)
{
$this->model = $model;
}
}
15 changes: 15 additions & 0 deletions app/Traits/TaggableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Traits;

use App\Models\Tag;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

trait TaggableTrait
{
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
29 changes: 29 additions & 0 deletions database/migrations/2024_10_13_100430_create_tags_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tags');
}
};
30 changes: 30 additions & 0 deletions database/migrations/2024_10_13_100910_create_taggables_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('taggables', function (Blueprint $table) {
$table->id();
$table->morphs('taggable');
$table->foreignId('tag_id')->constrained('tags');
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('taggables');
}
};
File renamed without changes.
5 changes: 1 addition & 4 deletions resources/js/Pages/Admin/PostCategories/PostCategories.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Button } from "@/shadcn/ui/button";
import { ScrollArea } from "@/shadcn/ui/scroll-area";
import { Head, Link } from "@inertiajs/react";
import {
Pencil,
PlusCircle,
} from "lucide-react";
import { Pencil, PlusCircle } from "lucide-react";
import { Checkbox } from "@/shadcn/ui/checkbox";
import RTable from "@/Components/RTable";
import AuthenticatedLayout from "@/Layouts/admin/AuthenticatedLayout";
Expand Down
99 changes: 99 additions & 0 deletions resources/js/Pages/Admin/Tags/Tags.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Button } from "@/shadcn/ui/button";
import { ScrollArea } from "@/shadcn/ui/scroll-area";
import { Head, Link } from "@inertiajs/react";
import { Pencil, PlusCircle } from "lucide-react";
import { Checkbox } from "@/shadcn/ui/checkbox";
import RTable from "@/Components/RTable";
import AuthenticatedLayout from "@/Layouts/admin/AuthenticatedLayout";
import PageHeading from "@/Components/PageHeading";
import Can from "@/Components/Can";

export const columns = [
{
id: "select",
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) =>
table.toggleAllPageRowsSelected(!!value)
}
aria-label="Select all"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label="Select row"
/>
),
enableSorting: false,
enableHiding: false,
},
{
accessorKey: "name",
header: "Name",
},
{
accessorKey: "created_at_string",
header: "Created At",
},
{
accessorKey: "updated_at_string",
header: "Updated At",
},
{
id: "actions",
header: () => <div className="text-right">Actions</div>,
enableHiding: false,
cell: ({ row }) => {
return (
<div className="flex justify-end items-center gap-2">
<Can permit="edit Tags">
<Button asChild variant="outline" size="icon">
<Pencil className="h-4 w-4" />
</Button>
</Can>
</div>
);
},
},
];

export default function tags({ tags }) {
return (
<AuthenticatedLayout>
<Head>
<title>Tags</title>
</Head>
<ScrollArea className="h-full">
<div className="flex-1 space-y-4 p-4 md:p-8 pt-6">
<PageHeading>
<PageHeading.Title>
Tags ({tags.meta.total})
</PageHeading.Title>

<PageHeading.Actions>
<Can permit="create tags">
<Button asChild>Create New</Button>
</Can>
</PageHeading.Actions>
</PageHeading>
<div className="grid gap-4 grid-cols-1">
<RTable
data={tags.data}
columns={columns}
searchColumns={["name"]}
// exportable
paginationLinks={tags.links}
meta={tags.meta}
/>
</div>
</div>
</ScrollArea>
</AuthenticatedLayout>
);
}
Loading