這篇文章主要講解了“如何使用Laravel 5框架中Eloquent”,文中的講解內(nèi)容簡單清晰,易于學(xué)習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習“如何使用Laravel 5框架中Eloquent”吧!

一個用戶可能有多個文章,一個文章是某個用戶書寫的,這就是關(guān)系。同樣文章中可能包含多個 TAG,而一個 TAG 可能關(guān)聯(lián)多個文章。
在項目中,我們已經(jīng)有了 User.php,也就是用戶模型,查看一下,相當簡單。我們希望直接使用 $user->articles() 的形式獲取全部文章,讓我們修改 user 模型:
public function articles() {
return $this->hasMany('App\Article');
}但是我們僅僅完成了關(guān)系中的一端,讓我們來處理另一端。我們希望的形式是 $article->user() ,讓我們修改 article 模型。
public function user() {
return $this->belongsTo('App\User');
}在我們的數(shù)據(jù)庫中,文章模型沒有用戶的外鍵,我們需要設(shè)置,修改 create_article_table.php
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
//指定外鍵列
$table->integer('user_id')->unsigned();
$table->string('title');
$table->text('body');
$table->timestamp('published_at');
$table->timestamps();
//生成外鍵
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});因為我們只是在開發(fā)階段,還沒有上線運行,我們可以直接修改數(shù)據(jù)庫遷移文件,回滾然后遷移即可,但是如果上線運行的話,應(yīng)該新建一個遷移。
php artisan migrate:refresh #輸出信息 Rolled back: 2015_03_28_050138_create_article_table Rolled back: 2014_10_12_100000_create_password_resets_table Rolled back: 2014_10_12_000000_create_users_table Nothing to rollback. Migrated: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_100000_create_password_resets_table Migrated: 2015_03_28_050138_create_article_table Migrated: 2015_03_28_051200_add_excerpt_to_articels_table
現(xiàn)在讓我們使用 tinker 來創(chuàng)建一個用戶。
php artisan tinker
Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman
#下面是執(zhí)行過程
>>> $user = new App\User;
=> <App\User #000000007f1ad61a000000006497cc4f> {}
>>> $user->name = 'zhang jinglin';
=> "zhang jinglin"
>>> $user->email = 'zjl@example.com';
=> "zjl@example.com"
>>> $user->password = bcrypt('pass');
=> "$2y$10$Nbl2b9wqd.rXqKEsd3pRSOoIyFAFIhbqf71BufwDfS3Guv21SlEx2"
>>> $user->save();
=> true
>>> App\User::first()->toArray();
=> [
"id" => "1",
"name" => "zhang jinglin",
"email" => "zjl@example.com",
"created_at" => "2015-03-31 03:24:55",
"updated_at" => "2015-03-31 03:24:55"
]
>>>現(xiàn)在我們需要新發(fā)布的文章和用戶關(guān)聯(lián),我們暫時先修改 form_partial.blade.php 來隱藏一個用戶id,只是暫時:
復(fù)制代碼 代碼如下:
{{--臨時處理--}}
{!! Form::hidden('user_id', 1) !!}
同時要修改模型的 $fillabel 屬性,以便我們的 Mass Assisment。
protected $fillable = [ 'title', 'body', 'published_at', 'user_id' //臨時設(shè)置 ];
OK,添加一個文章。我們使用 tinker 來查看一下。
php artisan tinker
Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman
>>> App\Article::first()->toArray();
=> [
"id" => "1",
"user_id" => "1",
"title" => "User 1 Article",
"body" => "User 1 Body",
"published_at" => "2015-03-31 08:00:00",
"created_at" => "2015-03-31 04:17:58",
"updated_at" => "2015-03-31 04:17:58",
"excerpt" => null
]
#獲取用戶
>>> $user = App\User::first();
=> <App\User #0000000051cbb9d70000000073e11a3e> {
id: "1",
name: "zhang jinglin",
email: "zjl@example.com",
created_at: "2015-03-31 03:24:55",
updated_at: "2015-03-31 03:24:55"
}
#獲取用戶文章
>>> $user->articles()->toArray();
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::toArray()'
>>> $user->articles->toArray();
=> [
[
"id" => "1",
"user_id" => "1",
"title" => "User 1 Article",
"body" => "User 1 Body",
"published_at" => "2015-03-31 08:00:00",
"created_at" => "2015-03-31 04:17:58",
"updated_at" => "2015-03-31 04:17:58",
"excerpt" => null
]
]
#為什么使用 $user->articles 而不是 #user->articles()?
#事實上,$user->articles()返回的是關(guān)系,如果你想用 articles() 你需要這樣用
>>> $user->articles()->get()->toArray();
=> [
[
"id" => "1",
"user_id" => "1",
"title" => "User 1 Article",
"body" => "User 1 Body",
"published_at" => "2015-03-31 08:00:00",
"created_at" => "2015-03-31 04:17:58",
"updated_at" => "2015-03-31 04:17:58",
"excerpt" => null
]
]
#你只能使用 articles() 來進行下一步的工作,比如下面的查詢
$user->articles()->where('title', 'User 1 Article')->get();
#我們也可以通過 article 獲取 user
>>> $article = App\Article::first();
=> <App\Article #0000000051cbb9d60000000073e11a3e> {
id: "1",
user_id: "1",
title: "User 1 Article",
body: "User 1 Body",
published_at: "2015-03-31 08:00:00",
created_at: "2015-03-31 04:17:58",
updated_at: "2015-03-31 04:17:58",
excerpt: null
}
>>> $article->user;
=> <App\User #0000000051cbb92d0000000073e11a3e> {
id: "1",
name: "zhang jinglin",
email: "zjl@example.com",
created_at: "2015-03-31 03:24:55",
updated_at: "2015-03-31 03:24:55"
}
>>>感謝各位的閱讀,以上就是“如何使用Laravel 5框架中Eloquent”的內(nèi)容了,經(jīng)過本文的學(xué)習后,相信大家對如何使用Laravel 5框架中Eloquent這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
分享名稱:如何使用Laravel5框架中Eloquent-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article30/dgpipo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、App開發(fā)、服務(wù)器托管、網(wǎng)頁設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容