โง '๋ฉ๋ฉํ์ ๋ผ๋ผ๋ฒจ๋ก ์๋ก ๋ง๋ค๊ธฐ' ํ๋ก์ ํธ ๋ชฉ์ฐจ โง (๋ณด์๋ ค๋ฉด ์๋ ๋๋ณด๊ธฐ ๋ฅผ ๋๋ฌ์ฃผ์ธ์.)
0. ๋ผ๋ผ๋ฒจ(laravel) ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถํ๊ธฐ-๋ฐฉ๋ฒ2 (Windows & ๋์ปค(docker) & sail ํ์ฉ)
2. ๊ธฐ์กด ์์ค๋ฅผ ํ๋์ ๋ธ๋ ์ด๋๋ก
5. ๊ธฐ์กด PHP ๋ก์ง์ ์ปจํธ๋กค๋ฌ๋ก
6. ๋ธ๋ ์ด๋ ๋ฌธ๋ฒ์ผ๋ก ์์
7. ์ธ๋ถ ๋ธ๋ ์ด๋ ํ์ผ, component ํ์ฉ
8. (DB) Model ๋ฐ ๊ธฐํ ํ์ผ ์์ฑ
9. (DB) ๋๋ฏธ๋ฐ์ดํฐ๋ฅผ ํ๊ธ๋ก ๋ง๋๋ ๋ฐฉ๋ฒ
10. (DB) ๋ชจ๋ธ, DB ํ์ฉ ์ฐ์ต
11. (DB) ๊ธฐ์กด ๋ฐ์ดํฐ ๋ฐฐ์ด์ DB๋ก ๋ณํ
12. (DB) DB ๋ฐ ๋ชจ๋ธ์ ์ด์ฉํ์ฌ ๊ตฌํ
13. ๋ก๊ทธ์ธ ๊ตฌํ์ ์ํ ์คํํฐํคํธ Breeze ์ค์น ๋ฐ ๋ก๊ทธ์ธ ์์
14. ๋ก๊ทธ์ธ ์ฐฝ ๋ท๋ฐฐ๊ฒฝ๋ ๋์์์ด ๋ณด์ด๋๋ก ์์
15. ๊ด๋ฆฌ์ ํ๋ฉด์์ ์ ์ฒด ๊ฒ์๊ธ ๋ฆฌ์คํธ ํ๋ฒ์ ๋ณด๊ธฐ
16. ๊ด๋ฆฌ์ ํ๋ฉด์์ ๊ฒ์๊ธ ์์ ํ๊ธฐ
17. ๊ด๋ฆฌ์ ํ๋ฉด์์ ๊ฒ์๊ธ ์ญ์ ํ๊ธฐ
๋ชจ๋ธ์ ํตํ DB ํ์ฉ์ด ์ฒ์์ด๊ธฐ์ ๋ช๊ฐ์ง ์ฐ์ต์ ๊ธฐ๋ก์ผ๋ก ๋จ๊ฒจ๋ด ๋๋ค.
1. ์ค๋น
๊ฐ. routes/web.php ์ ์๋์ ๊ฐ์ด ์ถ๊ฐ ์ ๋ ฅํฉ๋๋ค.
use App\Http\Controllers\PostController;
Route::get('/view', [PostController::class, 'index']);
๋. app/Http/Controllers/PostController.php ์์ ์๋ 2๋ฒ์ ๋ด์ฉ์ ์ ๋ ฅ ํ dd(~~) ๋ก ํ์ธํ๋ฉด ๋ฉ๋๋ค.
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$post = Post::first();
dd($post->title);
}
}
๋ค. ๋ธ๋ผ์ฐ์ ์์์ ๊ฒฐ๊ณผ๋ http://127.0.0.1/view ์์ ํ์ธํ๋ฉด ๋ฉ๋๋ค.
2. ๋ชจ๋ธ์ ํตํ DB ํ์ฉ
๊ฐ. posts ํ ์ด๋ธ์ ์๋ ๋ด์ฉ ์กฐํ
Post::first(); // posts ํ
์ด๋ธ ์ฒซ๋ฒ์งธ ๋ ์ฝ๋ ๋ถ๋ฌ์ค๊ธฐ
Post::find(2); // posts ํ
์ด๋ธ ๋๋ฒ์งธ ๋ ์ฝ๋ ๋ถ๋ฌ์ค๊ธฐ
Post::all(); // posts ํ
์ด๋ธ ์ ์ฒด ๋ ์ฝ๋ ๋ถ๋ฌ์ค๊ธฐ
Post::count(); // posts ํ
์ด๋ธ ๋ ์ฝ๋ ๊ฐฏ์
Post::where('category_id', 1)->find(1); // posts ํ
์ด๋ธ์์ category_id๊ฐ 1์ธ ๊ฒ ์ค ์ฒซ๋ฒ์งธ ์กฐํ
๋. posts ํ ์ด๋ธ์ ๋ ์ฝ๋ ์ถ๊ฐ - ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ
$post = new Post;
$post->category_id = 1;
$post->created = '19701012';
$post->version = '1.1';
$post->slug = 'aaaslugaaa';
$post->title = '์ฒซ๋ฒ์งธ ๊ฒ์๋ฌผ';
$post->excerpt = '๊ทธ๋ค์ ํผ๊ฐ ํฉ๊ธ์๋๋ฅผ ๋์ด';
$post->link_blog = '';
$post->link_youtube = '';
$post->body = "~~~~~~~~~";
$post->save();
๋ค. posts ํ ์ด๋ธ์ ๋ ์ฝ๋ ์ถ๊ฐ - ๋๋ฒ์งธ ๋ฐฉ๋ฒ
Post::create(['category_id'=>'1','created'=>'19701012','version'=>'1.1','slug'=>'11aaas','title'=>'์ฒซ๋ฒ์งธ ๊ฒ์๋ฌผ','excerpt'=>'๊ทธ๋ค์ ํผ๊ฐ ํฉ๊ธ์๋๋ฅผ ๋์ด','link_blog'=>'','link_youtube'=>'','body'=>'~~~~~~~~~']);
์ '๋ค'์ฒ๋ผ ์ ์ฅ์ ํ๋ฉด ์๋์ ๊ฐ์ mass assignment ์ค๋ฅ๊ฐ ๋ฐ์ํจ.
Add [category_id] to fillable property to allow mass assignment on [App\Models\Post].
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ app/Http/Models/Post.php ๋ชจ๋ธ์ ์๋์ ๊ฐ์ ๋ฌธ๊ตฌ๋ค ์ค ํ๋๋ฅผ ๋ฃ์ด์ผ ํด๊ฒฐ๋จ. (์์)
protected $fillable = ['title', 'excerpt', 'body'];
or
protected $guarded = ['id'];
or
protected $guarded = [];
๋ณธ ์์ ์์๋ ๋ง์ง๋ง ๋ฌธ์ฅ์ ๋ฃ์ด์ค ๊ฒ์.
class Post extends Model
{
use HasFactory;
protected $guarded = [];
}
๋ผ. posts ํ ์ด๋ธ์ ๋ ์ฝ๋ ์์ ($post→save() ํ์ง ์์๋ ๋จ!)
$post = Post::first();
$post->update(['excerpt' => 'Changed']);
3. Post์ Category ๋ชจ๋ธ ๊ด๊ณ ์ฐ๊ฒฐํ๊ธฐ
๊ฐ. ์๋์ ๊ฐ์ด ์ ๋ ฅํ๋ฉด ์ฒซ๋ฒ์งธ ๊ฒ์๊ธ์ category_id ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ผ๋ ์ด๊ฒ์ ๋จ์ง ์ซ์ 1๋ง์ ์๋ฏธํจ.
public function index()
{
$post = Post::first();
dd($post->category_id);
}
๋. ์ด์ Post์ Category ๋ชจ๋ธ ๊ฐ ๊ด๊ณ๋ฅผ ์ค์ ํ๋ฉด Post ๋ชจ๋ธ์์ ์นดํ ๊ณ ๋ฆฌ ํ ์ด๋ธ์ ์ฐ๊ด ์ ๋ณด๋ฅผ ๋ฐ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
app/Http/Models/Post.php ๋ชจ๋ธ์ ์๋์ ๊ฐ์ด ์ ๋ ฅํ๋ค.
class Post extends Model
{
use HasFactory;
protected $guarded = [];
//์๋ ๋ด์ฉ ์๋ก ์ถ๊ฐ
public function category()
{
return $this->belongsTo(Category::class);
}
}
์ด์ ์๋์ ๊ฐ์ด ์ปจํธ๋กค๋ฌ์ ์ ๋ ฅํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ ๋ณด๋ฅผ ๋ฐ๋ก ์ป์ ์ ์๋ค.
๋ค. ๊ด๊ณ ์ค์ ๋ช ๋ น์ด
Category ์์๋ Post๋ค์ด ํฌํจ๋๋ ๊ตฌ์กฐ์ด๊ธฐ ๋๋ฌธ์
- Category ๋ hasOne ๋๋ hasMany ๊ฐ๋ฅ
- Post ๋ belongsTo ๋๋ belongsToMany ๊ฐ๋ฅ
๋ผ. ๋ณธ ์์ ์์์ ๊ฒฝ์ฐ
< app/Http/Models/Post.php ๋ชจ๋ธ >
class Post extends Model
{
use HasFactory;
protected $guarded = [];
public function category()
{
return $this->belongsTo(Category::class);
}
}
< app/Http/Models/Category.php ๋ชจ๋ธ >
class Category extends Model
{
use HasFactory;
protected $guarded = [];
public function posts()
{
return $this->hasMany(Post::class);
}
}
3. N+1 Query ๋ฌธ์ ๋ฅผ ์ ๊ฒฝ์จ์ผ ํจ => ์ ๋ ์ ๋ชจ๋ฆ.
N+1 Query ๋ฌธ์ ํด๊ฒฐ์ ์ํ, Eager ๋ก๋ฉ์ ์ํด
Post ๋ชจ๋ธ์ ์๋์ ๊ฐ์ ๋ด์ฉ์ ์ถ๊ฐํด์ค.
protected $with = ['category'];
< ์ต์ข app/Http/Models/Post.php ๋ชจ๋ธ >
class Post extends Model
{
use HasFactory;
protected $with = ['category'];
protected $guarded = [];
public function category()
{
return $this->belongsTo(Category::class);
}
}
๐บ๐บ ์์ ํ ๋ชจ๋ ์์ค๋ ์๋ ๋งํฌ์์ ํ์ธํ์ค ์ ์์ต๋๋ค. ๐บ๐บ
https://github.com/mmssem/munghome
GitHub - mmssem/munghome
Contribute to mmssem/munghome development by creating an account on GitHub.
github.com