Laravel 5 Foundation (11)-Eloquent relationship
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 = '[email protected]';=> "[email protected]">>> $user->password = bcrypt('pass');=> "$2y$10$Nbl2b9wqd.rXqKEsd3pRSOoIyFAFIhbqf71BufwDfS3Guv21SlEx2">>> $user->save();=> true>>> App\User::first()->toArray();=> [ "id" => "1", "name" => "zhang jinglin", "email" => "[email protected]", "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: "[email protected]", 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" and "=" 1 "," title " = "User 1 article", "Body" and "User 1 body", "published_at" and "2015-03-31 08:00 : xx "," 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 the Relationship, if you want to use articles () you need to use >>> $user->articles ()->get ()->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" and "2015-03-31 04 : 17:58 "," updated_at "=" 2015-03-31 04:17:58 "," excerpt "and Null]] #你只能使用 AR Ticles () 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", 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: "[ Email protected] ", Created_at:" 2015-03-31 03:24:55 ", Updated_at:" 2015-03-31 03:24:55 "}>>>
-
1/ F Findgor
-
Let's talk about the background image upload. Like uploading a user's avatar. Form edit submission, mvc how to write. Frame can not see clearly.