0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

「laravel」レスポンスで返したくないカラムを設定する $hidden

Last updated at Posted at 2022-07-20

jsonなどでレスポンスを返す際、
テーブルの値が入ったオブジェクトを指定したら表示したくないカラムのデータがあるって場面がたまにあります。
そういうときはモデルに$hiddenを指定するだけで解決します

class User extends Model
{
    protected $table = 'user';
    
    protected $hidden = [
        'id',
        'user_id',
        'created_at',
        'updated_at'
    ];
} 
//$hidden設定前のレスポンス
 "user": {
        "id": 4,
        "user_id": 8,
        "login_at": "2022-07-20 11:32:33",
        "created_at": "2022-07-14 17:50:03",
        "updated_at": "2022-07-20 11:32:33"
    },

//$hiddnen設定後のレスポンス
   "user": {
        "login_at": "2022-07-20 11:32:55"
    }

laravelはコードが単純で機能が実装できるので初心者に優しいですね

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?