お力添えいただけると幸いです。
前提・実現したいこと
ビューファイルで、モデルに定義したgetAttributeメソッド(getSeparateCommaPointAddSignAmountAttribute)を実行させたい。
発生している問題・エラーメッセージ
モデルに定義しあgetAttributeメソッドが実行されません。
Laravelのエラーログ(storage/logs/laravel.log)を確認しても、何も出力されておりませんでした。
【モデル】
/app/Models/Payment.php
【コントローラー】
/app/Http/Controllers/StudentController.php
【ビューファイル】
/resources/views/students/trade.blade.php
該当のソースコード
まずは、該当箇所だけ記述させていただきます。
■ モデル(getAttributeメソッド)
Model
1/app/Models/Payment.php 2 3/** 4 * 決済金額を符号付きカンマ区切りにフォーマット 5 * 6 * @return string 7 */ 8public function getSeparateCommaPointAddSignAmountAttribute($value) 9{ 10 $sign = $this->add_sign_amount >= 0 ? '+' : '-'; 11 return $sign . '¥' . number_format(abs($this->add_sign_amount)); 12}
■ ビュー
blade
1/resources/views/students/trade.blade.php 2 3<div class="l-list--deal--detail pc-only"> 4 <table> 5 <thead> 6 <tr> 7 <td>日付</td> 8 <td>内容</td> 9 <td>対象レッスン</td> 10 <td>入金</td> 11 <td>出金</td> 12 <td>残高</td> 13 </tr> 14 </thead> 15 @foreach ($trade_details as $trade_detail) 16 <tr> 17 <td>{{ $trade_detail->formated_ymd_created_at }}</td> 18 <td>購入</td> 19 <td>「{{ $trade_detail->lessons_title }}」</td> 20 <td class="u-textAlign__right"></td> 21 <td class="u-textAlign__right">{{ $trade_detail->separate_comma_point_add_sign_amount }}</td> 22 <td class="u-textAlign__right">{{ $trade_detail->separate_comma_point_amount }}</td> 23 </tr> 24 @endforeach 25 </table> 26</div> 27 28{{ $trade_details }}の中身 29 30{"7":{"amount":"19000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":21000,"add_sign_amount":"19000"},"6":{"amount":"16000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":5000,"add_sign_amount":"16000"},"5":{"amount":"13000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":-8000,"add_sign_amount":"13000"},"4":{"amount":"10000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":-18000,"add_sign_amount":"10000"},"3":{"amount":"7000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":-25000,"add_sign_amount":"7000"},"2":{"amount":"4000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":-29000,"add_sign_amount":"4000"},"1":{"amount":"1000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":null,"point_amount":-30000,"add_sign_amount":"1000"},"0":{"amount":"30000","created_at":"2021-02-02T10:30:28.000000Z","lessons_title":"\u51fa\u91d1","point_amount":0,"add_sign_amount":-30000}}
試したこと
1.ビュー(bladeファイル)からモデル(getAttributeメソッド)に渡す値は合っているか
→ $trade_detail->add_sign_amountの値はあります。
2.モデルのgetAttributeメソッドの設定は合っているか
→ $this->add_sign_amountで正しく設定されています。
3.エラーログ(storage/logs/laravel.log)は出力されているか
→ なし
補足情報(FW/ツールのバージョンなど)
該当箇所以外の、関係ありそうなソースコードを記述させていただきます。
Model
1/app/Models/Payment.php 2 3 4<?php 5 6namespace App\Models; 7 8use Illuminate\Database\Eloquent\Factories\HasFactory; 9use Illuminate\Database\Eloquent\Model; 10use Illuminate\Database\Eloquent\SoftDeletes; 11use Illuminate\Support\Facades\Auth; 12use Illuminate\Support\Facades\DB; 13use App\Models\Withdrawal; 14 15~~~省略~~~ 16 17/** 18 * 取引詳細を取得 19 * 20 * @return \Illuminate\Database\Eloquent\Builder|Model|object|null 21 */ 22public function getDetail() 23{ 24 getFullNameAttribute($value) 25 26 $user_id = Auth::user()->id; 27 // 受け取り履歴一覧(キャンセルされたもの以外) 28 $payments = self::query() 29 ->select([ 30 'payments.amount AS amount', 31 'payments.created_at AS created_at', 32 'lessons.title AS lessons_title', 33 ]) 34 ->where([ 35 ['user_teacher_id', '=', $user_id], 36 ['payments.status', '=', 0], 37 ]) 38 ->leftJoin('applications', 'payments.application_id', '=', 'applications.id') 39 ->leftJoin('lessons', 'applications.lesson_id', '=', 'lessons.id'); 40 41 $details = Withdrawal::query() 42 ->select([ 43 'withdrawals.amount AS amount', 44 'withdrawals.created_at AS created_at', 45 DB::raw("'出金' AS lessons_title"), 46 ]) 47 ->where('withdrawals.user_id', '=', 1) 48 // 受け取り履歴一覧と出金リクエスト一覧を結合させる 49 ->unionAll($payments) 50 ->orderBy('created_at', 'desc') 51 ->get(); 52 53 $wk_point_amount = 0; 54 foreach ($details as $detail) { 55 56 // 取引時点の保有金額情報を付加 57 $detail->point_amount = $wk_point_amount; 58 $detail->add_sign_amount = $detail->lessons_title == '出金' ? $detail->amount * -1 : $detail->amount; 59 $wk_point_amount += $detail->add_sign_amount; 60 } 61 return $details->reverse(); 62}
controller
1/app/Http/Controllers/StudentController.php 2 3 4<?php 5 6namespace App\Http\Controllers; 7 8use App\Http\Requests\Message\SendRequest as MessageSendRequest; 9use App\Http\Requests\User\UpdateRequest as UserUpdateRequest; 10use App\Http\Requests\User\PasswordUpdateRequest as UserPasswordUpdateRequest; 11use App\Models\Payment; 12use App\Models\Withdrawal; 13use Illuminate\Http\Request; 14use Illuminate\Support\Facades\Auth; 15use Illuminate\Support\Facades\DB; 16use Illuminate\Support\Facades\Hash; 17use Jenssegers\Agent\Agent; 18 19class StudentController extends Controller 20{ 21 private $payment; 22 private $withdrawal; 23 24 public function __construct( 25 Payment $payment, 26 withdrawal $withdrawal 27 ) 28 { 29 $this->payment = $payment; 30 $this->withdrawal = $withdrawal; 31 } 32 33~~~省略~~~ 34 35/** 36 * 入出金履歴ページ 37 * 38 * @param Request $request 39 * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View 40 */ 41public function trade(Request $request) 42{ 43 $holding_amount = $this->payment->getHoldingAmount(); 44 $trade_months = $this->payment->getMonths(); 45 $trade_details = $this->payment->getDetail(); 46 $user_status = Auth::user()->status; 47 return view('students.trade', compact('holding_amount', 'trade_months', 'trade_details', 'user_status')); 48}
MySQL
1payments テーブル 2+----+----------------+-----------------+-----------------+--------+--------+---------------------+---------------------+------------+ 3| id | application_id | user_teacher_id | user_student_id | amount | status | created_at | updated_at | deleted_at | 4+----+----------------+-----------------+-----------------+--------+--------+---------------------+---------------------+------------+ 5| 1 | NULL | 1 | 1 | 1000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 6| 2 | NULL | 2 | 2 | 2000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 7| 3 | NULL | 0 | 3 | 3000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 8| 4 | NULL | 1 | 4 | 4000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 9| 5 | NULL | 2 | 5 | 5000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 10| 6 | NULL | 0 | 6 | 6000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 11| 7 | NULL | 1 | 0 | 7000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 12| 8 | NULL | 2 | 1 | 8000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 13| 9 | NULL | 0 | 2 | 9000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 14| 10 | NULL | 1 | 3 | 10000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 15| 11 | NULL | 2 | 4 | 11000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 16| 12 | NULL | 0 | 5 | 12000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 17| 13 | NULL | 1 | 6 | 13000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 18| 14 | NULL | 2 | 0 | 14000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 19| 15 | NULL | 0 | 1 | 15000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 20| 16 | NULL | 1 | 2 | 16000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 21| 17 | NULL | 2 | 3 | 17000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 22| 18 | NULL | 0 | 4 | 18000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 23| 19 | NULL | 1 | 5 | 19000 | 0 | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 24+----+----------------+-----------------+-----------------+--------+--------+---------------------+---------------------+------------+ 25 26withdrawals テーブル 27+----+---------+-----------+---------+--------+----------+---------------------+---------------------+------------+ 28| id | user_id | bank_type | bank_id | amount | verified | created_at | updated_at | deleted_at | 29+----+---------+-----------+---------+--------+----------+---------------------+---------------------+------------+ 30| 1 | 1 | 0 | 1 | 30000 | NULL | 2021-02-02 19:30:28 | 2021-02-02 19:30:28 | NULL | 31+----+---------+-----------+---------+--------+----------+---------------------+---------------------+------------+
Laravel Framework 8.25.0
mysql Ver 8.0.22
回答2件
あなたの回答
tips
プレビュー