-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOwned.php
More file actions
50 lines (42 loc) · 1.14 KB
/
Owned.php
File metadata and controls
50 lines (42 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Codice\Support\Traits;
use Auth;
use Illuminate\Http\Exceptions\HttpResponseException;
trait Owned
{
/**
* Find owned model (limited to the current user).
*
* @param int $id Model ID
* @return static
*/
public static function findMine($id)
{
$model = self::mine()->find($id);
if (!$model) {
$modelLangFile = array_reverse(explode('\\', get_called_class()))[0];
throw new HttpResponseException(redirect()->route('index')->with([
'message' => trans("$modelLangFile.not-found"),
'message_type' => 'danger',
]));
}
return $model;
}
/**
* Query scope for getting owned models (limited to the current user).
*
* @param $query \Illuminate\Database\Query\Builder
* @return \Illuminate\Database\Query\Builder
*/
public function scopeMine($query)
{
return $query->where('user_id', '=', Auth::id());
}
/**
* Define relationship to the User owning the model.
*/
public function user()
{
return $this->belongsTo('Codice\User');
}
}