Skip to content

Instantly share code, notes, and snippets.

@ogaty
Last active December 9, 2018 03:32
Show Gist options
  • Save ogaty/62d1828373e375fb3f09c89619f2d01d to your computer and use it in GitHub Desktop.
Save ogaty/62d1828373e375fb3f09c89619f2d01d to your computer and use it in GitHub Desktop.
laravel

紛らわしい2つのget

Laravelには2つのgetがある。
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.phpと
vendor/laravel/framework/src/Illuminate/Support/Collection.php

$p = Post::where('id', '<', 3)->get();

これはCollectionの配列になる idを取るにはpluck使うこと

$p = Post::where('id', '<', 3)->get(['id']);

これもCollectionの配列になる。

$q = collect(['id' => 1, 'name' => 'tom']);
$r = $q->get('id');

これはidを取得。1という数値になる。
配列の中身を取るのと変わらない。

つまりのところ、

Post::get(['id'])->get(0)->id

なんて書き方になるのだ。

紛らわしい2つのCollection

そしてLaravelには2つのCollectionが存在する。 object(Illuminate\Support\Collection)と
object(Illuminate\Database\Eloquent\Collection)の2つ。

前者はcollect()で作ったCollection。
後者はelloquentのget()で取ったCollection。
前者は$collect['name']で取れるけど後者は$collect->nameになるので注意。


select tag_id from post_tag where post_id = 1;

PostTag::where('post_id', $data->id)->pluck('tag_id')->toArray();

nullable、default

そのまんま
->default('')
->nullable()

change column

->changeをつける

やっちまった

reset叩くとさらにやっちまったことになる

php artisan migrate:rollback --step=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment