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, php覚書

Last updated at Posted at 2022-07-06

目的

忘れがちな関数や記法をストックしておく

where('id', $id)
//と
find($id);

findの方がシンプルに書ける

!is_null($a) ? $a : $b
//と
$a ?? $b

null合体演算子

$model->attach($id);

中間テーブルを作成

$model = Model::find($id)->update();
$model = Model::create($params);

作成/更新したデータを返すので、そのデータを再利用する時に使う。

$arr = ['44' => 0];
dd(empty($arr)); //false

$arr = ['44' => ''];
dd(empty($arr)); //false

$arr = ['44' => false];
dd(empty($arr)); //false

$arr = ['44' => null];
dd(empty($arr)); //false

キーが設定されていてバリューがfalsyな値の時、empty()するとfalseが返る

condition1 && condition2

!condition1 || !condition2 

&&の否定

$collection = User::with('posts')->select('title'); //titleはpostsのカラム

これだと取れない。生sqlのjoinとは異なり、eloquentでは結合したテーブルのカラムを取得できない。

$collection->pluck('key.value')

ドット区切りでpluckの引数を書くと、下の階層の要素を指定できる。

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?