Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F120822894
ObjectRelationListCommand.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
ObjectRelationListCommand.php
View Options
<?php
namespace
App\Console
;
use
Illuminate\Support\Str
;
/**
* This abstract class provides a means to treat objects in our model using CRUD, with the exception that
* this particular abstract class lists objects' relations.
*/
abstract
class
ObjectRelationListCommand
extends
ObjectCommand
{
/**
* The "relation" -- a method or property.
*
* @var string
*/
protected
$objectRelation
;
/**
* Optional arguments for $objectRelation method
*
* @var array
*/
protected
$objectRelationArgs
=
[];
/**
* Supplement the base command constructor with a derived or generated signature and
* description.
*
* @return mixed
*/
public
function
__construct
()
{
$this
->
description
=
"List {$this->objectRelation} for a {$this->objectName}"
;
$this
->
signature
=
sprintf
(
"%s%s:%s {%s}"
,
$this
->
commandPrefix
?
$this
->
commandPrefix
.
":"
:
""
,
$this
->
objectName
,
Str
::
kebab
(
$this
->
objectRelation
),
$this
->
objectName
);
$this
->
signature
.=
" {--attr=* : Attributes other than the primary unique key to include}"
;
parent
::
__construct
();
}
/**
* Execute the console command.
*
* @return mixed
*/
public
function
handle
()
{
$argument
=
$this
->
argument
(
$this
->
objectName
);
$object
=
$this
->
getObject
(
$this
->
objectClass
,
$argument
,
$this
->
objectTitle
);
if
(!
$object
)
{
$this
->
error
(
"No such {$this->objectName} {$argument}"
);
return
1
;
}
if
(
method_exists
(
$object
,
$this
->
objectRelation
))
{
$result
=
call_user_func_array
([
$object
,
$this
->
objectRelation
],
$this
->
objectRelationArgs
);
}
elseif
(
property_exists
(
$object
,
$this
->
objectRelation
))
{
$result
=
$object
->{
"{$this->objectRelation}"
};
}
else
{
$this
->
error
(
"No such relation {$this->objectRelation}"
);
return
1
;
}
// Convert query builder into a collection
if
(
(
$result
instanceof
\Illuminate\Database\Eloquent\Relations\Relation
)
||
(
$result
instanceof
\Illuminate\Database\Eloquent\Builder
)
)
{
$result
=
$result
->
get
();
}
// Print the result
if
(
(
$result
instanceof
\Illuminate\Database\Eloquent\Collection
)
||
is_array
(
$result
)
)
{
foreach
(
$result
as
$entry
)
{
$this
->
info
(
$this
->
toString
(
$entry
));
}
}
else
{
$this
->
info
(
$this
->
toString
(
$result
));
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Apr 24, 9:57 AM (1 w, 19 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
fd/ec/296d221b85dd0260ef34f0356df1
Default Alt Text
ObjectRelationListCommand.php (2 KB)
Attached To
Mode
rK kolab
Attached
Detach File
Event Timeline