質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

1337閲覧

MODELからレコードが取得できない

kotakeshi0923

総合スコア28

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

1クリップ

投稿2019/12/11 13:54

編集2019/12/11 15:06

以下のcontroller からMODELのfunctionを呼び出していますが、DBから取得できるレコードがNULLになってしまいます。
composer auto-load を実行しても改善されません。
model側で$this を確認すると、レコードがからのモデルのオブジェクトの内容は確認できます。
もちろん、DBにはレコードが存在します。
どこが悪いのかご助言をお願いできますでしょうか。

laravel v6.5
Mysql v8.0.18

<controller> ``` namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Middleware\VerifyCsrfToken;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;

use App\Model\RP;
use Carbon\Carbon;

class RPController extends Controller
{
function index(Request $request)
{
if(isset($request->display_items)){
$paginate = $request->display_items;
}
else {
$paginate = 20;
}
if(isset($request->page)){
$page = $request->page;
} else {
$page = 1;
}
$display_items = $paginate;

$page_params = array(); $page_params['size'] = 20; $page_params['direction'] = "ASC"; $rp_info = \App\Library\Pz::getRP($page_params); $rp_clients = new RP(); $rp_clients = $rp_clients->getRPClient($rp_info[0],'client_id'); $total_count = count($rp_info[0]); $start_record = ($page - 1) * $paginate + 1; $end_record = ($page * $paginate >= count($rp_info) ) ? count($rp_info) : $page * $paginate; $pagination_params = [ "display_items" => $display_items, ]; return view('rp/index', compact('rp_clients', 'start_record', 'end_record', 'total_count', 'display_items', 'pagination_params')); }
<model>

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use Carbon\Carbon;

class RP extends Model
{
use SoftDeletes;

protected $table = 'rp'; public $tableType = 'master'; protected $primaryKey = 'id'; protected $keyType = 'bigint'; public $incrementing = true; protected $dates = ['deleted_at']; protected $fillable = ['client_id', 'service_name', 'description', 'oidc_login_redirect_url', 'oidc_logout_redirect_url', 'oidc_client_secret', 'wifi_stadium_id', 'wifi_home_club_cd', 'enabled', 'display_pattern', 'display_header_footer', 'display_agreement_check', 'login_button_pattern', 'register_timestamp', 'register_principal', 'update_timestamp', 'update_principal']; // created_at, updated_at は DB の機能で自動更新する. public $timestamps = false; public function getRPClient($parentAry, $target){ foreach($parentAry as $childAry){ foreach($childAry as $key => $value){ if($key == $target){ $targetAry[] = ['client_id'=>$value,'description'=>$this->description,'enabled'=>$this->enabled]; } } } return $targetAry; }

}

<migration1>
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateRpTableForRds extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('rp', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('client_id'); $table->string('service_name'); $table->string('description')->nullable(); $table->string('oidc_login_redirect_url'); $table->string('oidc_logout_redirect_url'); $table->string('oidc_client_secret')->nullable(); $table->string('wifi_stadium_id')->nullable(); $table->string('wifi_home_club_cd')->nullable(); $table->boolean('enabled')->nullable(); $table->string('display_pattern')->nullable()->comment('"00": ミニマム、"01":標準、"02":フル'); $table->boolean('display_header_footer')->nullable()->comment('画面のヘッダ・フッタを表示/非表示'); $table->boolean('display_agreement_check')->nullable()->comment('規約同意チェックボックスの表示/非表示'); $table->string('login_button_pattern')->nullable()->comment('"00":標準、"01":メールアドレス'); $table->bigInteger('register_timestamp')->nullable(); // Unix Timestamp $table->string('register_principal')->nullable(); $table->bigInteger('update_timestamp')->nullable(); // Unix Timestamp $table->string('update_principal')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('rp'); } } ``` <migration2> ``` <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddSoftDeleteRpTableForRds extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('rp', function (Blueprint $table) { $table->softdeletes(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('rp', function (Blueprint $table) { $table->dropColumn('deleted_at'); }); } } ```

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/12/11 14:07

回答者が手元で現象を再現するために必要な情報を全て提示してください。
退会済みユーザー

退会済みユーザー

2019/12/11 14:42

migrationファイルも提示してください。
退会済みユーザー

退会済みユーザー

2019/12/11 15:14 編集

\App\Library\Pz::getRP() に相当するコードがないので検証できません。 今一度、ご自身の書いた質問内容で十分かどうかよく検証してください。
kotakeshi0923

2019/12/11 15:18

\App\Library\Pz::getRP() のデータは正常に取得できています。 その後の処理がnullになります。
退会済みユーザー

退会済みユーザー

2019/12/11 15:21 編集

こちらの手元で同じ状況を作らなきゃ回答できないから言っているんですけど、伝わりませんかね? 回答者はエスパーじゃありませんよ。
m.ts10806

2019/12/12 00:30

他人が再現できるコードを出さないのなら、自分で頑張ってデバッグしてくださいとしか…
kotakeshi0923

2019/12/12 03:07

公開できるか確認しますので、今しばらくお待ちください。
guest

回答1

0

自己解決

seedで、softdeleteの日付を入れているのが原因だったので、
softdeleteの日付を削除し、取得できることを確認しました。

投稿2020/01/03 14:43

kotakeshi0923

総合スコア28

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問