简体   繁体   English

如何从Laravel中的两个相关表中获取所有数据[一对多]

[英]How to get all data from two related tables in Laravel [one to many]

I am pretty new in Laravel and need write a simple backend API. 我在Laravel中很陌生,需要编写一个简单的后端API。 I am doing smething wrong and I dont know what, because I get some of data from Suppliers table and empty array payments:[ ] . 我做错了什么,我不知道怎么办,因为我从Suppliers表和空数组付款中获得了一些数据:[]

I am trying to get all data from two related tables - PAYMENTS and SUPPLIERS. 我正在尝试从两个相关的表中获取所有数据-付款和供应商。 It`sa one to many relation SUPPLIERS_ID in PAYMENTS table is connected with ID in SUPPLIERS. PAYMENTS表中的SUPPLIERS_ID与SUPPLIERS中的ID有一对多的关系。 Here I give You a graphic representation: 在这里,我给您一个图形表示:

忙碌的猫

Here`s my code: 这是我的代码:

Suppliers.php model Suppliers.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Suppliers extends Model
{
   public function payments()
   {
      return $this->hasMany('App\Payments'); 
   }
}

Payments.php model Payments.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payments extends Model
{
   public function suppliers()
   {
      return $this->hasOne('App\Suppliers'); 
  }
}

PaymentsController.php PaymentsController.php

use App\Payments;
use App\Suppliers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class PaymentsController extends Controller
{

public function index()
   {    
      $payments = Suppliers::with('payments')->get();
      return response($payments, Response::HTTP_OK);
   }
}

And i get the following answear: 我得到以下的回答:

[{"id":1,"name":"Ekonaft","adress":"33-100 Tarnow ","email":"ekonaft@gmail.com","payments":[]}, 
{"id":2,"name":"Orlen","adress":"Ares testowy","email":"email@email.pl","payments":[]}]

What I`m doing wrong that I get te empty array payments:[ ] on the end of each object? 我做错了什么,我在每个对象的末尾都得到了空数组付款:[]

Try the inverse relationship on payments 尝试付款的逆向关系

belongsTo = has a foreign key to another table

Quoting an example 举个例子

Should i use belongsTo or hasOne in Laravel? 我应该在Laravel中使用belongsTo或hasOne吗?

This is how you can access suppliers from Payments 这是您可以从“付款”中访问供应商的方式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payments extends Model
{
   public function suppliers()
   {
      return $this->belongsTo('App\Suppliers'); 
  }
}

This is payments from suppliers 这是供应商的付款

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Suppliers extends Model
{
   public function payments()
   {
      return $this->hasMany('App\Payments','suppliers_ID','id'); 
   }
}

Also, make sure the id's are visible on the output (if id's are hidden, laravel can't work with the relationship). 另外,请确保ID在输出中可见(如果ID被隐藏,则laravel无法处理该关系)。 You can also specify the keys on the relationship if you want to use hasOne 如果要使用hasOne还可以在关系上指定键

Edit: add the keys names within the relation, your fk naming is in capslock 编辑:在关系中添加键名称,您的fk命名为capslock

Change you relations like bel 像贝尔一样改变你的关系

Suppliers.php model Suppliers.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Suppliers extends Model
{
   public function payments()
   {
      return $this->hasMany(App\Payments::class, 'suppliers_ID', 'id'); 
   }
}

Payments.php model Payments.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payments extends Model
{
   public function suppliers()
   {
      return $this->belongsTo(App\Suppliers::class, 'suppliers_ID', 'id'); 
  }
}

then try again..... :) 然后再试一次..... :)

You are getting empty array of payments:[] due to miss-matching table relationship key name. 由于未匹配的表关系键名称,您将得到空的付款数组:[]

Please, make few changes in both relational function.

public function payments() 
{

    //return $this->hasMany('App\Model', 'foreign_key', 'local_key');
    return $this->hasMany('App\Payments', 'suppliers_id'); 

}

public function suppliers()
{

    //return $this->belongsTo('App\Model', 'foreign_key', 'other_key');
    return $this->belongsTo('App\Suppliers', 'suppliers_id'); 

}

You can learn more about eloquent relationship directly from Laravel documentation for better understanding. 您可以直接从Laravel文档中了解有关雄辩关系的更多信息,以更好地理解。 https://laravel.com/docs/5.7/eloquent-relationships#one-to-many https://laravel.com/docs/5.7/eloquent-relationships#one-to-many

Let me know if you still getting same error. 让我知道您是否仍然遇到相同的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM