I have 2 tables in SQL Server.
Table Account
:
[id] [binary](13) NOT NULL, [PRIMARI KEY]
[email] [varchar](50) NOT NULL,
....
Table Status
:
[numG] [int NOT NULL,
[id] [binary](13) NOT NULL, [PRIMARI KEY]
[contractname] [varchar](50) NOT NULL,
....
Here is the relation in AccountModel
:
public function status(): BelongsTo
{
return $this->BelongsTo(StatusModel::class, 'id', 'id');
}
StatusModel
:
public function account(): HasOne
{
return $this->HasOne(AccountModel::class, 'id', 'id');
}
CheckController
:
public function checkStatus($id) {
$account= AccountModel::where('id', DB::raw("convert(binary(13), '$id')"))->with('status')->first();
dd($account)
}
This is returning null :
#attributes: array:21 [▼
"id" => "testUserx00x00x00x00x00x00x00x00x00x00"
"email" => "[email protected]"
]
#relations: array:1 [▼
"account" => null
]
I have tried to print using eloquent
dd($account->status->numG)
But it throws an error :
Attempt to read property “numG” on null
I want to search for data by id from table account and get numG from table status.
Something like this
$data = [
"id" => $account->id,
"email" => $account->email
"numG" => $account->status->numG
]
Any suggestions on how to handle binary data from SQL Server in Laravel?