【问题标题】:Relationships, get products where relationship table belongs to the user关系,获取关系表属于用户的产品
【发布时间】:2017-10-07 09:48:21
【问题描述】:

所以我有 2 个表产品和购买,我试图获得属于产品所有者的购买。任何人都可以购买,但只有用户拥有产品

我的桌子是这样的:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->string('title');
    $table->text('description');
    $table->string('category');
    $table->string('auction_type');
    $table->string('allow_offers');
    $table->string('condition');
    $table->integer('quantity');
    $table->integer('auctionlength');
    $table->decimal('buyitnow_price');
    $table->decimal('auction_price');
    $table->string('shipping');
    $table->decimal('shipping_cost');
    $table->integer('shipping_time');
    $table->string('international_shipping');
    $table->string('location');
    $table->string('allow_returns');
    $table->date('enddate');
    $table->timestamps();
    $table->softDeletes();
});

采购表

Schema::create('purchases', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->integer('product_id');
    $table->decimal('amount');
    $table->string('name');
    $table->string('address');
    $table->string('city');
    $table->string('state');
    $table->string('country');
    $table->string('postal');
    $table->string('email');
    $table->string('status');
    $table->timestamps();
    $table->softDeletes();
});

我尝试了以下方法,但这似乎仍然会出现行:

dd(Product::where('user_id', Auth::user()->id)->with('purchases')->get());

采购模式

public function product()
{
    return $this->belongsTo('App\Models\Market\Product');
}

产品型号

public function purchases()
{
    return $this->hasMany('App\Models\Market\Purchases');    
}

我的两个型号是PurchasesProducts,没有原始查询,我如何购买属于user_id的产品?

【问题讨论】:

  • 我真的建议你在问任何问题之前使用文档,你必须了解 Laravel 是如何工作的,就像 Eloquent 一样,然后做你的模型/DB,然后问我们。对于那些写得不好,甚至对他/她正在使用的系统一无所知的人,真的很难帮助......

标签: php laravel laravel-5 eloquent


【解决方案1】:

您的 User 模型必须具有 hasMany 关系,至少与 Purchases 有关系(因为在这里您试图得到它),最好是两个模型,像这样

public function products()
{
    return $this->hasMany(Products::class);
}

public function purchases()
{
    return $this->hasMany(Purchases::class);
}

要获得购买,您可以这样做

$user->purchases;

假设$user 已经有一个User 模型(带有数据),当您将关系作为属性(而不是方法)调用时,您将获得Collection(因为是hasMany 关系)。

这一切都是凭记忆做的。

这样您就可以访问,比如说,第一笔购买的退货

$purchases = $user->purchases;

$purchases->first()->name;

编辑:所以您正试图让所有购买都在产品上进行。 您必须在 Products 模型上定义关系。

所以你需要访问Products模型,做我做的关系得到Purchases

public function purchases()
{
    return $this->hasMany(Purchases::class);
}

然后,您可以访问在该产品上完成的所有购买。

【讨论】:

  • 那只会获取用户的购买和产品。我正在尝试获取购买我产品的其他用户的购买
  • @randommman 我已经更新了我的答案。了解关系的工作原理非常重要,因为它是 Eloquent 最棒的地方之一。
  • 我拥有的每一件物品都有可能获得每次购买吗?
  • 如果我理解你的问题,是的。有了这个,您将获得product_id 列等于您正在使用的当前Product id 模型的所有购买。
  • 只获取特定项目的购买,而不是属于我拥有的产品的所有购买
【解决方案2】:

试试这个朋友:)

采购型号

public function product()
{
    return $this->belongsTo(Product:class ,'product_id', 'id');
}

产品型号

public function purchases()
{
    return $this->hasMany(Purchases::class,'id', 'product_id');    
}

【讨论】:

    【解决方案3】:

    一些建议描述了一种解决方案,在该解决方案中,我们将产品与他们的每次购买一起获取。这可以工作,但我们需要将每个产品的购买提取到一个集合中:

    $purchases = Product::with('purchases')
        ->where('user_id', $ownerId)
        ->get()
        ->pluck('purchases')
        ->flatten();
    

    这是一种更高效的方法——我们可以直接获取特定用户拥有的产品的购买:

    $purchases = Purchase::with('product') 
        ->whereHas('product', function ($query) use ($ownerId) { 
            $query->where('user_id', $ownerId);
        })
        ->get();
    

    如果我们不需要为每次购买使用存储在产品上的任何数据,我们可以从上述查询中删除 with('product')

    【讨论】:

    • 我不明白。我已经回答了这个问题,而且更好的是,有了 Eloquent 关系,无需重新发明轮子。
    • 你显然永远不会。看看他的做法,然后比较你的……这是正确的选择属于我所有产品的所有购买..
    • 嗨@matiaslauriti,我希望我能帮助澄清。此答案中的两种解决方案确实使用雄辩的关系...如果没有它们,我上面的代码将无法工作。该问题要求一种方法来获取指定标准的Purchase 记录的一维集,因此我编写了使用问题中的关系来生成请求的结果集的示例。您的答案包含解决方案的部分,值得+1...如果user_id 在两张桌子上相同,它会起作用,但purchases 上的user_id 是购买了产品,而不是产品所有者,所以$user->purchases 不起作用。
    • @matiaslauriti 这是一个很容易错过的细节,我几乎犯了同样的错误。让我知道这听起来是否合理。如果这有意义,请考虑更改您的投票。
    猜你喜欢
    • 2018-12-23
    • 2013-05-22
    • 2013-12-07
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2014-08-13
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多