【问题标题】:Laravel search with join methodLaravel 使用 join 方法进行搜索
【发布时间】:2017-10-06 08:17:04
【问题描述】:

我尝试同时在我的应用中搜索帖子标题和帖子标签,因此我在搜索功能中使用了 join 方法。

这是我的表格:

<div class="search">
      <form class="form-inline" action="/search" method="GET" role="search">
        {{ csrf_field() }}
        <i class="fa fa-search"></i>
        <div class="field-toggle">
          <input type="text" name="search" class="search-form" autocomplete="off" placeholder="Search...">
        </div>
        <button type="submit" name="button">search</button>
      </form>
    </div>

这是我的功能:

public function search() {

  $search = request('search');

  $foods = Food::join('food_ingredient', 'food_ingredient.food_id','=', 'food.id')
         ->join('ingredients','ingredient.id','=','food_ingredient.ingredient.id')
         ->where('food.title', 'LIKE', '%' . $search . '%')
         ->orWhere('ingredient.title', 'LIKE', '%' . $search . '%') //The fix
         ->orderBy('food.created_at', 'desc')
         ->groupBy('food.id')
         ->with('ingredients')
         ->paginate(8);

  return view('front.search', compact('foods'));
}

有了这个我得到这个错误:

SQLSTATE[42S22]:未找到列:1054 中的未知列“food.title” 'where 子句' (SQL: select count(*) as aggregate from foods inner 加入food_ingredientfood_ingredient.food_id = food.id 内部连接ingredientsingredient.id = food_ingredient.ingredient.id 其中food.title LIKE %papper% 或ingredient.title LIKE %papper% group by food.id)

我的数据库是这样的:

食物 ->商店帖子

成分 ->储存成分

Food_ingredient ->存储帖子标签

我该如何解决?

PS:我的帖子名称是food,我的标签名称是ingredient(只是命名不同)。

谢谢。

【问题讨论】:

    标签: php mysql laravel join


    【解决方案1】:

    使用连接时,您还需要在where 子句中指定表名,因为在joins 之后您将有多个列,因此在询问title 时需要指定表名:

      $foods = Food::join('food_ingredient', 'food_ingredient.food_id','=', 'food.id')
             ->join('ingredients','ingredient.id','=','food_ingredient.ingredient.id')
             ->where('ingredient.title', 'LIKE', '%' . $search . '%') ...
    

    假设您要搜索成分标题

    【讨论】:

    • 您应该更改 where 子句以满足您的需要,如前所述,我假设您想通过 ingredtient.title... 可能是 ingredients.name 进行搜索?这取决于您想要实现的目标
    • ingredtient.titlefood.title 我改变了你提到的部分也是我的错误。 (仍然出现错误)
    • 您在开始时基于 2 个标题进行了搜索(在原始问题中也是如此),您需要指定表格......就是这样!我猜不出你需要什么或者你想实现什么逻辑,因为我不知道你的表结构,也不知道你想使用 Eloquent 生成什么 SQL
    • 我在问题中提到了所有内容,请阅读,This is how is my database: Foods -&gt;store posts Ingredients -&gt;Store ingredients Food_ingredient -&gt;store posts tags
    【解决方案2】:

    您必须用逗号将通配符括起来,以便搜索它的参数

    public function searchOrders($param)
    {
        $id = "id";
        $search='%'.$param.'%';
    
        $orders = DB::select('SELECT o.*,s.name,b.name FROM orders o JOIN users b on o.buyer_id= b.id JOIN users s on o.seller_id=s.id where o.id LIKE  ?', [$search]);
    
        $data = array(
            "orders" => $orders
        );
        return View('admin.orders.service_search_orders', $data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多