質問編集履歴
1
モデルのコードを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,11 +18,23 @@
|
|
18
18
|
|
19
19
|
} else {
|
20
20
|
|
21
|
+
$data = $this->prices()->where('user_id', \Auth::User()->id)->first();
|
22
|
+
|
21
|
-
$result = $this->m
|
23
|
+
$result = (is_null($data) ? $this->master_price : $data->price);
|
22
24
|
|
23
25
|
}
|
24
26
|
|
25
27
|
return $result;
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
public function prices()
|
34
|
+
|
35
|
+
{
|
36
|
+
|
37
|
+
return $this->hasMany(Price::class);
|
26
38
|
|
27
39
|
}
|
28
40
|
|
@@ -33,6 +45,20 @@
|
|
33
45
|
|
34
46
|
|
35
47
|
Controller内で、このモデルクラスのmyPrice()を元に「orderBy」をかけたく思います。
|
48
|
+
|
49
|
+
※追記ここから
|
50
|
+
|
51
|
+
サンプルにする為コードを単純化していたのですが、単純にしすぎていました。
|
52
|
+
|
53
|
+
実際は上記のようなリレーションを挟んでの処理になります。
|
54
|
+
|
55
|
+
「該当のリレーションデータが存在しない場合は自クラスのデータを使う」という処理が入ります。
|
56
|
+
|
57
|
+
(自クラスのデータは絶対に入っています)
|
58
|
+
|
59
|
+
後出し仕様になってしまって申し訳ありません。
|
60
|
+
|
61
|
+
※追記ここまで
|
36
62
|
|
37
63
|
|
38
64
|
|