A user may have multiple articles, an article written by a user, which is a relationship. The same article may contain multiple tags, while a tag may have multiple articles associated with it.
In the project, we already have User.php
, that is the user model, to look at, quite simple. We want to $user->articles()
get all the articles in the form of direct use, let's modify the user model:
public function articles() { return $this->hasMany('App\Article'); }
But we just finished one end of the relationship, let's deal with the other end. We want the form to be $article->user()
, let's modify the article model.
public function user() { return $this->belongsTo('App\User'); }
In our database, the article model does not have the user's foreign key, we need to set up, modifycreate_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');});
Because we are only in the development phase, not yet on-line operation, we can directly modify the database migration files, roll back and then migrate, but if you run on-line, you should create a new migration.
php artisan migrate:refresh#输出信息Rolled back: 2015_03_28_050138_create_article_tableRolled back: 2014_10_12_100000_create_password_resets_tableRolled back: 2014_10_12_000000_create_users_tableNothing to rollback.Migrated: 2014_10_12_000000_create_users_tableMigrated: 2014_10_12_100000_create_password_resets_tableMigrated: 2015_03_28_050138_create_article_tableMigrated: 2015_03_28_051200_add_excerpt_to_articels_table
Now let's use tinker
to create a user.
php artisan tinkerPsy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman#下面是执行过程>>> $user = new App\User;=> {}>>> $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"
Now that we need new posts and user associations, we'll temporarily modify form_partial.blade.php
them to hide a user ID, but temporarily:
{{--临时处理--}}{!! Form::hidden('user_id', 1) !!}
Also modify the properties of the model $fillabel
so that our Mass assisment.
protected $fillable = [ 'title', 'body', 'published_at', 'user_id' //临时设置 ];
OK, add an article. Let's use it tinker
to check it out.
PHP artisan tinkerpsy Shell v0.4.1 (php 5.4.16-cli) by Justin hileman>>> App\article::first ()->toarray (); =&G T ["id" = "1", "user_id" = "1", "title" = "User 1 article", "body "=" User 1 Body "," Published_at "and" 2015-03-31 08:00:00 "," Created_at "and" 2015-03-31 04 : 17:58 "," updated_at "=" 2015-03-31 04:17:58 "," excerpt "and Null] #获取用户 >>> $user = Ap P\user::first ();=> {id: "1", Name: "Zhang Jinglin", Email: "Zjl@example.com", Created_at: "20 15-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" and "=" 1 "," title "=> "User 1 article", "body" = "User 1 Body", "published_at" and "2015-03-31 08:00:00", "Created_at" = "2015-03-31 04:17:58", "Updated_at" and "2015-03-31 04:17:58", "excerpt "+ null]" #为什么使用 $user->articles instead of #user->articles ()? #事实上, $user->articles () returns a relationship if you want to use a Rticles () You need to use >>> $user->articles ()->get ()->toarray ();=> [["id" = " 1 "," user_id "=" 1 "," title "=" User 1 article "," Body "and" U " Ser 1 Body "," published_at "=" 2015-03-31 08:00:00 "," Created_at "and" 2015-03-31 04:17:58 ", "Updated_at" = "2015-03-31 04:17:58", "excerpt" and "= null]] #你只能使用 articles () to Do the next work, such as the following query $user->articles ()->where (' title ', ' User 1 article ')->get (); #我们也可以通过 article get user>> > $article = app\article:: First ();=> {id: "1", user_id: "1", Title: "User 1 article", Body: "User 1 Body", publ Ished_at: "2015-03-31 08:00:00", Created_at: "2015-03-31 04:17:58", Updated_at: "2015-03-31 04:17:58", E Xcerpt:null}>>> $article->user;=> {id: "1", Name: "Zhang Jinglin", email: "Zjl@exam Ple.com ", Created_at:" 2015-03-31 03:24:55 ", Updated_at:" 2015-03-31 03:24:55 "}>>>
The above describes the Laravel 5 basic (11)-eloquent relationship, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.