dongyu1983 2015-11-17 19:18
浏览 40
已采纳

为什么不将全文搜索与Laravel 5.1中的标签结合使用?

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'),
      ];
   }
}
  • 写回答

1条回答 默认 最新

  • douhui7136 2015-11-18 18:07
    关注

    I figured it out. I had to use an if statement like so

    public function index(Request $request)
    {
        if($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);
        } else {
            $tag = $request->get('tag');
            $data = $this->dispatch(new TagIndexData($tag));
            $layout = $tag ? Tag::layout($tag) : 'blog.layouts.index';
    
            return view($layout, $data);
        }
    

    Then it worked. It still looks messy. If someone has a suggestion to make this neater I'm open to suggestions thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line