Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcoplus's avatar

relationship between tables

I have two tables with a column that has the same data, they are unique and are as if it were an id column, I connected these two columns to have a table with the data from both tables, the problem is this the parent table contains 10,000 rows while the child table contains 2,000 but I only see the data from the child table and I do not see those from the parent table, that is, I only see 2,000 results and instead I want to see all 10,000 results and if there is associated data in the child table see them if there is not see the empty columns, the relationship I created is this

public function modelcode()
    {
        return $this->belongsTo(Model::class, 'code_id', 'codeId')->withDefault(['codeId' => '']);
    }

and

->addColumn('codeId',function ($query) {
                return $query->modelcode->codeId;
            })

Column::make('codeId')->data('modelcode.codeId),

the values ​​present in the parent table table1 are loaded, but only the total of the values ​​of the child table table2 does not load everything, where am I going wrong?

0 likes
2 replies
jayandholariya's avatar
Level 4

@marcoplus For correct data retrieval, you need change the modelcode relationship to use hasOne rather than belongsTo.

  1. When the current (parent) model owns a related record in the child model, hasOne is suitable.
  2. belongsTo is used when the relationship's child is the current model, which isn't the case in this instance.
    public function modelcode() { 
        return $this->hasOne(ChildModel::class, 'codeId', 'code_id'); 
    }

Eagerly Loading Data Fetching:

    $data = ParentModel::with('modelcode')->get(); 

This guarantees that all parent records are obtained and, if applicable, associated child data is loaded.

Setup of DataTable Column:

If there is no corresponding child record,

    ->addColumn('codeId', function ($query) {
        return optional($query->modelcode)->codeId ?? 'N/A'; // Returns 'N/A'.
    })
  • When the associated child record is null, problems are avoided by using optional().
  • 'N/A' will appear in the DataTable field if there are no child records linked to the parent.
1 like
marcoplus's avatar

@jayandholariya thanks for your reply, if I view the parent table I see all the values ​​and also the associated values ​​in the child table, but a column where I have uploaded attachments I receive empty values the code I'm using to see the attachments is this I tried to insert it too but it doesn't work it remains empty

->addColumn('file', function ($query) {
                if ($query->file== null) {
                    return 'NA';
                }
                $array = explode(',', $query->file);

                $string = '';
                foreach ($array as $key => $value) {
                    $value = str_replace('//', '/', $value);
                    $url = ("https://s3.amazonaws.com/file/{$value}");
                    $key++;
                    $string .= "<a href='{$url}' target='_blank'>File- {$key} - {$query->createdAt}</a><br>";
                }
                return $string;
            })

this return all null

and if

->addColumn('file', function ($query) {
                if ($query->modelcode?->file== null) {  <---- ? and now work all
                    return 'NA';
                }
                $array = explode(',', $query->modelcode->file);

                $string = '';
                foreach ($array as $key => $value) {
                    $value = str_replace('//', '/', $value);
                    $url = ("https://s3.amazonaws.com/file/{$value}");
                    $key++;
                    $string .= "<a href='{$url}' target='_blank'>File- {$key} - {$query->modelcode->createdAt}</a><br>";
                }
                return $string;
            })


solved I was just missing a ? and now everything works, thanks for your support

1 like

Please or to participate in this conversation.