#eloquent relations not loaded

1 messages · Page 1 of 1 (latest)

full trellis
#

Hi all.. I'm newbie of laravel and I have an issue that I can't figured out.. I have connected my laravel to an IBMi AS400 with success and also queries through model works well. Now, I have an issue with relations. I have two tables: the First One Is the order header and the second One Is the order rows. The Classic order structure. In the Order model I've created a function called "rows" that returns an HasMany relations on specific foreign Key and destination Key. On the OrderRow model I've created a function called "order" that returns a belongTo relation. I've exactly followed the official guide.
Now, I want ti execute a query on the order header, using some where conditions that, for each order header returned, load also the corresponding rows (i have to create a rest api that returns the filtered orders with each associated row in a subkey of the resulting JSON). I've tried to do something like Order::with('rows')->where('field', value)->get() but without any success. The query returns correctly the Order header but the corresponding rows are empty. I've tried any type of additional package, syntax and so on but without success. During a debug session I see that, in some cases, it execute the First query as "select * from order where field = value" and then execute"select * from order_rows where fk_order_id in (?)" and null as parameter. So the rows query Is correct (even It can be Better ti use joins instead of multiple queries) but the parameter Is Always null.. any suggestion? Please help me!

bitter bear
#

show your actual code

#

also spend some time figuring out where the issue is actually coming from because 90% of this information is probably irrelevant

#

e.g. knowing that you are running this on a IBMi AS400 is irrelevant

rugged eagle
#

@full trellis And please learn about paragraphs. This is just a wall of text 😩

full trellis
full trellis
#

@bitter bear
Actual code
This is the Order model. The table is called BUCDTST00T and has BUCDID as primary key that is referenced on OrderRow table (BUCERIG00T) with foreign key BUCEIDBUCD (on rows table)
`
class Order extends Model
{
protected $connection = 'ibmi';
protected $table = 'KBCDTO_BRG.BUCDTST00T';
protected $primaryKey = 'BUCDID';

protected $fillable = ['BUCDID', 'BUCEIDBUCD'];

public function rows(): HasMany
{
    return $this->hasMany(OrderRow::class, 'BUCEIDBUCD', 'BUCDID');
}

}
This is the order row model
class OrderRow extends Model
{
protected $connection = 'ibmi';
protected $table = 'KBCDTO_BRG.BUCERIG00T';
protected $primaryKey = "BUCEID";

protected $fillable = ['BUCEIDBUCD', 'BUCDID'];

public function order(): BelongsTo
{
    return $this->belongsTo(Order::class, 'BUCEIDBUCD', 'BUCDID');
}

}
`
and the code that execute the query is this:

return Order::with('rows')->where('BUCDNRORD', 22222)->get();
What I need to extract is something like the order structure with a field "rows" that is an array of rows related to the order header

bitter bear
#
  1. most likely you also have a table or column name somewhere that is wrong but since they're illegible we won't know if thats the case
full trellis
#

@bitter bear unfortunately is the way that AS400 old style used to name it's columns..

#

array:2 [▼ // app/Http/Controllers/DBController.php:33
0 => array:3 [▼
"query" => "select * from KBCDTO_BRG.BUCDTST00T where BUCDNRORD = ? FETCH FIRST 1 ROWS ONLY"
"bindings" => array:1 [▼
0 => "22387217"
]
"time" => 45.54
]
1 => array:3 [▼
"query" => "select * from KBCDTO_BRG.BUCERIG00T where KBCDTO_BRG.BUCERIG00T.BUCEIDBUCD in (0)"
"bindings" => []
"time" => 33.22
]
]

#

anyway.. this is the output of query debugger.. the query, splitted in two steps, is formally correct

bitter bear
#

This is your app ... you can name the tables and columns however you want. Is this a database from an existing application?

full trellis
#

yes, is an existing db

#

the problem, as you can see from the snippet, is that the query must have has BUCEIDBUCD in param the value 22387217

#

but it gets zero

#

so, it creates correctly the queries

#

but in the second one, use the wrong parameter

#

if the second query became select * from KBCDTO_BRG.BUCERIG00T where KBCDTO_BRG.BUCERIG00T.BUCEIDBUCD in (22387217) the result (on db) is correct.. I don't understand why it is able to create the correct SQL but lacks the parameter

bitter bear
#

The first query probably returned no results

#

hence no ids passed to query 2

full trellis
#

nope, the first query returns the correct result (also on webpage)

bitter bear
#

Are you sure? You aren't running the query in multiple places?

full trellis
#

nope.. only this simple row of code

#

return Order::with('rows')->where('BUCDNRORD', 22222)->get();

bitter bear
#

If thats the case why would your query bindings be 22387217 instead of 22222?

full trellis
#

ok.. is only an example.. sorry but the problem is not in the parameter.. i've tried also with fixed params and without params using first() or limit

bitter bear
#

okay what gets returned if you run dd(Order::where('BUCDNRORD', 22387217 )->get())?

full trellis
bitter bear
#

okay and what is returned if you run dd(OrderRow::where('BUCEIDBUCD', 22387217)->get())?

#

If you had standard column names your relationships should look like this:

class Order extends Model
{
    public function orderRows(): HasMany
    {
        return $this->hasMany(
          OrderRow::class, 
          'order_id', // e.g. order_rows.order_id
          'id' // e.g. orders.id
);
    }
}

class OrderRow extends Model
{
    public function order(): BelongsTo
    {
        return $this->belongsTo(
          Order::class, 
          'order_id', // e.g. order_rows.order_id
          'id' // e.g. orders.id
);
    }
}
full trellis
full trellis
#

@bitter bear ok, I've found how to generate correctly the second query

#

is a problem related to the casing of column name (AS400 wants UPPERCASE columns and laravel lowercase one)