-
Hi, so I've runned into a small issue here. Let's say that I want to return data from database, that are owned by a specyfic user. How do I do it? class Keys extends Model
{
use AsSource;
/**
* @var array
*/
protected $fillable = [
'key',
'owner_id',
];
} class UserKeyScreen extends Screen
{
/**
* Display header name.
*
* @var string
*/
public $name = 'Keys';
/**
* Display header description.
*
* @var string
*/
public $description = 'Displays all your avaible keys';
/**
* Query data.
*
* @return array
*/
public function query(): array
{
return array(
'keys' => Keys::paginate()
);
}
/**
* Button commands.
*
* @return \Orchid\Screen\Action[]
*/
public function commandBar(): array
{
return [
];
}
/**
* Views.
*
* @return string[]|\Orchid\Screen\Layout[]
*/
public function layout(): array
{
return [
UserKeysListLayout::class
];
}
} class UserKeysListLayout extends Table
{
/**
* @var string
*/
public $target = 'keys';
/**
* @return TD[]
*/
public function columns(): array
{
return [
TD::make('key', 'Key'),
TD::make('owner', 'Owned By')
->render(function (Keys $key){
$user = User::find($key->owner_id);
return $user->name;
}),
];
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hello. You need to establish a relationship between the models. After these steps, the final version will look something like this: /**
* Query data.
*
* @return array
*/
public function query(Request $request): array
{
return array(
'keys' => $request->user()->keys()->paginate()
);
} |
Beta Was this translation helpful? Give feedback.
-
Oh. I must have missed that somehow. Thank you! |
Beta Was this translation helpful? Give feedback.
-
Adding this to: Users Model public function keys()
{
return $this->hasMany(Keys::class, 'owner_id');
}
/**
* Query data.
*
* @return array
*/
public function query(Request $request): array
{
return array(
'keys' => $request->user()->keys()->paginate()
);
} solved the issue. |
Beta Was this translation helpful? Give feedback.
Hello. You need to establish a relationship between the models.
You can find more details here: https://laravel.com/docs/8.x/eloquent-relationships
After these steps, the final version will look something like this: