eloquent
์๋กํํธ
๋ผ๋ผ๋ฒจ์ด ์ ๊ณตํ๋ ORM๊ตฌํ์ฒด๋ก DB์ ๊ฐ์ฒด๊ฐ ๋งตํ๊ธฐ์
์ฟผ๋ฆฌ
>>> $post = App\Models\Post::get();
๋ชจ๋ธ ํด๋์ค๋ฅผ ์ด์ฉํด์ ์ฟผ๋ฆฌ๋น๋์ ๊ฐ์ด ๋ฉ์๋๋ก ์ฟผ๋ฆฌ๋ฅผ ์ํํ ์ ์๋๋ฐ ์ด๋ Modelํด๋์ค๋ฅผ ์์๋ฐ๊ธฐ ๋๋ฌธ์ ๋ฉ์๋๋ฅผ ์ด์ฉํ ์ ์๋ค.
save()
>>> $post = App\Models\Post::first();
>>> $post->title = 'foo';
>>> $post->save(); //error๋ฐ์
์์์ save()๋ฅผ ์คํํ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋๋ฐ, ์๋กํํธ๋ ๋ชจ๋ ํ
์ด๋ธ์ created_at
,updated_at
์ด ์๋ค๊ณ ๊ณผ์ ํ๊ณ ํ์ฌ์ ํ์์คํฌํ๊ฐ์ ํ ๋นํ๋๋ฐ ํด๋น ์ปฌ๋ผ์ด ์กด์ฌํ์ง ์์ ๋ฐ์ํ๋ ์๋ฌ์ด๋ค. ํด๋น ์ปฌ๋ผ์ ์ถ๊ฐํด์ฃผ๊ฑฐ๋ ํ์์คํฌํ ์๋์
๋ ฅ๊ธฐ๋ฅ์ ๋๋ ๋ฐฉ๋ฒ์ผ๋ก ํด๊ฒฐํ ์ ์๋ค.
public $timestamps = false;
์ ์ถ๊ฐํด ์๋์
๋ ฅ๊ธฐ๋ฅ์ ๋ ์ ์๋ค.
create()
save()๋ฉ์๋๋ ๋ชจ๋ธ์ ํ๋กํผํฐ ๊ฐ์ ํ๋์ฉ ํ ๋นํด์ฃผ์ด์ผ ํ๋๋ฐ ์ฌ๋ฌ๊ฐ์ ํ๋กํผํฐ๊ฐ์ ํ๋ฒ์ ํ ๋นํ๋ ๊ฒฝ์ฐ์๋ create()๋ฅผ ์ฌ์ฉํ๋ฉด ํธํ๊ฒ ์์ ํ ์ ์๋ค.
>>> App\Models\Post::create([
'email' => 'test@test.com',
'password' => 'bcrypt('1234')',
]);
create()๋ฉ์๋๋ฅผ ์ด์ฉํด ๋งค๊ฐ๋ณ์๋ก associate array
๋ฅผ ๋๊ฒจ์ฃผ์ด ๊ฐ์ ํ ๋นํด ์ค ์ ์๋ค.
bcrypt()
๋ 60๋ฐ์ดํธ์ง๋ฆฌ ๋จ๋ฐฉํฅ ํด์๋ฅผ ๋ง๋๋ ๋์ฐ๋ฏธ ํจ์๋ก ๋ณตํธํ๊ฐ ๋ถ๊ฐ๋ฅํ๊ธฐ๋๋ฌธ์ ๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ๊ฐ ๋ถ๊ฐ๋ฅํ๋ค.
์ ์ฝ๋๋ฅผ ์คํ์์ผ๋ณด๋ฉด MassAssignmentException
์ด ๋ฐ์ํ๋๋ฐ ์ด๋ ์๋กํํธ ์์ฒด์์ ํด์ปค์ ๋๋ ํ ๋น ๊ณต๊ฒฉ์ ๋ณดํธํ๊ธฐ ์ํ ์์ธ์ด๋ค. ์ด๋ฅผ ์ํด์๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋๋ฐ ํ๋๋ $fillable
ํ๋กํผํฐ๋ฅผ ์ด์ฉํ๋ ํ์ฉ๋ชฉ๋ก ๋ฐฉ์์ด๊ณ , ํ๋๋ $guarded
ํ๋กํผํฐ๋ฅผ ์ด์ฉํ ๊ธ์ง๋ชฉ๋ก ๋ฐฉ์์ด๋ค.
์ฐ๊ด๊ด๊ณ
1. ์ผ๋๋ค ๊ด๊ณ
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
๋ง์ด๊ทธ๋ ์ด์
์์ ๋ค(N)
๊ฐ ๋๋ ์ชฝ์ foreign()
๋ฉ์๋๋ฅผ ์ด์ฉํด์ ํ
์ด๋ธ๋ผ๋ฆฌ์ ์ธ๋ ํค ๊ด๊ณ๋ฅผ ์ฐ๊ฒฐ ํด์ฃผ๊ณ ๋ชจ๋ธ์์ ์๋์ ๊ฐ์ด ์ฐ๊ฒฐ๊ด๊ณ๋ฅผ ํํํด์ฃผ๋ฉด ๋๋ค.
//User
class User extends Authenticatable
{
use HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
public function posts(){
return $this->hasMany(Post::class);
}
}
//Post
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title',
'body',
];
public $timestamps = false;
public function user(){
return $this->belongsTo(User::class,'custom_user_id');
}
}
๊ฐ ๋ชจ๋ธ๋ด์์ ์ฐ๊ด๊ด๊ณ์ค์ ํ ๋ชจ๋ธ์ด๋ฆ์ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด hasMany(), belongsTo()๋ฉ์๋์ ๊ฐ์ด ์ ๊ทผ ํ ์ ์๊ฒ ์ ์ ํด์ค ์ ์๋๋ฐ ์ด๋ ๋ฉ์๋ ๋ช ์ผ๋ก ๊ด๋ก์ 1์ด ๋๋ ์ชฝ ๋ชจ๋ธ์ ์ ๊ทผํ๋ ๋ฉ์๋๋ ๋จ์, N์ด ๋๋ ๋ชจ๋ธ์ ์ ๊ทผํ๋ ๋ฉ์๋๋ ๋ณต์๋ฅผ ์ฌ์ฉํ๋ค. 1์ด๋๋์ชฝ์์ N์ ์ฐ๊ฒฐํ ๋ hasMany()๋ฅผ, N์ด ๋๋์ชฝ์์ 1์ ์ฐ๊ฒฐํ ๋ belongsTo()๋ฅผ ์ด์ฉํ๋ค.
์ธ๋ํค ์ด๋ฆ์ ๋ชจ๋ธ๋ช ์ ๊ฐ์ง๊ณ ์๋ฆฌํํธ๊ฐ ์ ์ถํ์ฌ ์ฐ๊ฒฐํ์ง๋ง ํน๋ณํ id๋ก ์ฐ๊ฒฐ์ ์ํ๋ค๋ฉด Post๋ชจ๋ธ์ user()๊ณผ ๊ฐ์ด ๋งค๊ฐ๋ณ์๋ก ์ธ๋ํค ์ด๋ฆ์ ์ง์ ํด์ค ์ ์๋ค.
๋ค๋๋ค ์ฐ๊ฒฐ
$ php artisan make:migration create_article_tag_table --create=article_tag
class CreateArticleTagTable extends Migration
{
public function up()
{
Schema::create('article_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('article_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('articles')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('article_tag');
}
}
N:N๊ด๊ณ๋ฅผ ์ฐ๊ฒฐํ๊ธฐ ์ํด์๋ ํ ์ด๋ธ ํ๊ฐ๋ฅผ ๋ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
//Post
class Post extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'title',
'body',
];
public function user(){
return $this->belongsTo(User::class,'custom_user_id');
}
public function tags(){
return $this->belongsToMany(Tag::class);
}
}
//tag
class Tag extends Authenticatable
{
use HasFactory, Notifiable;
protected $fillable = [
'name'
];
public function posts(){
return $this->belongsToMany(Post::class);
}
}
N:N์ ์์ชฝ ๋ค belongsToMany()๋ฅผ ์ด์ฉํ์ฌ ์ฐ๊ฒฐ์ ํด์ฃผ๋ฉด ๋๋๋ฐ ์ฒซ๋ฒ์งธ์์ ํ ์ด๋ธ์ ํ๊ฐ๋ ์์ฑํ๋ ์ด์ ๋ ์ด ํ ์ด๋ธ์ด N:N๊ด๊ณ๋ฅผ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ํผ๋ฒ ํ ์ด๋ธ์ด ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์กฐํ
all()
$flights = App\Models\Flight::all();
foreach ($flights as $flight) {
echo $flight->name;
}
all()๋ฉ์๋๋ฅผ ํตํด ํ ์ด๋ธ์ ์กฐํํ ์์์ง๋ง ์ด๋ ๋ชจ๋ ํ์ด ์กฐํ๋๊ธฐ ๋๋ฌธ์ ์ฟผ๋ฆฌ๋น๋์ ๋น์ทํ๊ฒ ์กฐ๊ฑด๋ค์ ์ถ๊ฐํ ์ ์๋ค.
์๋กํํธ ๋ชจ๋ธ์ ๋ชจ๋ ์ฟผ๋ฆฌ๋น๋์ด๊ธฐ ๋๋ฌธ์ ์ฟผ๋ฆฌ ๋น๋์ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์ด์ฉํ ์ ์๋ค.
$flights = App\Models\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
cursor()
$users = App\Models\User::cursor()->filter(function ($user) {
return $user->id > 500;
});
foreach ($users as $user) {
echo $user->id;
}
cursor๋ฉ์๋๋ ๋จํ๋์ ์ฟผ๋ฆฌ๋ฅผ ์คํ์ ํตํด ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌ ํ ์ ์์ด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ํฌ๊ฒ ์ค์ผ ์ ์๋ค. ์ด๋ ๋ฐํ๊ฐ์ผ๋ก LazyCollection
์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ Collection ๋ฉ์๋๋ค์ ์ด์ฉํ ์ ์๋ค.
์ฝ์
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
์ฟผ๋ฆฌ๋น๋์ insert๋ณด๋ค ๊ฐํธํ๊ฒ save()๋ฉ์๋๋ก ์ฝ์ ์ ์ํํ ์ ์๋ค. ์ด๋ ํ์์คํฌํ๋ ์๋์ผ๋ก ์ค์ ๋์ด ์๋์ผ๋ก ์ง์ ํ ํ์๊ฐ ์๋ค.
๋ํ, ์ด๋ฏธ ์กด์ฌํ๋ ํ์ด๋ผ๋ฉด update๋ฅผ ์ํํ๋ค.
์์ฑ ๋ณ๊ฒฝ ํ์ธ
isDirty() | isClean()
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false
$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true
$user->save();
$user->isDirty(); // false
$user->isClean(); // true
ํ์ฌ ๋ชจ๋ธ์ด db์ flush๊ฐ ๋์๋์ง ๊ฐ ์ปฌ๋ผ๋ณ๋ก ํ์ธ๊ธฐ ๊ฐ๋ฅํ๋ค.
wasChanged()
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->save();
$user->wasChanged(); // true
$user->wasChanged('title'); // true
$user->wasChanged('first_name'); // false
ํ์ฌ ์์ฒญ์ฃผ๊ธฐ๋ด์ ๋ชจ๋ธ์ ์์ฑ์ด ๋ณ๊ฒฝ๋์๋์ง ํ์ธํ๋ ๋ฉ์๋
getOriginal()
$user = User::find(1);
$user->name; // John
$user->email; // john@example.com
$user->name = "Jack";
$user->name; // Jack
$user->getOriginal('name'); // John
$user->getOriginal(); // Array of original attributes...
flush๋๊ธฐ ์ ์ ๋ชจ๋ธ์ ์์ ๋ ์ด์ ๊ฐ์ ํ์ธํ๋ ๋ฉ์๋
๋๋ ํ ๋น
๋ชจ๋ธ์ ๊ฐ ํ๋กํผํฐ๋ฅผ ๊ฐ๊ฐ ์ฑ์ save()๋ก ์ ์ฅํ๋ ๋ฐฉ๋ฒ ์ธ์ create()
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ํ๋ฒ์ ์ ์ฅ ํ ์๋ ์๋ค.
$flight = App\Models\Flight::create(['name' => 'Flight 10']);
ํ์ง๋ง, ์๋กํํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋๋ ํ ๋น์ผ๋ก๋ถํฐ ๋ณดํธํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ ์ ์์ด fillable
์์ฑ์ ์ปฌ๋ผ๋ค์ ์ถ๊ฐํด์ฃผ์ด์ผ ํ๋ค.
Last updated