staudenmeir/eloquent-json-relations

Relationship queries fail for string Ids

musab-olurode opened this issue · 28 comments

Hi. The package works wonderfully and has been nothing short of incredible since discovering it. I noticed though, that relationship queries fail when used with records that have ids of random strings (as is the case in some parts of my implementation) instead of the expected integer.

As an example, a relation defined like so:

public function collectedScrap()  
{
    return $this->belongsToJson(CollectedScrap::class, 'collection_ids->collection_id');
}

where the collection_ids is an array of ids that are of six random letters and numbers:

["IJSM9","JSN3HG","HYSH7T"]

returns an empty array.
I would like to know if the package even supports this behaviour and how I might go about implementing a workaround if it doesn't.

Thanks.

The package does also support string IDs. Please share the CollectedScrap model.

Thanks for the quick reply. Here's the model you asked for

class CollectedScrap extends Model
{

    use SoftDeletes, \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'total_cost',
        'materials',
        'payment_mode',
        'total_tonnage',
        'address',
        'producer_id',
        'producer_type',
        'collector_id',
        'agent_id',
        'pickup_id',
        'drop_off_id',
        'collection_type',
        'material_state',
        'location_id',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'updated_at',
    ];

    protected $dates = ['deleted_at'];

    protected $primaryKey = "id";

    public $timestamps = true;

    public $incrementing = false;

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'materials' => 'array',
    ];

    protected $with = [
        'producer:id,first_name,last_name',
        'location:id,lat,lng',
        'homogeneous_materials:id,name,price,image,collector_commission',
        'composite_materials:id,class,producer_commission,collector_commission',
    ];

    public static function boot()
    {
        parent::boot();

        static::creating(function ($collected_scrap) {
            $collected_scrap->id = strtoupper(Str::random(6));
        });
    }

    // public function getMaterialsAttribute($materials)
    // {
    //     return array_merge($this->homogeneous_materials(), $this->composite_materials());
    // }

    public function producer()
    {
        return $this->belongsTo(User::class);
    }

    public function collector()
    {
        return $this->belongsTo(User::class);
    }

    public function household()
    {
        return $this->belongsTo(User::class);
    }

    public function enterprise()
    {
        return $this->belongsTo(User::class);
    }

    public function pickupRequest()
    {
        return $this->belongsTo(PickupRequest::class);
    }

    public function dropOff()
    {
        return $this->belongsTo(DropOff::class);
    }

    public function location()
    {
        return $this->belongsTo(Location::class);
    }

    public function homogeneous_materials()
    {
        return $this->belongsToJson(Material::class, 'materials[]->homogeneous_id');
    }

    public function composite_materials()
    {
        return $this->belongsToJson(CompositeMaterial::class, 'materials[]->composite_id');
    }
}

Please try adding protected $keyType = 'string'; to the CollectedScrap model.

Just tried that. Relationship still returning an empty array.

Please share the model that contains the collectedScrap relationship.

Here it is:

class DropOff extends Model
{
    use SoftDeletes, \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'collector_id',
        'agent_id',
        'collection_ids',
    ];

    protected $with = [
        'collector',
        'agent',
        'collected_scrap',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'collection_ids' => 'array',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    // protected $appends = ['collected_scrap'];

    public function collector()
    {
        return $this->belongsTo(User::class);
    }

    public function agent()
    {
        return $this->belongsTo(User::class);
    }

    public function collected_scrap()
    {
        return $this->belongsToJson(CollectedScrap::class, 'collection_ids->collection_id');
    }

    // public function getCollectedScrapAttribute()
    // {
    //     return CollectedScrap::findMany($this->collection_ids);
    // }
}

How are you using the collectedScrap relationship?

I'm adding it to the model's with array so that its always loaded during model load:

protected $with = [
        'collector',
        'agent',
        'collected_scrap',
    ];

Please log the executed queries.

Here is the query for the relationship:

array:3 [
    "query" => "select * from `collected_scraps` where 0 = 1 and `collected_scraps`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.62
  ]

I also just noticed from the logs that its not actually only failing with an array of string ids but with all array of Ids

How are you fetching the DropOff model(s) in this case?

It's being gotten from another model's relationship:

    public function assignedDropOffs()
    {
        return $this->hasMany(DropOff::class, 'agent_id');
    }

I'm querying the User model's assignedDropOffs which returns this relationship.

This relationship then gets the dropOff model and tries to query its collectedScrap relationship which results in the empty array

Please share the whole chain of queries.

here it is:

array:18 [
  0 => array:3 [
    "query" => "select * from `drop_offs` where `drop_offs`.`agent_id` = ? and `drop_offs`.`agent_id` is not null and `drop_offs`.`deleted_at` is null"
    "bindings" => array:1 [
      0 => "PISJ98"
    ]
    "time" => 0.48
  ]
  1 => array:3 [
    "query" => "select * from `users` where `users`.`id` in (0) and `users`.`deleted_at` is null"
    "bindings" => []
    "time" => 10.8
  ]
  2 => array:3 [
    "query" => "select * from `households` where `households`.`id` in (597, 1635, 138, 1640, 1467, 1431, 1511, 1451, 1039, 1505, 1344, 70, 519, 499, 1375, 44, 1449, 335, 296, 1252, 362, 1074, 1297, 1203, 1021, 352, 495, 1348, 25, 401, 145, 341, 1667, 1664, 212, 379, 113, 105, 459, 1340, 135, 1076, 290, 60, 1073, 1167, 433, 1496, 484, 1210, 509, 387, 1376, 1658, 1302, 630, 1516, 263, 623, 1617, 1421, 1164, 1538, 1120, 197, 1143, 364, 42, 563, 46, 383, 271, 124, 1359, 541, 1466, 501, 1016, 457, 351, 1476, 1139, 1151, 1241, 1101, 299, 266, 230, 356, 338, 504, 359, 1341, 101, 127, 160, 1108, 112, 1181, 225, 1399, 1356, 1294, 452, 386, 1277, 1185, 397, 1591, 1566, 579, 1655, 545, 196, 180, 1382, 1335, 1535, 443, 1416, 1558, 486, 1157, 1406, 1485, 1301, 598, 1352, 498, 216, 466, 1149, 1163, 1232, 300, 69, 119, 1615, 415, 1072, 171, 205, 1196, 181, 392, 161, 1532, 1285, 1537, 396, 86, 317, 1649, 252, 1652, 231, 476, 1123, 320, 1318, 1357, 445, 1370, 1276, 400, 1234, 1584, 72, 381, 1062, 1479, 1338, 1064, 527, 518, 125, 1636, 531, 1186, 469, 142, 1527, 1134, 1372, 289, 458, 274, 511, 358, 332, 446, 1470, 328, 462, 360, 487, 333, 1180, 481, 307, 428, 539, 1433, 244, 1497, 117, 239, 513, 41, 282, 47, 1046, 13, 1474, 1627, 126, 1260, 1578, 1540, 185, 1258, 118, 1440, 347, 1409, 1044, 1424, 235, 1585, 1508, 1363, 612, 148, 1106, 1395, 594, 523, 548, 572, 1412, 259, 326, 1329, 1435, 1088, 184, 361, 1403, 1619, 1588, 625, 485, 1225, 483, 1386, 1323, 1642, 631, 479, 567, 574, 1574, 1360, 1571, 269, 429, 99, 1095, 1130, 1109, 262, 1069, 418, 49, 218, 1364, 1066, 1529, 1351, 464, 1026, 250, 1437, 1647, 1315, 1669, 34, 295, 207, 1441, 1457, 522, 1308, 375, 1400, 622, 372, 1502, 378, 1545, 1651, 1217, 1379, 1254, 1215, 1019, 1673, 357, 1597, 1075, 1623, 211, 1405, 1140, 508, 1156, 1599, 1061, 1065, 1054, 581, 324, 1530, 100, 402, 255, 470, 1452, 144, 1480, 1402, 1575, 492, 321, 1534, 1113, 566, 256, 1262, 337, 1622, 1563, 1102, 1170, 1087, 421, 1618, 1020, 1296, 488, 1230, 411, 1213, 294, 1526, 432, 1159, 234, 444, 530, 580, 1590, 20, 312, 1643, 1048, 593, 363, 130, 1144, 1522, 1397, 1477, 1086, 1408, 373, 1309, 490, 369, 35, 327, 1314, 1034, 618, 1059, 257, 521, 1286, 1169, 245, 1327, 1197, 1295, 410, 632, 628, 1609, 36, 1495, 1407, 350, 1365, 533, 279, 1427, 1045, 179, 1033, 557, 71, 1601, 236, 137, 1025, 1199, 1570, 232, 1190, 23, 605, 1366, 1666, 147, 512, 302, 1547, 463, 1145, 1586, 570, 1611, 1550, 1068, 133, 544, 11, 1124, 1011, 1393, 146, 68, 1330, 1292, 1282, 210, 1504, 136, 408, 1568, 97, 1515, 32, 1637, 278, 611, 50, 80, 1306, 1243, 1616, 1650, 1103, 67, 1188, 573, 388, 1153, 584, 414, 405, 1334, 134, 1462, 301, 1561, 1436, 273, 325, 178, 604, 1447, 1546, 420, 220, 272, 156, 1274, 65, 1389, 538, 115, 1337, 1443, 1028, 1384, 1119, 1192, 193, 267, 413, 1080, 575, 1097, 559, 246, 204, 1324, 1242, 96, 169, 322, 1229, 607, 500, 87, 14, 1336, 1195, 478, 448, 31, 30, 552, 477, 461, 1422, 1079, 1036, 1268, 1012, 1098, 1445, 1373, 57, 200, 1022, 1114, 195, 102, 16, 547, 43, 1037, 1082, 617, 1251, 1548, 409, 1090, 473, 1261, 203, 371, 1057, 436, 339, 1378, 152, 1612, 1552, 1521, 626, 128, 561, 1463, 1439, 1572, 532, 58, 1475, 562, 1539, 103, 183, 198, 247, 1058, 154, 1051, 1031, 1391, 1154, 55, 1555, 1473, 1325, 228, 315, 1116, 441, 1608, 583, 1198, 1331, 288, 1172, 1369, 439, 1491, 1015, 1486, 1228, 188, 542, 61, 1320, 620, 348, 366, 1417, 507, 1541, 1291, 1450, 1041, 368, 309, 1429, 1580, 1345, 1316, 1512, 1310, 182, 157, 304, 1426, 1328, 221, 1387, 517, 1175, 526, 173, 529, 233, 1656, 64, 1244, 1531, 1013, 1313, 1670, 1043, 629, 88, 1290, 172, 475, 121, 1231, 355, 1024, 424, 1293, 251, 613, 582, 1592, 1605, 1201, 62, 390, 569, 83, 1461, 107, 1562, 1401, 1583, 1111, 599, 1161, 1091, 1587, 1222, 616, 1321, 1240, 1174, 1349, 1227, 546, 310, 1135, 578, 206, 493, 1038, 1141, 1353, 94, 1221, 202, 141, 1131, 129, 1055, 1219, 190, 336, 89, 1659, 311, 1564, 1374, 1137, 1158, 33, 90, 460, 1305, 438, 277, 1256, 1237, 1607, 187, 1602, 1162, 215, 376, 1582, 1483, 505, 275, 1490, 1089, 330, 395, 258, 365, 1557, 1600, 1056, 1049, 81, 1551, 1520, 550, 1674, 1577, 592, 108, 1206, 1654, 1367, 506, 59, 394, 1362, 37, 343, 292, 540, 270, 334, 123, 170, 1096, 208, 149, 1668, 1245, 556, 1553, 1645, 78, 1420, 1646, 1517, 1267, 162, 1050, 242, 1432, 1339, 1544, 1503, 1454, 286, 377, 1273, 1010, 201, 276, 1200, 1319, 1204, 1469, 1630, 1287, 1032, 564, 98, 1187, 1396, 1052, 1312, 576, 1160, 404, 417, 465, 419, 238, 165, 1168, 1629, 551, 1257, 298, 1289, 510, 22, 1549, 603, 560, 92, 73, 1099, 209, 1281, 520, 1603, 1121, 1150, 1040, 1171, 189, 1392, 480, 1419, 1604, 52, 1216, 393, 497, 1083, 1624, 1146, 1275, 151, 571, 1300, 331, 1488, 1448, 318, 153, 66, 1593, 1626, 54, 1184, 1456, 38, 1077, 416, 502, 1438, 1425, 191, 1455, 1528, 389, 619, 565, 1191, 596, 591, 553, 150, 74, 159, 1085, 554, 1035, 1410, 588, 515, 1398, 166, 253, 1506, 1182, 139, 1641, 1223, 1606, 287, 1067, 316, 624, 1507, 1233, 482, 1524, 314, 514, 1070, 1122, 391, 39, 568, 353, 1632, 1148, 1105, 1248, 194, 84, 1404, 77, 1380, 1500, 1665, 1042, 1255, 1596, 1493, 1377, 1263, 1355, 468, 226, 1279, 442, 1519, 1071, 1661, 1489, 106, 1625, 186, 1595, 472, 555, 615, 382, 1194, 1224, 284, 1166, 1283, 440, 1358, 1053, 1589, 248, 213, 1390, 1333, 24, 1117, 1347, 1518, 241, 1205, 1543, 1202, 1631, 114, 1468, 281, 430, 293, 1471, 1560, 1023, 549, 1559, 1423, 496, 1459, 1671, 1648, 110, 425, 1569, 1029, 303, 131, 491, 1133, 1411, 1594, 1100, 1453, 1118, 1177, 340, 1332, 1104, 426, 503, 167, 48, 384, 79, 164, 155, 1081, 1272, 76, 1078, 1434, 1214, 1639, 1509, 1662, 240, 370, 1554, 1127, 543, 427, 1278, 132, 1513, 1567, 1482, 158, 1464, 354, 602, 601, 1322, 528, 1415, 243, 1487, 283, 1107, 53, 1208, 104, 609, 1269, 621, 1620, 431, 1136, 1613, 606, 27, 374, 199, 176, 45, 177, 346, 534, 1621, 222, 91, 1299, 1444, 1446, 1514, 608, 455, 1250, 1176, 1047, 1576, 535, 319, 143, 1132, 1235, 454, 1465, 268, 434, 1129, 1183, 1657, 471, 1092, 306, 1147, 1027, 1361, 122, 1063, 437, 1413, 403, 1247, 19, 1458, 1270, 1346, 168, 85, 95, 111, 28, 26, 192, 1238, 1189, 1218, 345, 1510, 1494, 380) and `households`.`deleted_at` is null"
    "bindings" => []
    "time" => 3.68
  ]
  3 => array:3 [
    "query" => "select * from `collectors` where `collectors`.`id` in (7, 2, 5, 1, 14, 13, 10, 11, 4, 8, 9, 16, 15, 17, 18, 12) and `collectors`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.47
  ]
  4 => array:3 [
    "query" => "select `id`, `lga`, `name` from `major_areas` where 0 = 1 and `major_areas`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.33
  ]
  5 => array:3 [
    "query" => "select * from `enterprises` where `enterprises`.`id` in (5, 10, 12, 8, 15, 14, 13, 6, 9, 7, 1, 3, 16) and `enterprises`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.49
  ]
  6 => array:3 [
    "query" => "select * from `admins` where `admins`.`id` in (5, 1, 3, 4) and `admins`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.31
  ]
  7 => array:3 [
    "query" => "select * from `agents` where `agents`.`id` in (1, 2, 3) and `agents`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.28
  ]
  8 => array:3 [
    "query" => "select `id`, `lat`, `lng` from `locations` where `locations`.`id` in (0, 1, 2) and `locations`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.42
  ]
  9 => array:3 [
    "query" => "select * from `users` where `users`.`id` in (0) and `users`.`deleted_at` is null"
    "bindings" => []
    "time" => 11.61
  ]
  10 => array:3 [
    "query" => "select * from `households` where `households`.`id` in (597, 1635, 138, 1640, 1467, 1431, 1511, 1451, 1039, 1505, 1344, 70, 519, 499, 1375, 44, 1449, 335, 296, 1252, 362, 1074, 1297, 1203, 1021, 352, 495, 1348, 25, 401, 145, 341, 1667, 1664, 212, 379, 113, 105, 459, 1340, 135, 1076, 290, 60, 1073, 1167, 433, 1496, 484, 1210, 509, 387, 1376, 1658, 1302, 630, 1516, 263, 623, 1617, 1421, 1164, 1538, 1120, 197, 1143, 364, 42, 563, 46, 383, 271, 124, 1359, 541, 1466, 501, 1016, 457, 351, 1476, 1139, 1151, 1241, 1101, 299, 266, 230, 356, 338, 504, 359, 1341, 101, 127, 160, 1108, 112, 1181, 225, 1399, 1356, 1294, 452, 386, 1277, 1185, 397, 1591, 1566, 579, 1655, 545, 196, 180, 1382, 1335, 1535, 443, 1416, 1558, 486, 1157, 1406, 1485, 1301, 598, 1352, 498, 216, 466, 1149, 1163, 1232, 300, 69, 119, 1615, 415, 1072, 171, 205, 1196, 181, 392, 161, 1532, 1285, 1537, 396, 86, 317, 1649, 252, 1652, 231, 476, 1123, 320, 1318, 1357, 445, 1370, 1276, 400, 1234, 1584, 72, 381, 1062, 1479, 1338, 1064, 527, 518, 125, 1636, 531, 1186, 469, 142, 1527, 1134, 1372, 289, 458, 274, 511, 358, 332, 446, 1470, 328, 462, 360, 487, 333, 1180, 481, 307, 428, 539, 1433, 244, 1497, 117, 239, 513, 41, 282, 47, 1046, 13, 1474, 1627, 126, 1260, 1578, 1540, 185, 1258, 118, 1440, 347, 1409, 1044, 1424, 235, 1585, 1508, 1363, 612, 148, 1106, 1395, 594, 523, 548, 572, 1412, 259, 326, 1329, 1435, 1088, 184, 361, 1403, 1619, 1588, 625, 485, 1225, 483, 1386, 1323, 1642, 631, 479, 567, 574, 1574, 1360, 1571, 269, 429, 99, 1095, 1130, 1109, 262, 1069, 418, 49, 218, 1364, 1066, 1529, 1351, 464, 1026, 250, 1437, 1647, 1315, 1669, 34, 295, 207, 1441, 1457, 522, 1308, 375, 1400, 622, 372, 1502, 378, 1545, 1651, 1217, 1379, 1254, 1215, 1019, 1673, 357, 1597, 1075, 1623, 211, 1405, 1140, 508, 1156, 1599, 1061, 1065, 1054, 581, 324, 1530, 100, 402, 255, 470, 1452, 144, 1480, 1402, 1575, 492, 321, 1534, 1113, 566, 256, 1262, 337, 1622, 1563, 1102, 1170, 1087, 421, 1618, 1020, 1296, 488, 1230, 411, 1213, 294, 1526, 432, 1159, 234, 444, 530, 580, 1590, 20, 312, 1643, 1048, 593, 363, 130, 1144, 1522, 1397, 1477, 1086, 1408, 373, 1309, 490, 369, 35, 327, 1314, 1034, 618, 1059, 257, 521, 1286, 1169, 245, 1327, 1197, 1295, 410, 632, 628, 1609, 36, 1495, 1407, 350, 1365, 533, 279, 1427, 1045, 179, 1033, 557, 71, 1601, 236, 137, 1025, 1199, 1570, 232, 1190, 23, 605, 1366, 1666, 147, 512, 302, 1547, 463, 1145, 1586, 570, 1611, 1550, 1068, 133, 544, 11, 1124, 1011, 1393, 146, 68, 1330, 1292, 1282, 210, 1504, 136, 408, 1568, 97, 1515, 32, 1637, 278, 611, 50, 80, 1306, 1243, 1616, 1650, 1103, 67, 1188, 573, 388, 1153, 584, 414, 405, 1334, 134, 1462, 301, 1561, 1436, 273, 325, 178, 604, 1447, 1546, 420, 220, 272, 156, 1274, 65, 1389, 538, 115, 1337, 1443, 1028, 1384, 1119, 1192, 193, 267, 413, 1080, 575, 1097, 559, 246, 204, 1324, 1242, 96, 169, 322, 1229, 607, 500, 87, 14, 1336, 1195, 478, 448, 31, 30, 552, 477, 461, 1422, 1079, 1036, 1268, 1012, 1098, 1445, 1373, 57, 200, 1022, 1114, 195, 102, 16, 547, 43, 1037, 1082, 617, 1251, 1548, 409, 1090, 473, 1261, 203, 371, 1057, 436, 339, 1378, 152, 1612, 1552, 1521, 626, 128, 561, 1463, 1439, 1572, 532, 58, 1475, 562, 1539, 103, 183, 198, 247, 1058, 154, 1051, 1031, 1391, 1154, 55, 1555, 1473, 1325, 228, 315, 1116, 441, 1608, 583, 1198, 1331, 288, 1172, 1369, 439, 1491, 1015, 1486, 1228, 188, 542, 61, 1320, 620, 348, 366, 1417, 507, 1541, 1291, 1450, 1041, 368, 309, 1429, 1580, 1345, 1316, 1512, 1310, 182, 157, 304, 1426, 1328, 221, 1387, 517, 1175, 526, 173, 529, 233, 1656, 64, 1244, 1531, 1013, 1313, 1670, 1043, 629, 88, 1290, 172, 475, 121, 1231, 355, 1024, 424, 1293, 251, 613, 582, 1592, 1605, 1201, 62, 390, 569, 83, 1461, 107, 1562, 1401, 1583, 1111, 599, 1161, 1091, 1587, 1222, 616, 1321, 1240, 1174, 1349, 1227, 546, 310, 1135, 578, 206, 493, 1038, 1141, 1353, 94, 1221, 202, 141, 1131, 129, 1055, 1219, 190, 336, 89, 1659, 311, 1564, 1374, 1137, 1158, 33, 90, 460, 1305, 438, 277, 1256, 1237, 1607, 187, 1602, 1162, 215, 376, 1582, 1483, 505, 275, 1490, 1089, 330, 395, 258, 365, 1557, 1600, 1056, 1049, 81, 1551, 1520, 550, 1674, 1577, 592, 108, 1206, 1654, 1367, 506, 59, 394, 1362, 37, 343, 292, 540, 270, 334, 123, 170, 1096, 208, 149, 1668, 1245, 556, 1553, 1645, 78, 1420, 1646, 1517, 1267, 162, 1050, 242, 1432, 1339, 1544, 1503, 1454, 286, 377, 1273, 1010, 201, 276, 1200, 1319, 1204, 1469, 1630, 1287, 1032, 564, 98, 1187, 1396, 1052, 1312, 576, 1160, 404, 417, 465, 419, 238, 165, 1168, 1629, 551, 1257, 298, 1289, 510, 22, 1549, 603, 560, 92, 73, 1099, 209, 1281, 520, 1603, 1121, 1150, 1040, 1171, 189, 1392, 480, 1419, 1604, 52, 1216, 393, 497, 1083, 1624, 1146, 1275, 151, 571, 1300, 331, 1488, 1448, 318, 153, 66, 1593, 1626, 54, 1184, 1456, 38, 1077, 416, 502, 1438, 1425, 191, 1455, 1528, 389, 619, 565, 1191, 596, 591, 553, 150, 74, 159, 1085, 554, 1035, 1410, 588, 515, 1398, 166, 253, 1506, 1182, 139, 1641, 1223, 1606, 287, 1067, 316, 624, 1507, 1233, 482, 1524, 314, 514, 1070, 1122, 391, 39, 568, 353, 1632, 1148, 1105, 1248, 194, 84, 1404, 77, 1380, 1500, 1665, 1042, 1255, 1596, 1493, 1377, 1263, 1355, 468, 226, 1279, 442, 1519, 1071, 1661, 1489, 106, 1625, 186, 1595, 472, 555, 615, 382, 1194, 1224, 284, 1166, 1283, 440, 1358, 1053, 1589, 248, 213, 1390, 1333, 24, 1117, 1347, 1518, 241, 1205, 1543, 1202, 1631, 114, 1468, 281, 430, 293, 1471, 1560, 1023, 549, 1559, 1423, 496, 1459, 1671, 1648, 110, 425, 1569, 1029, 303, 131, 491, 1133, 1411, 1594, 1100, 1453, 1118, 1177, 340, 1332, 1104, 426, 503, 167, 48, 384, 79, 164, 155, 1081, 1272, 76, 1078, 1434, 1214, 1639, 1509, 1662, 240, 370, 1554, 1127, 543, 427, 1278, 132, 1513, 1567, 1482, 158, 1464, 354, 602, 601, 1322, 528, 1415, 243, 1487, 283, 1107, 53, 1208, 104, 609, 1269, 621, 1620, 431, 1136, 1613, 606, 27, 374, 199, 176, 45, 177, 346, 534, 1621, 222, 91, 1299, 1444, 1446, 1514, 608, 455, 1250, 1176, 1047, 1576, 535, 319, 143, 1132, 1235, 454, 1465, 268, 434, 1129, 1183, 1657, 471, 1092, 306, 1147, 1027, 1361, 122, 1063, 437, 1413, 403, 1247, 19, 1458, 1270, 1346, 168, 85, 95, 111, 28, 26, 192, 1238, 1189, 1218, 345, 1510, 1494, 380) and `households`.`deleted_at` is null"
    "bindings" => []
    "time" => 4.81
  ]
  11 => array:3 [
    "query" => "select * from `collectors` where `collectors`.`id` in (7, 2, 5, 1, 14, 13, 10, 11, 4, 8, 9, 16, 15, 17, 18, 12) and `collectors`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.54
  ]
  12 => array:3 [
    "query" => "select `id`, `lga`, `name` from `major_areas` where 0 = 1 and `major_areas`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.3
  ]
  13 => array:3 [
    "query" => "select * from `enterprises` where `enterprises`.`id` in (5, 10, 12, 8, 15, 14, 13, 6, 9, 7, 1, 3, 16) and `enterprises`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.47
  ]
  14 => array:3 [
    "query" => "select * from `admins` where `admins`.`id` in (5, 1, 3, 4) and `admins`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.25
  ]
  15 => array:3 [
    "query" => "select * from `agents` where `agents`.`id` in (1, 2, 3) and `agents`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.26
  ]
  16 => array:3 [
    "query" => "select `id`, `lat`, `lng` from `locations` where `locations`.`id` in (0, 1, 2) and `locations`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.46
  ]
  17 => array:3 [
    "query" => "select * from `collected_scraps` where 0 = 1 and `collected_scraps`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.32
  ]
]

This also contains a few unrelated queries though

Does DropOff::find($id)->collected_scrap work by itself with/without $with? If not, what queries are executed?

DropOff::find($id)->collected_scrap also returns an empty array and I couldn't get the query run for the ->collected_scrap to show up. (only "query" => "select * from `drop_offs` where `drop_offs`.`id` = ? and `drop_offs`.`deleted_at` is null limit 1" shows up).

DropOff::find($id)->with('collected_scrap')->get() though returns

array:3 [
  0 => array:3 [
    "query" => "select * from `drop_offs` where `drop_offs`.`id` = ? and `drop_offs`.`deleted_at` is null limit 1"
    "bindings" => array:1 [
      0 => 1
    ]
    "time" => 3.12
  ]
  1 => array:3 [
    "query" => "select * from `drop_offs` where `drop_offs`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.38
  ]
  2 => array:3 [
    "query" => "select * from `collected_scraps` where 0 = 1 and `collected_scraps`.`deleted_at` is null"
    "bindings" => []
    "time" => 1.53
  ]
]

Adding a with('collector')->get() to it like so: DropOff::find(1)->collected_scrap()->with('collector')->get() though executes

array:2 [
  0 => array:3 [
    "query" => "select * from `drop_offs` where `drop_offs`.`id` = ? and `drop_offs`.`deleted_at` is null limit 1"
    "bindings" => array:1 [
      0 => 1
    ]
    "time" => 5.0
  ]
  1 => array:3 [
    "query" => "select * from `collected_scraps` where 0 = 1 and `collected_scraps`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.31
  ]
]

What's the result of dd(DropOff::find($id)->getAttributes(), DropOff::find($id)->collection_ids);?

So, my implementation for that model has changed, and using the exact model for the test will require me to roll back multiple changes.

I have instead used another model with the same implementation of an array of IDs returning an empty array;

array:12 [
  "id" => 7
  "age" => "23 - 27"
  "sex" => "male"
  "collection_coverage_zone" => "kwara"
  "coverage_zone_coordinates" => "[1,5,7]"
  "location_ids" => null
  "approved_as_collector" => 1
  "current_location" => "{"lat":6.64462099999999988853005561395548284053802490234375,"lng":3.363557099999999966399855111376382410526275634765625}"
  "assigned_agent" => "PISJ98"
  "created_at" => "2020-08-14 15:27:49"
  "updated_at" => "2021-06-02 12:55:42"
  "deleted_at" => null
]
array:3 [
  0 => 1
  1 => 5
  2 => 7
]

Here, the coverage_zone_coordinates is the field containing the array of IDs.

If you still require the exact model for running the queries, I can also do that.

Is coverage_zone_coordinates or collection_ids the actual column name?

In my DropOff model, it was collection_ids. But like I explained, my DropOff model no longer implements the JSON relationship so I'm currently using another model, (Collector) implementing the same type of relationship (and also returning an empty array) to run the queries. And on this Collector model, the column name is coverage_zone_coordinates.

How is the new relationship defined? What does the new related model CoverageZoneCoordinate (?) look like?

The relationship is defined like so:

public function coverage_zone_coordinates()
{
   return $this->belongsToJson(MajorArea::class, 'coverage_zone_coordinates->location_ids');
}

'coverage_zone_coordinates->location_ids'

This doesn't match the output of getAttributes() from above:

"coverage_zone_coordinates" => "[1,5,7]"

So you mean the coverage_zone_coordinates->location_ids in the relationship should be defined differently for the array in this coverage_zone_coordinates" => "[1,5,7] output?

In your data, there is no location_ids key, but coverage_zone_coordinates itself contains the related IDs.

I only see location_ids as a completely separate attribute.

With this data structure, the relationship needs to be:

public function coverage_zone_coordinates()
{
   return $this->belongsToJson(MajorArea::class, 'coverage_zone_coordinates');
}

Oh, that was the problem. It works fine now. Thanks for the assistance.