Laravel获取关系返回错误的数据

旅游旅行

我有四个表,sellerproducts,buyersdownloads,的逻辑是:卖家可以products和买家可以购买products并将其存储在downloads表中。因此,例如:

卖方:

---------------------
|id | name | phone  |
---------------------
| 1 | mike | 989898 |
---------------------
| 2 | joe  | 989898 |
---------------------

产品:

---------------------
|id | name | user_id|
---------------------
| 1 | benz |    1   |
---------------------
| 2 | bmw  |    1   |
---------------------

买家:

----------------------
|id | name   | phone  |
----------------------
| 1 | carlos | 989898 |
----------------------
| 2 | nina   | 989898 |
----------------------

资料下载:

-----------------------------
|id | product_id  | buyer_id |
-----------------------------
| 1 |      1      |     2    |
-----------------------------
| 2 |      2      |     2    |
-----------------------------

因此,考虑到买家用id2购买了产品,我想获得产品(id 2)和卖方数据(id 1),另一方面,nina购买了benzbmw,这些产品属于mike,所以我想获得产品数据和卖方数据在BuyerController中。

class Buyer extends Authenticatable
{
...
    public function downloads(){
        return $this->hasMany('App\Download','buyer_id');
    }

    public function products(){
        return $this->hasManyThrough('App\Product','App\Seller', 'id', 'user_id');
    }
}

在控制器中:

public function files()
{
    $buyer = Buyer::find($id);
    $files = Buyer::with('downloads', 'products')->get();

    return $files;
}

它可以正常工作,但没有错误,但是返回错误的数据,例如,买方没有购买的产品,也没有返回卖方的数据,只是返回"laravel_through_key": 1第一个功能工作正常,但第二个功能hasManyThrough无法正常工作。

我是Laravel的新手,特别是关系。我做错了什么?

y

根据您的解释,Buyer具有直接关系downloads和间接关系products,但后者是通过downloads,不是sellers因此,应为:

class Buyer extends Authenticatable
{
  //...
  public function downloads()
  {
    return $this->hasMany(Download::class);
  }

  public function products()
  {
    return $this->hasManyThrough(Product::class, Download::class,
      'buyer_id', 'id', 'id', 'product_id');
  }
}

不要忘记为其他模型编写关系:

class Product extends Model
{
    function seller()
    {
      return $this->belongsTo(Seller::class, 'user_id');
    }
    function downloads()
    {
      return $this->hasMany(Download::class);
    }
}
class Download extends Model
{
    function product()
    {
      return $this->belongsTo(Product::class);
    }

    function buyer()
    {
      return $this->belongsTo(Buyer::class);
    }
}

现在您可以使用其功能了:

1.获取买家产品:

>>> App\Buyer::find(2)->products
=> Illuminate\Database\Eloquent\Collection {#3018
     all: [
       App\Product {#3022
         id: 1,
         name: "benz",
         user_id: 1,
         laravel_through_key: 2,
       },
       App\Product {#3019
         id: 2,
         name: "bmw",
         user_id: 1,
         laravel_through_key: 2,
       },
     ],
   }

2.与卖家信息相同

>>> App\Buyer::find(2)->products()->with('seller')->get()
=> Illuminate\Database\Eloquent\Collection {#3026
     all: [
       App\Product {#3034
         id: 1,
         name: "benz",
         user_id: 1,
         laravel_through_key: 2,
         seller: App\Seller {#3038
           id: 1,
           name: "mike",
           phone: "989898",
         },
       },
       App\Product {#3031
         id: 2,
         name: "bmw",
         user_id: 1,
         laravel_through_key: 2,
         seller: App\Seller {#3038},
       },
     ],
   }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

关系返回错误/空数据(Laravel 5.2)

来自分类Dev

Laravel关系数据返回

来自分类Dev

从关系中获取数据,Laravel

来自分类Dev

Laravel根据关系获取数据

来自分类Dev

Laravel 获取关系数据

来自分类Dev

雄辩的关系返回错误尝试在 Laravel 5.4 中获取非对象的属性

来自分类Dev

Laravel 5.3关系返回错误之一

来自分类Dev

当数据库中不存在值时,Laravel ORM关系返回错误

来自分类Dev

Laravel根据关系结果返回数据

来自分类Dev

Laravel受约束的关系不会返回数据

来自分类Dev

Laravel从多对多关系中获取数据

来自分类Dev

查询以获取Laravel中的关系数据

来自分类Dev

如何从Laravel中的关系获取数据?

来自分类Dev

在laravel中通过javascript获取数据关系

来自分类Dev

在 Laravel 中使用模型关系获取数据

来自分类Dev

从hasOne关系Laravel 4.1获取hasMany关系数据

来自分类Dev

Laravel从数据透视表获取数据的多对多关系

来自分类Dev

Laravel 5.6 如何获取模型的多态关系,返回null

来自分类Dev

laravel错误从数据库获取数据

来自分类Dev

从多对多关系返回的空数据laravel 5

来自分类Dev

返回laravel中的所有关系数据

来自分类Dev

Laravel + Vue 返回具有多个关系的数据

来自分类Dev

Laravel关系错误-试图获取非对象的属性

来自分类Dev

Laravel关系错误-试图获取非对象的属性

来自分类Dev

Laravel雄辩地返回错误的数据

来自分类Dev

Laravel:获取特定于多对多关系的数据透视

来自分类Dev

Laravel雄辩的模型如何从关系表中获取数据

来自分类Dev

从雄辩的构建者laravel 7获取关系数据

来自分类Dev

Laravel-尝试通过一对多关系获取数据

Related 相关文章

热门标签

归档