A user may have multiple articles, an article is written by a user, this is the relationship. The same article may contain more than one tag, and one tag may be associated with multiple articles.
In the project, we already have the user.php, namely the user model, the view, is quite simple. We want to get all the articles directly using the form $user->articles (), let's modify the user model:
Public Function articles () {return
$this->hasmany (' app\article ');
}
But we just finished one end of the relationship and 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, modify create_article_table.php
Schema::create (' articles ', function (Blueprint $table)
{
$table->increments (' id ');
Specifies the foreign key column
$table->integer (' user_id ')->unsigned ();
$table->string (' title ');
$table->text (' body ');
$table->timestamp (' published_at ');
$table->timestamps ();
Generate foreign Key
$table->foreign (' user_id ')
->references (' id ')
->on (' users ')
->ondelete ( ' Cascade ');
Because we are only in the development phase, there is no online operation, we can directly modify the database migration files, roll back and then migrate, but if the line to run, you should create a new migration.
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
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
Now let's use Tinker to create a user.
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"
]
>> >
Now that we need a new post and user Association, we'll temporarily modify form_partial.blade.php to hide a user ID, just for the time being:
Copy Code code as follows:
{{--Temporary processing--}}
{!! Form::hidden (' user_id ', 1)!!}
Also modify the $fillabel properties of the model so that our Mass assisment.
protected $fillable = [
' title ',
' body ', '
published_at ', '
user_id '/temporary setting
];
OK, add an article. Let's use tinker to check it out.
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->ar
Ticles ()->toarray (); Badmethodcallexception with message ' Call to undefined method Illuminate\database\query\builder::toarray () ' >>
> $user->articles->toarray (); => [["id" => "1", "user_id" => "1", "title" => "user1 Article "," The Body "=>" the User 1 Body "," Published_at "=>" 2015-03-31 08:00:00 "," Created_at "=& Gt "2015-03-31 04:17:58", "Updated_at" => "2015-03-31 04:17:58", "excerpt" => NULL]] #为什么使用 $
User->articles rather than #user->articles ()?
#事实上, $user->articles () returns the relationship if you want to use articles () you need to use the >>> $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:5 8 "," Updated_at "=>" 2015-03-31 04:17:58 "," excerpt "=> null]" #你只能使用 articles () to do the next step
, such as the following query $user->articles ()->where (' title ', ' User 1 Article ')->get ();
#我们也可以通过 article get user >>> $article = App\article::first ();
=> <app\article #0000000051cbb9d60000000073e11a3e > {id: "1", USER_ID: "1", Title: "User 1 Article", Body: "The user 1 Body", Published_at: "2015-03-31 08:00:00", creat
Ed_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@exampl
E.com ", Created_at:" 2015-03-31 03:24:55 ", Updated_at:" 2015-03-31 03:24:55 "} >>>
The above is the entire contents of this article, I hope you can enjoy.