laravel涓婃墜

鏈杩戞墦绠楁壘宸ヤ綔锛屾墍浠ヤ富瑕佷笂鎵嬩竴涓嬩竴鐐逛富娴佺殑妗嗘灦銆

#鍏堢敤composer瀹夎涓涓媗aravel
composer create-project laravel/laravel newlaravel
#瀹夎瀹屽熀鏈笂璁块棶public搴曚笅鐨刬ndex.php灏卞彲浠ヤ簡

mysql5.7浠ヤ笅鐨勪細鎶ラ敊

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique (email))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

淇敼涔熶笉闅撅紝鎵惧埌app\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

鐗规
1. migration 鏁版嵁搴撹縼绉 鐩稿綋浜庢暟鎹簱鐨勭増鏈帶鍒

composer make:migration create_articles_table
#杩愯鍚庝細鍦╠atabase/migrations搴曚笅鐢熸垚涓涓甫鏃堕棿鎴崇殑杩佺Щ鐨刾hp浠g爜锛岀劧鍚庢牴鎹嚜宸遍渶姹傚啀娣绘补鍔犻唻
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body')->nullable();
            $table->integer('user_id');
            $table->timestamps();
        });
    }
#鐒跺悗杩愯杩佺Щ
php artisan migrate
#鑻ヨ鍥炴粴鏈鍚庝竴娆¤縼绉伙紝鍙互浣跨敤 rollback 鍛戒护銆傛鍛戒护灏嗗洖婊氭渶鍚庝竴娆°岃縼绉汇嶏紝鍏朵腑鍙兘鍖呭惈澶氫釜杩佺Щ鏂囦欢锛
php artisan migrate:rollback
#鍒犻櫎鎵鏈夎〃 & 杩佺Щ
php artisan migrate:fresh
#婵娲绘埛鐧诲綍娉ㄥ唽鍔熻兘
php artisan make:auth

鐒跺悗閰嶇疆鏁版嵁搴擄紝鍦ㄦ牴鐩綍搴曚笅鐨.env

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5
DB_USERNAME=root
DB_PASSWORD=password

鐒跺悗鏄ā鍨

a. Eloquent 鏄粈涔
Eloquent 鏄 Laravel 鍐呯疆鐨 ORM 绯荤粺锛屾垜浠殑 Model 绫诲皢缁ф壙鑷 Eloquent 鎻愪緵鐨 Model 绫伙紝鐒跺悗锛屽氨澶╃敓鍏峰浜嗘暟鍗佷釜寮傚父寮哄ぇ鐨勫嚱鏁帮紝浠庢鎯冲共鍟ヤ簨鍎块兘鏄竴琛屼唬鐮佸氨鎼炲畾銆

缁忚繃浜嗕笁骞村鐨勫ぇ瑙勬ā浣跨敤锛屾垜鍙戠幇 Eloquent 鍙﹁緹韫婂緞閲囩敤鍜 Java 鎶鏈畬鍏ㄤ笉鍚岀殑鎬濊矾瑙e喅浜嗗浜哄紑鍙戣﹀悎杩囬噸鐨勯棶棰橈細鏁版嵁搴撶浉鍏虫搷浣滃叏閮ㄧ敤涓鍙ヨ瘽瑙e喅锛孧odel 涓笉鍐欏叡鐢ㄥ嚱鏁帮紝澶у箙闄嶄綆浜 bug 鍑犵巼銆備粈涔堬紵浣犺鎬ц兘锛熻蒋浠跺伐绋嬬爺绌剁殑瀵硅薄鏄汉锛屾ц兘鐨勪紭鍏堢骇姣斾唬鐮佹牸寮忚鑼冮兘浣庡ソ鍢涖傛垜鏃跺埢璋ㄨ锛氫唬鐮佹槸鍐欑粰浜虹湅鐨勶紝鍙槸鎭板ソ鑳借繍琛屻

php artisan make:model Article
鍛戒护琛岀敓鎴愭ā鍨

Seeder 鏄垜浠帴瑙﹀埌鐨勪竴涓柊姒傚康锛屽瓧闈㈡剰鎬濅负鎾鏈恒係eeder 瑙e喅鐨勬槸鎴戜滑鍦ㄥ紑鍙 web 搴旂敤鐨勬椂鍊欙紝闇瑕佹墜鍔ㄥ悜鏁版嵁搴撲腑濉叆鍋囨暟鎹殑绻佺悙浣庢晥闂銆

php artisan make:seeder ArticleSeeder

鐒跺悗淇敼database/seeds閲岄潰鏂扮敓鎴愮殑鏂囦欢ArticleSeeder.php锛屼慨鏀箁un鏂规硶

public function run()
{
    DB::table('articles')->delete();

    for ($i=0; $i < 10; $i++) {
        \App\Article::create([
            'title'   => 'Title '.$i,
            'body'    => 'Body '.$i,
            'user_id' => 1,
        ]);
    }
}

鐒跺悗淇敼database/seeds/DatabaseSeeder.php鐨剅un鏂规硶

public function run()
{
    $this->call(ArticleSeeder::class);
}

鐒跺悗鎵цcomposer dump-autoload,php artisan db:seed,搴旇articles鏁版嵁琛ㄩ噷闈㈠氨澶氫簡10鏉℃暟鎹紝

鎺у埗鍣ˊ鏂规硶 璺敱 HomeController@index

鎺ヤ笅鏉ヨ鐢ㄨ矾鐢便丮VC銆丒loquent ORM 鍜 blade 瑙嗗浘绯荤粺鎼缓涓涓崥瀹㈢郴缁熴

blade 娴呮瀽
blade 寮曟搸浼氬瑙嗗浘鏂囦欢杩涜棰勫鐞嗭紝甯垜浠畝鍖栦竴浜涢噸澶嶆у緢楂樼殑 echo銆乫oreach 绛 PHP 浠g爜銆俠lade 杩樻彁渚涗簡涓涓伒娲诲己澶х殑瑙嗗浘缁勭粐绯荤粺銆傛墦寮 home.blade.php锛

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif

                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@extends(‘layouts.app’)
resources/views/layouts/app.blade.php 銆傝繖涓嚱鏁拌繕闅愬惈浜嗕竴涓皬鐭ヨ瘑锛氬湪浣跨敤鍚嶇О鏌ユ壘瑙嗗浘鐨勬椂鍊欙紝鍙互浣跨敤 . 鏉ヤ唬鏇 / 鎴 \銆
@Section(‘content’) … @endsection
杩欎袱涓爣璇嗙涔嬮棿鐨勪唬鐮侊紝浼氳鏀惧埌鍩鸿鍥剧殑 @yield(‘content’) 涓繘琛岃緭鍑恒

鏃㈢劧 Controller – View 鐨勬灦鏋勫凡缁忚繍琛岋紝涓嬩竴姝ュ氨鏄紩鍏 Model 浜嗐侺aravel 涓悜瑙嗗浘浼犳暟鎹潪甯哥畝鍗曪細

public function index()
{
    return view('home')->withArticles(\App\Article::all());
}

\App\Article::all() 鏄噰鐢ㄧ粷瀵瑰懡鍚嶇┖闂存柟寮忓 Article 绫荤殑璋冪敤銆
withArticles 鏄垜瀹氫箟鐨勬柟娉曪紝Laravel 骞朵笉鎻愪緵锛岃繖涔熸槸 Laravel 浼橀泤鐨勪竴涓〃鐜帮細Laravel View 閲囩敤 __call 鏉 handle 瀵规湭瀹氫箟 function 鐨勮皟鐢紝鍏朵綔鐢ㄥ緢绠鍗曪細缁欒鍥剧郴缁熸敞鍏ヤ竴涓悕涓 $articles 鐨勫彉閲忥紝杩欐浠g爜绛変环浜 ->with(‘articles’, \App\Article::all())銆
灞曞紑璁蹭竴涓嬶紝->withFooBar(100) 绛変环浜 ->with(‘foo_bar’, 100)锛屽嵆椹煎嘲鍙橀噺浼氳瀹屽叏杞崲涓鸿泧褰㈠彉閲忋

All posts

Other pages

鍙戣〃璇勮

鐢靛瓙閭欢鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞