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?

laravelインストール基礎

0
Last updated at Posted at 2026-02-17

laravelの基本チートシート(初心者用)

前提条件
・MacBook
・mamp
・環境構築完了済み

記載内容

今回はlaravelのプロジェクトの作成方法をまとめた記事になります
正直mampとxampあまり変わらない

プロジェクト作成

laravelのインストール

composer create-project laravel/laravel プロジェクト名

npm インストール

npm install

mampをスタートさせる

image.png

サーバー起動

<!-- 別々のコマンドを開いて実行
実行場所はlaravelのプロジェクトフォルダー-->
1
php artisan serve
2
npm run dev

DB接続

.enb
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=members
DB_USERNAME=root
DB_PASSWORD=root
↓xamppの場合は必要ない
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

enbファイルを書き換え後に

.md
php artisan migrate

認証機能をつける場合

Breezeのインストール

composer require laravel/breeze --dev
php artisan breeze:install

①まず「どのスタックをインストールしますか?」と質問が表示されます。Livewire等を使う予定が特になければ、一番上を選択します。

②「ダークモードサポートをインストールしますか?」と質問が表示されます。ダークモード(黒っぽい画面)を使用したい場合には、yesを選びます。そうでなければ、noを選びます。ここでは、「no」を選択します。

③テストフレームワークを選択します。ご希望に応じて選んでください。ここでは「1」(PHPUnit) を選択します。

最後にphp artisan migrateでマイグレートを実行します。

認証機能をつけない場合

認証機能をつけない場合はこれで終わりなのですが個人的にbladeのビューのテンプレート化したコピペ用のコードを記載

app.blade.php

blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'Laravel') }}</title>
    <!-- Fonts -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">
    <!-- Styles -->
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>

<body>
    <header>
        @include('layouts.header')
    </header>

    <main>
        @yield('content')
    </main>
</body>
	
</html>

header.blade.php

blade.php
<div>
    header
</div>

home.blade.php

balde.php
@extends('layouts.app')
@section('content')

ここにメインを書く

@endsection

コントローラーの作成

php artisan make:controller コントローラー名 --resource

基本的なCRUDのためのメソッド名

public function index()
public function create()
public function store(Request $request)
public function show(Post $post)
public function edit(Post $post)
public function update(Request $request, Post $post)
public function destroy(Post $post)

モデル(DB)の作成

php artisan make:model モデル名 -m

データ型と説明

データ型 説明(公式サイトより) 使用時
integer INTEGERカラム 整数
string VARCHARカラム 名前など短めの文字列
text TEXTカラム コメントなどの文字列
longText LONGTEXTカラム かなり長い文字列
unsignedBigInteger 符号なしBIGINTカラム 他のテーブルのID
foreignId 符号なしBIGINTカラム(外部キー用) 他のテーブルのID(リレーション用)
boolean BOOLEANカラム true / false

マイグレーションファイルの書き方の例

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body');
        $table->text('image')->nullable();
        $table->timestamps();
    });
}

マイグレーションファイルを書き換えたらコマンド実行

php artisan migrate

ルートの書き方(web.php)

冒頭の宣言
use App\Http\Controllers\使用するコントローラー名;

基本的な書き方
Route::HTTPメソッド('URL', [コントローラー::class, 'メソッド'])->name('ルート名');

リソースコントローラー
Route::resource('post', PostController::class);

全て書くとこんな感じ

Route::get('post', [PostController::class, 'index'])->name('post.index');
Route::get('post/create', [PostController::class, 'create'])->name('post.create');
Route::post('post', [PostController::class, 'store'])->name('post.store');
Route::get('post/{post}', [PostController::class, 'show'])->name('post.show');
Route::get('post/{post}/edit', [PostController::class, 'edit'])->name('post.edit');
Route::patch('post/{post}', [PostController::class, 'update'])->name('post.update');
Route::delete('post/{post}', [PostController::class, 'destroy'])->name('post.destroy');

httpメソッド一覧

HTTPメソッド 説明 使用時
get ページを表示 一覧表示・詳細表示など
post データを保存 新規登録・送信フォーム
put / patch データの更新 編集処理
delete データの削除 削除処理

【リソースコントローラーのURLとルート名一覧】

メソッド URL ルート名 HTTPメソッド
index /post post.index GET
create /post/create post.create GET
store /post post.store POST
show /post/{post} post.show GET
edit /post/{post}/edit post.edit GET
update /post/{post} post.update PUT / PATCH
destroy /post/{post} post.destroy DELETE
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?