Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用laravel中的联合创建查询搜索_Mysql_Sql_Laravel 5 - Fatal编程技术网

Mysql 如何使用laravel中的联合创建查询搜索

Mysql 如何使用laravel中的联合创建查询搜索,mysql,sql,laravel-5,Mysql,Sql,Laravel 5,我可以使用以下代码一次搜索一个表中的任何列 <?php namespace App; use App\Services\Markdowner; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class Post extends Model { protected $dates = ['published_at']; protected $fillable = [ 'title', 'subtitle',

我可以使用以下代码一次搜索一个表中的任何列

<?php

namespace App;

use App\Services\Markdowner;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Post extends Model
{
protected $dates = ['published_at'];

protected $fillable = [
    'title', 'subtitle', 'content_raw', 'page_image', 'meta_description',
    'layout', 'is_draft', 'published_at',
];


public function scopeSearch($query, $search)
    {

        return $query

            ->where('title', 'LIKE', "%{$search}%")
            ->orWhere( 'subtitle', 'LIKE', "%{$search}%")
            ->orWhere('content_raw', 'LIKE', "%{$search}%")
            ->orderBy('created_at', 'desc')
            ->join($first)

            ->paginate(15);
    }
}
编辑这里是my BlogController.php

    <$php
     namespace App\Http\Controllers;

    use App\Jobs\BlogIndexData;
    use App\Http\Requests;
    use App\Post;
    use App\Tag;
    use Illuminate\Http\Request;
    use App\Services\RssFeed;
    use App\Services\SiteMap;

    class BlogController extends Controller
    {
        public function index(Request $request)
        {

            $query = $request->get('q');
            $posts = $query
                ? Post::search($query)
                : Post::orderBy('published_at', 'desc')->paginate(15);


            $tag = $request->get('tag');
            $data = $this->dispatch(new BlogIndexData($tag));
            $layout = $tag ? Tag::layout($tag) : 'blog.layouts.index';



            return view($layout, $data)->withPosts($posts);
        }

        public function showPost($slug, Request $request)
        {
            $post = Post::with('tags')->whereSlug($slug)->firstOrFail();
            $tag = $request->get('tag');
            if ($tag) {
                $tag = Tag::whereTag($tag)->firstOrFail();
            }

            return view($post->layout, compact('post', 'tag', 'slug'));
        }

        public function rss(RssFeed $feed)
        {
            $rss = $feed->getRSS();

            return response($rss)
                ->header('Content-type', 'application/rss+xml');
        }

        public function siteMap(SiteMap $siteMap)
        {
            $map = $siteMap->getSiteMap();

            return response($map)
                ->header('Content-type', 'text/xml');
        }
    }
get('q');
$posts=$query
? Post::搜索($query)
:Post::orderBy('published_at','desc')->paginate(15);
$tag=$request->get('tag');
$data=$this->dispatch(新BlogIndexData($tag));
$layout=$tag?Tag::layout($Tag):'blog.layouts.index';
返回视图($layout,$data)->withPosts($posts);
}
公共功能showPost($slug,Request$Request)
{
$post=post::with('tags')->whereSlug($slug)->firstOrFail();
$tag=$request->get('tag');
如果($tag){
$tag=tag::whereTag($tag)->firstOrFail();
}
返回视图($post->layout,compact('post','tag','slug'));
}
公共功能rss(RssFeed$feed)
{
$rss=$feed->getRSS();
返回响应($rss)
->标题(“内容类型”、“应用程序/rss+xml”);
}
公共功能站点地图(站点地图$siteMap)
{
$map=$siteMap->getSiteMap();
返回响应($map)
->标题('Content-type','text/xml');
}
}
编辑我的BlogIndexData.php

    <?php
    namespace App\Http\Controllers;
    use App\Jobs\BlogIndexData;
    use App\Http\Requests;
    use App\Post;
    use App\Tag;
    use Illuminate\Http\Request;
    use App\Services\RssFeed;
    use App\Services\SiteMap;

    class BlogController extends Controller
    {
        public function index(Request $request)
        {

            $query = $request->get('q');
            $posts = $query
                ? Post::search($query)
                : Post::orderBy('published_at', 'desc')->paginate(15);


            $tag = $request->get('tag');
            $data = $this->dispatch(new BlogIndexData($tag));
            $layout = $tag ? Tag::layout($tag) : 'blog.layouts.index';



            return view($layout, $data)->withPosts($posts);
        }

        public function showPost($slug, Request $request)
        {
            $post = Post::with('tags')->whereSlug($slug)->firstOrFail();
            $tag = $request->get('tag');
            if ($tag) {
                $tag = Tag::whereTag($tag)->firstOrFail();
            }

            return view($post->layout, compact('post', 'tag', 'slug'));
        }

        public function rss(RssFeed $feed)
        {
            $rss = $feed->getRSS();

            return response($rss)
                ->header('Content-type', 'application/rss+xml');
        }

        public function siteMap(SiteMap $siteMap)
        {
            $map = $siteMap->getSiteMap();

            return response($map)
                ->header('Content-type', 'text/xml');
        }
    }

尝试让PDO实例直接执行查询:

    $PDO=DB::connection('mysql')->getPdo();
    $stmt=$PDO->prepare("
         SELECT title FROM posts
         WHERE title like :query
         UNION
         select subtitle from posts
         WHERE subtitle like :query
         union
         select title from tags
         WHERE title like :query
         union
         select subtitle from tags
         WHERE subtitle like :query
    ");
    $stmt->bindParam(':query', $query);


    $stmt->execute();

    $result = $stmt->fetchAll();
在我运行在Laravel4上的一个项目中,我有一个这样的查询,您可能需要修改参数。另外,在谷歌上搜索laravel原始查询


祝你好运

尝试让PDO实例直接执行查询:

    $PDO=DB::connection('mysql')->getPdo();
    $stmt=$PDO->prepare("
         SELECT title FROM posts
         WHERE title like :query
         UNION
         select subtitle from posts
         WHERE subtitle like :query
         union
         select title from tags
         WHERE title like :query
         union
         select subtitle from tags
         WHERE subtitle like :query
    ");
    $stmt->bindParam(':query', $query);


    $stmt->execute();

    $result = $stmt->fetchAll();
在我运行在Laravel4上的一个项目中,我有一个这样的查询,您可能需要修改参数。另外,在谷歌上搜索laravel原始查询


祝你好运

列数据类型是否相同?标题和副标题?@vkp是的。我在sequel pro中运行了这段sql代码,它返回了我想要的所有数据,这些数据与itAre标记和帖子有什么关系?如果是,您只是想检索匹配的文章,您可以尝试使用连接。是的,它们是,但我需要这样做,因为它的代码更干净。此外,我希望能够保持我的选项处于打开状态,以便以后可以添加表并查询它们,而无需“重新布线”我的代码列数据类型是否相同?标题和副标题?@vkp是的。我在sequel pro中运行了这段sql代码,它返回了我想要的所有数据,这些数据与itAre标记和帖子有什么关系?如果是,您只是想检索匹配的文章,您可以尝试使用连接。是的,它们是,但我需要这样做,因为它的代码更干净。此外,我希望能够保持我的选项处于打开状态,以便以后可以添加表并查询它们,而无需“重新布线”我的代码
    $PDO=DB::connection('mysql')->getPdo();
    $stmt=$PDO->prepare("
         SELECT title FROM posts
         WHERE title like :query
         UNION
         select subtitle from posts
         WHERE subtitle like :query
         union
         select title from tags
         WHERE title like :query
         union
         select subtitle from tags
         WHERE subtitle like :query
    ");
    $stmt->bindParam(':query', $query);


    $stmt->execute();

    $result = $stmt->fetchAll();