鏈杩戞墦绠楁壘宸ヤ綔锛屾墍浠ヤ富瑕佷笂鎵嬩竴涓嬩竴鐐逛富娴佺殑妗嗘灦銆
#鍏堢敤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)锛屽嵆椹煎嘲鍙橀噺浼氳瀹屽叏杞崲涓鸿泧褰㈠彉閲忋
鍙戣〃鍥炲