Skip to content

Commit

Permalink
slug added
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Oct 5, 2022
1 parent f6d933b commit fc9ab90
Show file tree
Hide file tree
Showing 57 changed files with 1,666 additions and 1,963 deletions.
191 changes: 23 additions & 168 deletions app/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ public function draft(Request $request)
*/
public function show(Request $request, $slug)
{

$titleArray = explode('-', $slug);
$id = end($titleArray);
$blog = Blog::find($id);
$blog = Blog::where("slug", $slug)->first();
if ($blog) {
if ($blog->status == "posted") {
// $shareBlog = ShareFacade::page(
Expand All @@ -136,11 +133,11 @@ public function show(Request $request, $slug)
// dd($blog->body());


$existView = BlogView::where([['ip_address', "=", $request->ip()], ["blog_id", "=", $id]])->count();
$existView = BlogView::where([['ip_address', "=", $request->ip()], ["blog_id", "=", $blog->id]])->count();
if ($existView < 1) {
$newView = new BlogView();
$newView->ip_address = $request->ip();
$newView->blog_id = $id;
$newView->blog_id = $blog->id;
$newView->save();
}
$related = Blog::where("status", "=", "posted")->with(['user', 'tags', 'bloglikes', 'blogviews'])->whereHas('tags', function ($query) use ($blog) {
Expand All @@ -155,132 +152,25 @@ public function show(Request $request, $slug)
}
return abort(404);;
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/

/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateBlogRequest $request
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function store(StoreBlogRequest $request)
{
// dd($request);
// {{ Str::slug($blog->title(), '-') }}-{{ $blog->id() }}
return $request;
}
public function post(Request $request)
public function edit($slug)
{
dd($request->all());
$blogId = $request->get('blog_id');
$blogTitle = $request->get('title');
$blogDescription = $request->get('description');
$tagNames = json_decode($request->get('tags'));
if ($blogId != NULL) {
$blog = Blog::find($blogId);
$blog->title = $blogTitle;
$blog->description = $blogDescription;
$blog->status = "posted";
$tagIds = [];
foreach ($tagNames as $tagName) {
$tag = Tag::firstOrCreate(['title' => $tagName]);
if ($tag) {
$tagIds[] = $tag->id;
}
};
$blog->save();
$blog->tags()->sync($tagIds);
} else {
$blog = new Blog();
$blog->title = $blogTitle;
$blog->description = $blogDescription;
$blog->status = "posted";
$blog->user_id = auth()->user()->id;
$tagIds = [];
foreach ($tagNames as $tagName) {

$tag = Tag::firstOrCreate(['title' => $tagName]);
if ($tag) {
$tagIds[] = $tag->id;
}
};
$blog->save();
$blog->tags()->sync($tagIds);

$blogId = $blog->id;
}
return redirect()->to("blogs/" . $blogId)->with([
"blog" => $blog,
"success" => 'blog created successfully.'
]);
}
public function edit(Request $request, $title)
{
$titleArray = explode('-', $title);
$id = end($titleArray);
$blog = Blog::find($id);
$blog = Blog::where("slug", $slug)->first();
$this->Authorize('view', $blog);
if ($blog) {
$tagTitles = [];
foreach ($blog->tags as $tag) {
$tagTitles[] = $tag->title;
}
return view("blogs.update")->with([
"blog" => $blog,
"tagTitles" => json_encode($tagTitles),
]);
return view("blogs.update")->with(["blog" => $blog]);
}
}
public function editStore(Request $request)
{
if (auth()->user()->id == $request->get('user_id')) {
$blogId = $request->get('blog_id');
$blogTitle = $request->get('title');
$blogDescription = $request->get('description');
$tagNames = json_decode($request->get('tags'));

$blog = Blog::find($blogId);
$blog->title = $blogTitle;
$blog->description = $blogDescription;
foreach ($tagNames as $tagName) {

$tag = Tag::firstOrCreate(['title' => $tagName]);
if ($tag) {
$tagIds[] = $tag->id;
$tagTitles[] = $tag->title;
}
};
$blog->tags()->sync($tagIds);
$blog->save();
$comments = Comment::where("blog_id", "=", $blogId)->paginate(5)->fragment('comments');

return redirect()->to("blogs/" . $blogId)->with([
"blog" => $blog,
"comments" => $comments,
"success" => 'blog updated successfully.'
]);
}
return view("error");
}
public function manage(Request $request, $title)
public function manage($slug)
{
$titleArray = explode('-', $title);
$id = end($titleArray);
$blog = Blog::find($id);
$blog = Blog::where("slug", $slug)->first();
$this->Authorize('update', $blog);
if ($blog) {
if (auth()->user()->id == $blog->user_id) {
return view("blogs.manage")->with([
"blog" => $blog,
]);
}
return view("blogs.manage")->with([
"blog" => $blog,
]);
}
return view("error");
return abort(404);
}
public function seo(Request $request)
{
Expand All @@ -299,20 +189,16 @@ public function seo(Request $request)
}
return view("error");
}
public function stats(Request $request, $title)
public function stats($slug)
{
$titleArray = explode('-', $title);
$id = end($titleArray);
$blog = Blog::find($id);
$blog = Blog::where("slug", $slug)->first();
$this->Authorize('update', $blog);
if ($blog) {

if (auth()->user()->id == $blog->user_id) {
return view("blogs.stats")->with([
"blog" => $blog,
]);
}
return view("blogs.stats")->with([
"blog" => $blog,
]);
}
return view("error");
return abort(404);
}
public function manageStore(Request $request)
{
Expand All @@ -332,42 +218,11 @@ public function manageStore(Request $request)
}
return view("404");
}
public function detailCard(Request $request)
{
$blogId = $request->get('blogId');

$blog = Blog::query()->where('id', '=', $blogId)->first();

$html = view("blogs.popover")
->with([
"blog" => $blog,
])->render();
return json_encode($html);
}
public function tagSearch(Request $request, $title)
public function tagSearch(Request $request, $slug)
{
$searchTag = Tag::where("title", "=", $title)->first();
// if ($request->tab == 'likes') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->withCount(['bloglikes' => function ($q) {
// $q->where('status', '=', 1);
// }])->orderByDesc('bloglikes_count')->paginate(10);
// } else if ($request->tab == 'newest') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->orderByDesc('created_at')->paginate(10);
// } else if ($request->tab == 'views') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->withCount('blogviews')->orderByDesc('blogviews_count')->paginate(10);
// } else {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->orderByDesc('created_at')->paginate(10);
// }
$searchTag = Tag::where("title", "=", $slug)->first();
return view("blogs.tagged")->with([
"searchTag"=>$searchTag
"searchTag" => $searchTag
]);
}

Expand Down
43 changes: 29 additions & 14 deletions app/Http/Livewire/Blogs/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@

use App\Events\BlogWasCreated;
use App\Models\Blog;
use App\Models\Tag;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Str;

use Cviebrock\EloquentSluggable\Services\SlugService;
class Create extends Component
{
use WithFileUploads;

public $title;

public $body = "# hello peoples";

public $body;
public $message;

public $photo;
public $tags = [];

public $search;
public $searchTags=[];
protected $rules = [
// 'cover_image' => ['required', 'mimes:png,jpg,svg,gif', 'max:2048'],
'title' => ['required', 'max:200', 'min:20'],
'body' => ['required', 'min:20'],
'tags' => ['required', 'array', 'min:1', 'max:5']
];

public function render()
{
if ($this->search != NULL) {
$this->searchTags = Tag::query()->where('title', 'LIKE', '%'.$this->search.'%')->take(5)->get();
}else{
$this->searchTags=[];
}
return view('livewire.blogs.create');
}

public function updatedPhoto()

{
$this->validate([
'photo' => 'image|max:1024', // 1MB Max
Expand All @@ -47,8 +48,16 @@ public function submit()
'body' => $this->body,
'user_id' => auth()->id()
]);
$tagIds = [];
foreach ($this->tags as $tag) {
$tag = Tag::firstOrCreate(['title' => $tag]);
if ($tag) {
$tagIds[] = $tag->id;
}
};
$blog->tags()->sync($tagIds);
BlogWasCreated::dispatch($blog);
return redirect()->to('/blogs/' . Str::slug($this->title, '-') . '-' . $blog->id);
return redirect()->to('/blogs/'.$blog->slug);
}
public function draft()
{
Expand All @@ -57,11 +66,17 @@ public function draft()
'title' => $this->title,
'body' => $this->body,
'status' => "drafted",
'user_id' => auth()->id(),

'user_id' => auth()->id()
]);
$tagIds = [];
foreach ($this->tags as $tag) {
$tag = Tag::firstOrCreate(['title' => $tag]);
if ($tag) {
$tagIds[] = $tag->id;
}
};
$blog->tags()->sync($tagIds);
$this->emit('changed');
session()->flash('message', 'Social info updated successfully');
return redirect()->to('/settings?tab=drafts');
}
}
Loading

0 comments on commit fc9ab90

Please sign in to comment.