Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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_Laravel_Laravel 5_Eloquent_Laravel Eloquent - Fatal编程技术网

Php Laravel雄辩-如何加入表格

Php Laravel雄辩-如何加入表格,php,laravel,laravel-5,eloquent,laravel-eloquent,Php,Laravel,Laravel 5,Eloquent,Laravel Eloquent,我正在为我的应用程序开发API 我试图从数据库中提取项目并以JSON对象的形式返回它们, “我的项目”表如下所示: Items -id -name -description -price -currency_id -company_id [ { 'id':'1', 'name':'name', 'description': 'example', 'price':'100', 'currency':{ 'id':'1', 'name

我正在为我的应用程序开发API

我试图从数据库中提取项目并以JSON对象的形式返回它们, “我的项目”表如下所示:

Items
-id
-name
-description
-price
-currency_id
-company_id
[
  {
    'id':'1',
    'name':'name',
    'description': 'example',
    'price':'100',
    'currency':{
     'id':'1',
     'name':'usd',
     'symbol': '$'
     }
  }
]
以下是我获取项目的方式:

$rows = Company::where('guid',$guid)
                ->first()
                ->items()
                ->orderBy('name', $sort_order);
我想用包含currency表所有列的currency对象替换currency\u id

因此,我的结果如下:

Items
-id
-name
-description
-price
-currency_id
-company_id
[
  {
    'id':'1',
    'name':'name',
    'description': 'example',
    'price':'100',
    'currency':{
     'id':'1',
     'name':'usd',
     'symbol': '$'
     }
  }
]
更新: 这是我的货币表:

id
name
symbol
code
试试下面

在项目模型中建立一个关系

 public function currencies() {
        return $this->hasMany('App\Currency');
     } 
然后在控制器中执行以下操作

$row=Items::All()->with('currencies');

编辑2:用户的问题比这更复杂,因为有分页和搜索与查询集成。帮助

编辑:我已经测试了代码。所以在这里

公司内模式

public function items()
{
    return $this->hasMany(Item::class);
}
项目内模型

public function currency()
{
    return $this->belongsTo(Currency::class);
}
控制器逻辑

$items = Company::with(['items' => function($query) use ($sort_order) {
    $query->with('currency')->orderBy('name', $sort_order);
}])
    ->where('guid', $guid)
    ->first()
    ->items;
结果与测试数据一致

[
    {
        "id": 2,
        "name": "Toy",
        "description": "Random text 2",
        "price": 150,
        "company_id": 1,
        "currency_id": 1,
        "currency": {
            "id": 1,
            "name": "usd",
            "symbol": "$",
            "code": "USD"
        }
    },
    {
        "id": 1,
        "name": "Phone",
        "description": "Random text",
        "price": 100,
        "company_id": 1,
        "currency_id": 1,
        "currency": {
            "id": 1,
            "name": "usd",
            "symbol": "$",
            "code": "USD"
        }
    }
]

试试这个

$rows = Company::with('items.currency')
    ->where('guid', $guid)
    ->first()
    ->items()
    ->orderBy('name', $sort_order);

事实上,我的关系是不同的,因为每个项目只有一种货币。公共函数currency(){return$this->hasOne('App\currency');}我想获取公司的项目,正如您在我的示例中所看到的,我得到了这个错误:“where子句”中的未知列“currences.item_id”(SQL:select*from
currency
where
currency
item_id您可以发布您的货币表结构吗?您在其他注释中发布的关系是错误的,请将其更改为
公共函数currency(){return$this->belongsTo('App\currency');}
现在我没有得到错误,但仍然没有得到我需要的结果,我想返回一个项目数组,在该数组中,我想返回货币对象,而不是返回货币id。我想返回货币对象是来自项目表或公司表的order by字段
name
?我将编写一些测试代码,在您回复后返回给您东南方