LoginSignup
5
4

More than 5 years have passed since last update.

Laravelにおける小技メモ

Last updated at Posted at 2016-11-25

storage/log/laravel.logに出力

$test = ["val", "2"];
\Log::debug('log test', ['key1' => 'val1', 'key2' => $test]);

言語ファイルの値を取得

以下のような言語ファイルがあるとする。
resources/lang/ja/validation.php

<?php
    return [
        'attributes' => [
            'book_name' => 'laravelの本',
        ],
        'answer' => 'この本の名前は, :book_name',
    ];

例)コントローラからattributesの値を取得する場合。(config/app.phpのlocaleをjaに設定)
testController.php

echo trans('validation.attributes.book_name');

Bladeから取得方法

test.blade.php

{{ trans('validation.attributes.book_name') }}

プレースフォルダーを利用してanswerメッセージを表示。

echo trans('validation.answer', ['book_name' => 'phpの本']);

テーブルに存在しないカラムをプロパティとしてシリアライズ時に追加する (Eloquent)

$appendsプロパティとアクアセサ機能を組み合わせる。

例)published_date(data型)の月の値をもつ、published_monthを追加する。

class book extends Model
{
    protected $appends = ['published_month'];

    public function getPublishedMonthAttribute()
    {
        return Carbon::parse($this->attributes['published_date'])->month;
    }
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4