简体   繁体   中英

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. I am doing smething wrong and I dont know what, because I get some of data from Suppliers table and empty array . 获得了一些数据

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. Here I give You a graphic representation:

忙碌的猫

Here`s my code:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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 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?

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). You can also specify the keys on the relationship if you want to use hasOne

Edit: add the keys names within the relation, your fk naming is in capslock

Change you relations like bel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

<?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. https://laravel.com/docs/5.7/eloquent-relationships#one-to-many

Let me know if you still getting same error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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