hasOne Last updated: 2023-05-29

Let's say  you need to fetch a value in reverse with a table that has data such as addresses for the user and you want to fetch only one address, this is an example of that

php
php
php
php
php
php
php
php
php
php
php
php
hasOne()->make('the address', 'address',AddressResource::class)

Label: the address

method: address in current Model User

php
php
php
php
php
php
php
php
php
php
php
php
class User extends Authenticatable {
public function address() {
return $this->hasOne(Address::class);
}
}

reference resource is: App\Http\DashResource\AddressResource 

you see that output like belongsTo but you cannot add by hasOne 

 

in Started Version 1.0.62 you can provide a new Method viewColumns 

to view custom columns in datatable index and sub resource in show page 

working with belongsTo,hasOne functions  

php
php
php
php
php
php
php
php
php
php
php
php
//->viewColumns(['email','account_type','account_status']) OR
->viewColumns('email','account_type','account_status')

 

full  Example 

php
php
php
php
php
php
php
php
php
php
php
php
belongsTo()->make('User','user',Users::class)
//->viewColumns(['email','account_type','account_status']) OR
->viewColumns('email','account_type','account_status')
// you can add a custom label on specific column
// Example
->viewColumns(['email'=>'Email Adderss','account_type'=>__('Email Address'),'account_status'])