質問編集履歴
1
モデルのコードを修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,14 +8,27 @@
|
|
8
8
|
if (\Auth::User()->role == 'admin') {
|
9
9
|
$result = $this->master_price;
|
10
10
|
} else {
|
11
|
+
$data = $this->prices()->where('user_id', \Auth::User()->id)->first();
|
11
|
-
$result = $this->
|
12
|
+
$result = (is_null($data) ? $this->master_price : $data->price);
|
12
13
|
}
|
13
14
|
return $result;
|
14
15
|
}
|
16
|
+
|
17
|
+
public function prices()
|
18
|
+
{
|
19
|
+
return $this->hasMany(Price::class);
|
20
|
+
}
|
15
21
|
}
|
16
22
|
```
|
17
23
|
|
18
24
|
Controller内で、このモデルクラスのmyPrice()を元に「orderBy」をかけたく思います。
|
25
|
+
※追記ここから
|
26
|
+
サンプルにする為コードを単純化していたのですが、単純にしすぎていました。
|
27
|
+
実際は上記のようなリレーションを挟んでの処理になります。
|
28
|
+
「該当のリレーションデータが存在しない場合は自クラスのデータを使う」という処理が入ります。
|
29
|
+
(自クラスのデータは絶対に入っています)
|
30
|
+
後出し仕様になってしまって申し訳ありません。
|
31
|
+
※追記ここまで
|
19
32
|
|
20
33
|
`$itemList = Item::where('aaa', 'bbb')->orderBy('updated_by', 'desc')->paginate(20);`
|
21
34
|
とりあえず上記コードが動くことは確認しました。
|