【问题标题】:mb_strpos() expects parameter 1 to be string, object given when querying 2 tables in Laravelmb_strpos() 期望参数 1 是字符串,在 Laravel 中查询 2 个表时给出的对象
【发布时间】:2020-01-25 18:58:22
【问题描述】:

好的,不知道能不能这样,但是我需要把2个表的2个Where子句的结果合并成一个变量。

到目前为止,我正在查询一个表:

$allCompanies = Products::where('category_id', $id->id)->groupBy('company_id')->get();

这是另一个:

$companies = Company::where('visible', 0)->get();

但是有没有办法让它们进入同一个查询字符串?像这样我可以得到 ID 与一个表中的 ID 列匹配的位置,以及另一个表中可见为 0 的位置?

我试过了:

$allCompanies = Products::with('company', function($q){
            $q->where('visible', 0);
            })->where('category_id', $id->id)
            ->groupBy('company_id')->get();

但出现此错误:

mb_strpos() expects parameter 1 to be string, object given

公司模式:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    public function products()
    {
        return $this->hasMany('App\Products');
    }
}

产品型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
    protected $fillable = [
        'name',
        'description',
        'image',
        'size',
        'price',
        'stock',
        'company_id',
        'category_id'
    ];
    public function category()
    {
        return $this->belongsTo('App\Categories', 'category_id');
    }
    public function company()
    {
        return $this->belongsTo('App\Company', 'company_id');
    }
}

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    当在with 中使用回调时,你必须使用数组。也就是说,您的查询应该是:

    $allCompanies = Products::with(['company', function($q){
                $q->where('visible', 0);
                }])->where('category_id', $id->id)
                ->groupBy('company_id')->get();
    

    参考:https://laravel.com/docs/8.x/eloquent-relationships#nested-eager-loading-morphto-relationships

    【讨论】:

      【解决方案2】:

      取决于你想要什么:

      如果您想要所有知名公司的产品:

      $products = Products::whereHas('company', function($q) {
         $q->where('visible',0);
      })->get();
      

      如果您想要所有公司的产品(我会建议):

      $Companies = Company::with('products')->where('visible',0)->get();
      

      【讨论】:

      • 遗憾的是,这没有带来任何结果,而应该有的时候
      • 如果你把-&gt;get()改成-&gt;toSql()就可以看到运行的是什么SQL,这样调试起来会更方便。
      • 我收到"select * from `products` where exists (select * from `companies` where `products`.`company_id` = `companies`.`id` and `visible` = ?)"
      • 我建议直接在数据库上运行该查询并调整该查询以获得您想要的内容并查看发生了什么。子查询 (select * from companies where visible = 0) 是否返回正确的公司?他们的产品是否附属于公司?
      • 嗯,好的,所以它运行了,我可以看到不同之处,因为我可以将“可见”查询更改为 1 或 0,它显示不同的回报,但由于某种原因,我看到每家公司都重复了几十个次。
      【解决方案3】:

      实现这一点的最 Laravel 方法是通过 RelationshipswhereHas。如果您定义了关系,则可以执行以下操作:

      $products = Products::where('category_id', $id->id)
                              ->whereHas('company', function($q) {
                                $q->where('visible', 0);
                              });
      

      这将查询具有特定category_id 的所有产品,这些产品也与具有值为0 的列visible 的公司有关系

      【讨论】:

        猜你喜欢
        • 2020-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 2017-08-30
        • 2018-07-30
        • 2018-07-04
        • 2019-05-10
        • 1970-01-01
        相关资源
        最近更新 更多