Laravel 5 Framework Learning eloquent relationship, laraveleloquent
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 the user.php, which is the user model, to look at, rather simple. We want to get all the articles directly in the form of $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, let's deal with the other end. The form we want is $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, 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, 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# Output information 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# The following is the execution process >>> $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" and "2015-03-31 03:24:55"
Now that we need new posts and user associations, we will temporarily modify form_partial.blade.php to hide a user ID, but temporarily:
Copy the 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 settings ];
OK, add an article. Let's take a look at it using Tinker.
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 "=" 2015-03-31 08:00:00 "," created_at "=" 2015-03-31 04:17:58 "," Updated_at "and" 20 15-03-31 04:17:58 "," excerpt "= null] #获取用户 >>> $user = App\user::first ();=> {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" and "2015-03"-31 08:00:00 "," created_at "=" 2015-03-31 04:17:58 "," updated_at "=" 2015-03-31 04:17:58 "," Excer PT "+ null]" #为什么使用 $user->articles instead of #user->articles ()? #事实上, $user->articles () returns the relationship if you want to use article S () You need to use >>> $user->articles ()->get ()->toarray ();=> [["id" = "1", "user_id" = "1", "title" = "User 1 article", "Body" and "User 1 body", "PUBLISHED_AT" and "2015- 03-31 08:00:00 "," created_at "=" 2015-03-31 04:17:58 "," updated_at "=" 2015-03-31 04:17:58 "," exc Erpt "+ null]" #你只能使用 articles () to do the next work, such as the following query $user->articles ()->where (' title ', ' User 1 article ')-& Gt;get (); #我们也可以通过 article get user>>> $article = App\article::first ();=> {id: "1", user_id: "1", Tit Le: "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;=> {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"}>>>
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/981355.html www.bkjia.com true http://www.bkjia.com/PHPjc/981355.html techarticle Laravel 5 Framework Learning eloquent relationship, laraveleloquent a user may have multiple articles, an article is written by a user, and that is the relationship. The same article may contain more than one ...