Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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
Php 我正在尝试查询我的laravel数据库中的两个表_Php_Mysql_Laravel - Fatal编程技术网

Php 我正在尝试查询我的laravel数据库中的两个表

Php 我正在尝试查询我的laravel数据库中的两个表,php,mysql,laravel,Php,Mysql,Laravel,我试图查询数据库中的两个表,但它返回此错误。 我正在尝试通过多个表实现搜索。该项目是一个在线商店,有3个独特的表、产品、类别和品牌。我只能在Products表中搜索,但似乎无法从我的blade文件中获得相同的搜索字段来搜索类别或品牌,并返回相关产品的结果 QLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous (SQL: select * from `cat

我试图查询数据库中的两个表,但它返回此错误。 我正在尝试通过多个表实现搜索。该项目是一个在线商店,有3个独特的表、产品、类别和品牌。我只能在Products表中搜索,但似乎无法从我的blade文件中获得相同的搜索字段来搜索类别或品牌,并返回相关产品的结果

QLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous (SQL: select * from `categories` inner join `products` on `products`.`category_id` = `categories`.`id` where `name` LIKE %Television% and `status` = 1)
我的搜索功能

public function searchProducts(Request $request) {
        $product = $request->input('product');

        $categories = Category::with('categories')->where(['parent_id' => 0])->get();

        $productsAll = Category::query()->join('products', 'products.category_id', '=', 'categories.id')
                                    ->where('name', 'LIKE', "%{$product}%")
                                    ->where('status', 1)->get();


        $breadcrumb = "<a href='/'>Home</a> / ".$product;

        return view('pages.results')->with(compact('categories','productsAll','product','breadcrumb'));

    }
我的产品模型

class Product extends Model implements Searchable
{

    public function category() {
        return $this->hasMany('App\Category', 'id') ;
    }

    public function attributes(){
        return $this->hasMany('App\Product','id');
    }
}

您在多个表中有状态列。 改变这个

->where('status', 1)->get();
对此

->where('products.status', 1)->get();

我可以通过如下修改我的搜索函数来解决这个问题

public function searchProducts(Request $request) {
        $product = $request->input('product');

        $categories = Category::with('categories')->where(['parent_id' => 0])->get();

        $productsAll = Category::query()->join('products', 'products.category_id', '=', 'categories.id')
                                    ->where('categories.name', 'LIKE', "%{$product}%")
                                    ->orWhere('products.product_name', 'LIKE', "%{$product}%")
                                    ->where('products.status', 1)->get();


        $breadcrumb = "<a href='/'>Home</a> / ".$product;

        return view('pages.results')->with(compact('categories','productsAll','product','breadcrumb'));

    }
公共功能搜索产品(请求$Request){
$product=$request->input('product');
$categories=Category::with('categories')->where(['parent_id'=>0])->get();
$productsAll=Category::query()->join('products'、'products.Category_id'、'='、'CATEGORES.id')
->其中('categories.name','LIKE',“%{$product}%”)
->orWhere('products.product_name','LIKE',“%{$product}%”)
->其中('products.status',1)->get();
$breadcrumb=“/”$product;
返回视图('pages.results')->带有(compact('categories','productsAll','product','breadcrumb');
}
public function searchProducts(Request $request) {
        $product = $request->input('product');

        $categories = Category::with('categories')->where(['parent_id' => 0])->get();

        $productsAll = Category::query()->join('products', 'products.category_id', '=', 'categories.id')
                                    ->where('categories.name', 'LIKE', "%{$product}%")
                                    ->orWhere('products.product_name', 'LIKE', "%{$product}%")
                                    ->where('products.status', 1)->get();


        $breadcrumb = "<a href='/'>Home</a> / ".$product;

        return view('pages.results')->with(compact('categories','productsAll','product','breadcrumb'));

    }