Laravel? ??? ??? ??? ?? ?? PHP ? ????????. Laravel? ???? ???? ???? ?? ORM(Object Relational Mapping) ??? ???? ???? ??? ??????? ?? ???? ??? ? ??? ????.
Laravel? ??? ? Laravel? ??????? ???? ??? ? ??? ?? ?????? ??? ???? ???. ????? Laravel?? ??????? ???? ??? ?????.
1. ?? ??
Laravel??? .env ??? ???? ?????? ??? ??? ? ????. .env ???? ?? ??? ?? ? ????.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
DB_CONNECTION
? ?????? ??? ???? ? ?????. Laravel?? ???? ?????? ???? mysql, pgsql, sqlite, sqlsrv ?? ????.DB_HOST
用于指定數(shù)據(jù)庫(kù)所在的主機(jī)名或IP地址,一般指定為localhost
或127.0.0.1
。DB_PORT
用于指定數(shù)據(jù)庫(kù)服務(wù)器的端口號(hào)。DB_DATABASE
用于指定要使用的數(shù)據(jù)庫(kù)名稱(chēng)。DB_USERNAME
用于指定連接數(shù)據(jù)庫(kù)使用的用戶(hù)名。DB_PASSWORD
用于指定連接數(shù)據(jù)庫(kù)使用的密碼。
DB_CONNECTION
用于指定數(shù)據(jù)庫(kù)的類(lèi)型,Laravel支持的數(shù)據(jù)庫(kù)類(lèi)型有mysql、pgsql、sqlite、sqlsrv等。在進(jìn)行完以上的設(shè)置之后,Laravel將會(huì)使用這些設(shè)置來(lái)連接我們的數(shù)據(jù)庫(kù)。
2. 數(shù)據(jù)庫(kù)遷移
Laravel提供了數(shù)據(jù)庫(kù)遷移的功能,可以方便我們?cè)诓煌臄?shù)據(jù)庫(kù)之間進(jìn)行數(shù)據(jù)遷移。在進(jìn)行數(shù)據(jù)庫(kù)遷移時(shí)需要注意的是,我們需要先創(chuàng)建數(shù)據(jù)庫(kù)并設(shè)置好相應(yīng)的連接信息,然后再利用遷移器進(jìn)行數(shù)據(jù)遷移。
在Laravel中,我們可以通過(guò)執(zhí)行php artisan make:migration create_users_table
命令來(lái)創(chuàng)建一個(gè)遷移文件。該命令將會(huì)在database/migrations
目錄中生成一個(gè)新的遷移文件,文件名類(lèi)似于2019_04_01_000001_create_users_table.php
。
在創(chuàng)建完遷移文件之后,我們需要打開(kāi)該文件并編輯其中的up
方法和down
方法。其中,up
方法將會(huì)在執(zhí)行遷移時(shí)被調(diào)用,用于定義我們需要執(zhí)行的數(shù)據(jù)庫(kù)操作;down
方法將會(huì)在撤銷(xiāo)遷移時(shí)被調(diào)用,用于定義我們需要執(zhí)行的撤銷(xiāo)操作。下面我們以創(chuàng)建用戶(hù)表為例,進(jìn)行代碼示范:
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }
以上代碼將會(huì)創(chuàng)建一個(gè)名為users
的表,該表包含5個(gè)字段id
、name
、email
、password
和remember_token
,以及兩個(gè)自動(dòng)維護(hù)的字段created_at
和updated_at
。
在完成了上述設(shè)置之后,我們可以執(zhí)行php artisan migrate
命令來(lái)執(zhí)行數(shù)據(jù)遷移操作。
3. 模型
在Laravel中,我們可以使用Eloquent ORM來(lái)方便地操作我們的數(shù)據(jù)庫(kù)。Eloquent ORM提供了許多方法用于執(zhí)行CRUD(create, read, update, delete)操作,能夠幫助我們快速進(jìn)行數(shù)據(jù)庫(kù)操作。
我們先來(lái)看一下在模型中設(shè)置數(shù)據(jù)庫(kù)的方法。在模型類(lèi)中,我們可以使用以下方法來(lái)指定表名、主鍵以及數(shù)據(jù)庫(kù)連接信息:
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; class User extends Model { protected $table = 'users'; protected $primaryKey = 'id'; protected $connection = 'mysql'; }
以上代碼將會(huì)指定使用mysql
連接來(lái)訪問(wèn)users
表,該表的主鍵為id
。
在設(shè)置完數(shù)據(jù)庫(kù)連接信息后,我們就可以使用Eloquent ORM來(lái)進(jìn)行數(shù)據(jù)庫(kù)操作了。下面我們來(lái)看一些Eloquent ORM的基本操作。
3.1 創(chuàng)建數(shù)據(jù)
在Eloquent ORM中,我們可以使用create
方法來(lái)創(chuàng)建數(shù)據(jù)。例如:
$user = User::create([ 'name' => 'Tom', 'email' => 'tom@example.com', 'password' => bcrypt('password'), ]);
以上代碼將會(huì)創(chuàng)建一條名為Tom
、郵箱為tom@example.com
、密碼為password
的用戶(hù)數(shù)據(jù)。
3.2 查詢(xún)數(shù)據(jù)
在Eloquent ORM中,我們可以使用get
方法來(lái)查詢(xún)數(shù)據(jù)。例如:
$users = User::get();
以上代碼將會(huì)從users
表中查詢(xún)出所有的用戶(hù)數(shù)據(jù)。
我們也可以使用where
方法來(lái)進(jìn)行條件查詢(xún)。例如:
$users = User::where('name', 'Tom')->get();
以上代碼將會(huì)從users
表中查詢(xún)出名稱(chēng)為Tom
的所有用戶(hù)數(shù)據(jù)。
3.3 更新數(shù)據(jù)
在Eloquent ORM中,我們可以使用update
方法來(lái)更新數(shù)據(jù)。例如:
$user = User::where('name', 'Tom')->first(); $user->email = 'new_email@example.com'; $user->save();
以上代碼將會(huì)將名為Tom
的用戶(hù)數(shù)據(jù)的郵箱改為new_email@example.com
。
3.4 刪除數(shù)據(jù)
在Eloquent ORM中,我們可以使用delete
方法來(lái)刪除數(shù)據(jù)。例如:
$user = User::where('name', 'Tom')->first(); $user->delete();
以上代碼將會(huì)刪除名為Tom
DB_HOST
? ??????? ?? ??? ???? IP ??? ???? ? ???? ????? localhost
?? 127.0.0.1
? ?????. >.
DB_PORT
? ?????? ??? ?? ??? ???? ? ?????. ??DB_DATABASE
? ??? ?????? ??? ???? ? ?????. ??DB_USERNAME
? ??????? ???? ? ???? ??? ??? ???? ? ?????. ??DB_PASSWORD
? ??????? ???? ? ???? ????? ???? ? ?????. ??? ??? ??? ? Laravel? ? ??? ???? ??????? ?????. ????2. ?????? ??????????Laravel? ?? ?? ?????? ?? ???? ?? ??????? ? ?? ?????? ?????? ??? ?????. ?????? ??????? ??? ? ???? ? ?? ?? ??????? ???? ?? ?? ??? ??? ? ?????? ??? ???? ???? ???????? ??? ????. ????Laravel??? php artisan make:migration create_users_table
??? ???? ?????? ??? ??? ? ????. ? ??? 2019_04_01_000001_create_users_table.php
? ??? ?? ??? ???? database/migrations
????? ? ?????? ??? ?????. ?????????? ??? ??? ? ??? ?? up
???? down
???? ???? ???. ?? up
???? ???? ?? ?????? ??? ???? ?? ??????? ??? ? ?????. down
???? ??????? ??? ? ???? ??? ?????. ??? ???? ? ?? ?? ??. ??? ???? ?? ?? ??? ???? ??? ?????. ??rrreee??? ??? id
, ?? 5? ??? ???? <code>users
?? ???? ?????. >name, email
, password
, remember_token
? ???? ???? 2?? ?? created_at
> ? updated_at
. ????? ??? ??? ? php artisan migration
??? ???? ??? ??????? ??? ? ????. ????3. Model????Laravel??? Eloquent ORM? ???? ??????? ???? ??? ? ????. Eloquent ORM? CRUD(??, ??, ????, ??) ??? ???? ??? ??? ???? ?????? ??? ??? ???? ? ??? ???. ?????? ??? ??????? ???? ??? ???????. ?? ?????? ?? ??? ???? ??? ??, ?? ? ? ?????? ?? ??? ??? ? ????. ??rrreee??? ??? mysql
??? ???? ? ?????? ?????. >users
????? ? ???? ?? ?? id
???. ?????????? ?? ??? ??? ? Eloquent ORM? ???? ?????? ??? ??? ? ????. Eloquent ORM? ? ?? ?? ??? ???????. ??3.1 ??? ??
??Eloquent ORM???create
???? ???? ???? ??? ? ????. ?: ??rrreee??? ??? Tom
??? ??? ???? ????, ??? ??? tom@example.com
, ????? password
???. > . ??3.2 ??? ??
??Eloquent ORM???get
???? ???? ???? ??? ? ????. ?: ??rrreee??? ??? users
???? ?? ??? ???? ?????. ????where
???? ???? ??? ??? ??? ?? ????. ?: ??rrreee??? ??? users
????? Tom
??? ??? ?? ??? ???? ?????. ??3.3 ??? ????
??Eloquent ORM???update
???? ???? ???? ????? ? ????. ?: ??rrreee??? ??? Tom
??? ??? ???? ??? ??? new_email@example.com
?? ?????. ??3.4 ??? ??
??Eloquent ORM???delete
???? ???? ???? ??? ? ????. ?: ??rrreee??? ??? Tom
??? ??? ???? ?????. ????
??? ???, Laravel? ? ?????? ?? ? ?? ???? ??? ? ?? ??? ?????? ?? ??? ?????. ??????? ??? ? ???? ??? ??? ?? ?? ?? ?? ? ?????? ?????? ?? ??? ???? ???. ??? Eloquent ORM? ???? ?? CRUD ?? ??? ???? ?? ????? ?? ????? ?? ? ????.
? ??? ??? ?????? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

TOWORKEFCITIVE WITHPIVOTTBLESINLARAVEL, FIRDSCESSPIVOTDATAUSINGWITHPIVOT () ORWITHTIMESTAMPS (), thenupdateentrieswithupdatee XistingPivot (), ManagerElationshipsviadetach () andsync (), andusecustompivotmodelswheneded.1.usewithpivot () toincludescificcol

Laravel ?? ???? 4 ?? ?? ??? ?? ?? ???? ???? ???? ? ????. 1. ?? ????? ???? ?? ??? ??? ??? ?? ???? ??? ???? ?????? ??? ??? ????? ?? ??? ?? ???? ??? ??????. 2. ???? ?? ????? ??????? ?????, n 1 ??? ???, ?? ??? ????, ??? ??, ??? ?? ? ?? ? ?? ??, ?? ??? ????. 3. ?? ?? ?? ? ?? ???? ??? ??? ?? ?? ??? ? ??? ???? ???? ???? ???? ???? ? ?? ????? ??????. 4. ??? ?? ? ???? ??? ??? ??? ?? ???? ????? ?? ?????? ????? ?? ???? ? ??? ?? ??? ????? ??????.

Laravel ????? ?????? ??? ???? ???? RefreshDatabase ??, ??? ??? ??, ?? ??? ?? ? ??? ?? ?? ??? ?????. 1. ?? ??? ???? ? ???? ??? ??????? ??????? ???? ?? ?????? ??? ???? ?????????. 2. ?? ??? ???? ??? ???? ??? ?? ???? ?? ?? ???? ?????. 3. DatabasEtransactionStrait? ???? ??? ?? ??? ????? ? ?????????. 4. ???? ???? ???? ??????? ???? ??? ? ?? ? ??????? ?? ??????. ??? ??? ???? ???? ???? ???? ?? ??? ? ??? ??? ?? ???? ?????.

Laravelsanctum? SPA ?? ??? ??????? ?? ???? ?? API ??? ??? ?? ??? ?? OAUTH2 ??? ??? ????? ?????. 1. Sanctum? ?? ?? ??? ????, ??? ???? ?????. 2. Passport? ?? ?? ? ????? ?? ??? ?? ??? ????? ????, ?? ???? ?????? ?????. 3. ?? ?? ? ??? ? ???? ?? ?? ??? ????. 4. ?? ??? ?????? ??? ???? ??? ?? ??? ??? ???? ?????. ??? ? ???? ?? ??? ?? OAUTH2 ??? ???? ??? ???????.

Laravel? ?? ???? ?????? ???? ??? ??????. 1. DB :: Transaction () ???? ???? ??? ???? ???? ?? ?? ?? ?? ?? ??; 2. ?? ? ????? ???? ?? ???? ?? ????? ????? ?? ???? ??? ???? ???? ??? ?? ????. 3.?? ??? ??? ??? ????? ??? begintransaction (), commit () ? rollback ()? ?? ?? ?? ??? ?????. 4. ?? ???? ??? ?? ????, ??? ?? ??, ??? ?? ? ?? ?? ??? ?????. ????? ?? ?? ??? ???? ?? ???? ??? ? ??? ???? ? ????.

Laravel?? ??? ??? ??? ???? ?? ???? ??? Route () ??? ??? ???? ????. Route () ??? ??? ?? ??? ???? ??? ???? ???? ?? ?? ???? ???? ????. 1. Route ( 'user.profile', [ 'id'=> 1])? ?? ???? ????? ?? ??? ?? ??? ?????. 2. ?? ?? ?? ? ? ?? ? ?????? ??? Route ( 'user.post.show', [ 'id'=> 1, 'postId'=> 10]? ?? ??? ??? ??? ????. 3. ??? ????? ?? ???? ???? ?? ?? ? ? ????. 4. ??? ?? ??? ???? ??? Route ( 'user.post',

Laravel?? HTTP ?? ? ??? ???? ??? ?? ???, ?? ?? ? ?? ???? ??? ????? ????. 1. ?? ???? ?? ? ? ?? ????? ?? ?? ????? ???? input () ?? ?? ???? ???? ??? ?? ??? ?? validate () ?? ?? ?? ???? ?? ? ? ????. 2. Return Response? ???,??, JSON, ?? ?? ? ??? ?? ?? ? ???? ??? ?????. 3. ?? ???? ?? ? ?? ?? () ??? ? Store ()? ???? ??? ???????. ????? ?? ?? ?? ? ??? ?????? ???? ??? ??????? ??? ? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.
