snipe-it/app/Models/CheckoutRequest.php
Daniel Meltzer 61543f3a04 Add presenters for models. (#3098)
* Add presenters for models.  Move bootstrap table JSON generation to these presenters, which cleans up controllers a lot.  Move view specific modifications from the models to the presenters as well.

* Fix some issues found by travis and codacy

* Fix a few more issues found while testing.

* Attempt another acceptance test fix

* Try something else

* Maybe..
2016-12-23 17:52:00 -08:00

60 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CheckoutRequest extends Model
{
//
protected $fillable = ['user_id'];
protected $table = 'checkout_requests';
public function user()
{
return $this->belongsTo(User::class);
}
public function requestingUser()
{
return $this->user()->first();
}
public function requestedItem()
{
return $this->morphTo('requestable');
}
public function itemRequested() // Workaround for laravel polymorphic issue that's not being solved :(
{
return $this->requestedItem()->first();
}
public function itemType()
{
return snake_case(class_basename($this->requestable_type));
}
public function location()
{
if ($this->itemType() == "asset") {
$asset = $this->itemRequested();
if ($asset->assigneduser && $asset->assetloc) {
return $asset->assetloc;
} elseif ($asset->defaultLoc) {
return $asset->defaultLoc;
}
}
return $this->itemRequested()->location;
}
public function name()
{
if ($this->itemType() == "asset") {
return $this->itemRequested()->present()->name();
}
return $this->itemRequested()->name;
}
}