My tags won't work unless I remove search capability and vice versa. I've followed a tutorial which I've tried to modify to suit my blogs needs. The app already had the ability to search tags. The code looks like the following
public function index(Request $request)
{
$tag = $request->get('tag');
$data = $this->dispatch(new TagIndexData($tag));
$layout = $tag ? Tag::layout($tag) : 'tags.layouts.index';
return view($layout, $data);
}
I've modified it, by adding a search capability and the code looks like the following
public function index(Request $request)
{
$query = $request->get('q');
$posts = Post::where('title', 'LIKE', "%{$query}%")
->orwhere('subtitle', 'LIKE', "%{$query}%")->get();
$videos = Video::where('title', 'LIKE', "%{$query}%")
->orwhere('subtitle', 'LIKE', "%{$query}%")->get();
$tag = $request->get('tag');
$data = $this->dispatch(new TagIndexData($tag));
$layout = $tag ? Tag::layout($tag) : 'tags.layouts.index';
return view($layout, $data)->withPosts($posts)->withVideos($videos);
}
When I modify it, the search works but the ability to use the tags no longer works. When I click on a tag, it returns everything. Also here is my TagIndexData
<?php
namespace App\Jobs;
use App\Post;
use App\Video;
use App\Tag;
use Carbon\Carbon;
use Illuminate\Contracts\Bus\SelfHandling;
class TagIndexData extends Job implements SelfHandling
{
protected $tag;
/**
* Constructor
*
* @param string|null $tag
*/
public function __construct($tag)
{
$this->tag = $tag;
}
/**
* Execute the command.
*
* @return array
*/
public function handle()
{
if ($this->tag) {
return $this->tagIndexData($this->tag);
}
return $this->normalIndexData();
}
/**
* Return data for normal index page
*
* @return array
*/
protected function normalIndexData()
{
$posts = Post::with('tags')
->where('published_at', '<=', Carbon::now())
->where('is_draft', 0)
->orderBy('published_at', 'desc')
->simplePaginate(config('blog.posts_per_page'));
$videos = Video::with('tags')
->where('published_at', '<=', Carbon::now())
->where('is_draft', 0)
->orderBy('published_at', 'desc')
->simplePaginate(config('blog.posts_per_page'));
return [
'title' => config('tags.title'),
'subtitle' => config('tags.subtitle'),
'posts' => $posts,
'videos' => $videos,
'page_image' => config('home.page_image'),
'meta_description' => config('home.description'),
'reverse_direction' => false,
'tag' => null,
];
}
/**
* Return data for a tag index page
*
* @param string $tag
* @return array
*/
protected function tagIndexData($tag)
{
$tag = Tag::where('tag', $tag)->firstOrFail();
$reverse_direction = (bool)$tag->reverse_direction;
$posts = Post::where('published_at', '<=', Carbon::now())
->whereHas('tags', function ($q) use ($tag) {
$q->where('tag', '=', $tag->tag);
})
->where('is_draft', 0)
->orderBy('published_at', $reverse_direction ? 'asc' : 'desc')
->simplePaginate(config('blog.posts_per_page'));
$posts->addQuery('tag', $tag->tag);
$videos = Video::where('published_at', '<=', Carbon::now())
->whereHas('tags', function ($q) use ($tag) {
$q->where('tag', '=', $tag->tag);
})
->where('is_draft', 0)
->orderBy('published_at', $reverse_direction ? 'asc' : 'desc')
->simplePaginate(config('blog.posts_per_page'));
$videos->addQuery('tag', $tag->tag);
$page_image = $tag->page_image ?: config('tags.page_image');
return [
'title' => $tag->title,
'subtitle' => $tag->subtitle,
'posts' => $posts,
'videos' => $videos,
'page_image' => $page_image,
'tag' => $tag,
'reverse_direction' => $reverse_direction,
'meta_description' => $tag->meta_description ?:
config('blog.description'),
];
}
}