public function articles() {
return $this->hasMany('App\Article');
}
public function user() {
return $this->belongsTo('App\User');
}
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');
});
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
php artisan tinker
Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman
#下面是执行过程
>>> $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"
]
>>>
protected $fillable = [
'title',
'body',
'published_at',
'user_id' //临时设置
];
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()返回的是关系,如果你想用 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"
}
>>>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有