Eloquent Relationship(一对多,一对一)

WBOY
Freigeben: 2016-06-23 13:02:30
Original
1092 Leute haben es durchsucht

前提数据库的数据

users表id  name    email   password    remember_token  created_at  updated_at1   123456  123@qq.com  $2y$10$EfEo1gCcK6JdwgxjqjNbK.fVZjgu7i68uKMPqNwBX9VpNVuvgthm6    wSaR4V256k4xxisbbiVNS1o9iEqwCaIDZB5Nk5YZYj5JNBENIiowTALrBNNT    2016-05-25 15:31:07 2016-05-25 15:41:53articles表id  title   content publish_at  created_at  updated_at  user_id1   title   content 2016-05-14 18:04:44 2016-05-14 18:04:48 2016-05-14 18:04:48 1--------------------------------id  title   content publish_at  created_at  updated_at  user_id2   title2  content2    2016-05-15 04:24:48 2016-05-14 18:07:42 2016-05-14 18:07:42 1
Nach dem Login kopieren

user_id作为了默认的外键名称,因为user表里面的主键是id,laravel的eloquent模块自动处理了这些,他在自己本身里面维护了一个外键的关联关系。

一对一的关系

例如,一个User模型有一个与之对应的article模型,一个用户有一篇文章

app/User.php    public function articles-one(){        return $this->hasOne('App\Articles'); //用hasOne方法,这个方法是laravel提供的,直接在model里面使用                                              //传入参数是另外一个model,这里是抽象化,解析出来其实就是一个表和另外一个表的一对一的关联    }
Nach dem Login kopieren

用命令行工具tinker演示

php artisan tinkerPsy Shell v0.7.2 (PHP 5.5.34 — cli) by Justin Hileman>>> $user = App\User::first();=> App\User {#691     id: 1,   //这个是user的主键,其实就是users表的主键     name: "123456",     email: "123@qq.com",     created_at: "2016-05-25 15:31:07",     updated_at: "2016-05-25 15:41:53",   }>>> $user->articles-one;  //调用刚才写的articles-one方法,写这个方法目的就是在这里,在实例化这个model的时候,添加一个可以操作的方法,然后实现我们自己需要的设计。=> App\Articles {#692     id: 1,     title: "title",     content: "content",     publish_at: "2016-05-14 18:04:44",     created_at: "2016-05-14 18:04:48",     updated_at: "2016-05-14 18:04:48",     user_id: 1,   //自动默认关联的是user model的id键,这是laravel的默认设定,关联后会自动生成 model名+主键的外键                   //这样就代表了这篇文章是属于id为1的用户的。                 }>>> 
Nach dem Login kopieren

如果不想默认,想自定义外键名称的话, return $this->hasOne('App\Articles', 'foreign_key', 'local_key');传入一个foreign_key的参数,然后后面再加一个你的自定义键就好了。

一对多的关系

一个User模型有一个与之对应的article模型,一个用户有多篇文章

app/User.php    public function articles(){ //为了方便区分,将articles-one 改为articles,代表很多文章,而不只是一篇        return $this->hasMany('App\Articles');    }
Nach dem Login kopieren

用命令行工具tinker演示

php artisan tinkerPsy Shell v0.7.2 (PHP 5.5.34 — cli) by Justin Hileman>>> $user = App\User::find(1);=> App\User {#693     id: 1,     name: "123456",     email: "123161769@qq.com",     created_at: "2016-05-25 15:31:07",     updated_at: "2016-05-25 15:41:53",   }>>> $articles = App\User::find(1)->articles; //find方法是laravel默认提供的,查询方法使用方式就知道,传入的是id参数,所以这里的意思是找到id为1的所有数据                                             //然后通过链式方式,继续在原来的基础上过滤结果,执行articles方法,组合起来就会返回属于用户id为1的所有文章                                             //其实相当于写select * from articles where user_id = 1;(当然这里面是有多表查询的,这不是可执行的sql语法,这里只是为了方便转换思维)=> Illuminate\Database\Eloquent\Collection {#702     all: [       App\Articles {#703         id: 1,         title: "title",         content: "content",         publish_at: "2016-05-14 18:04:44",         created_at: "2016-05-14 18:04:48",         updated_at: "2016-05-14 18:04:48",         user_id: 1,       },       App\Articles {#704         id: 2,         title: "title2",         content: "content2",         publish_at: "2016-05-15 04:24:48",         created_at: "2016-05-14 18:07:42",         updated_at: "2016-05-14 18:07:42",         user_id: 1,       },
Nach dem Login kopieren

一对多还有一个反向查找的用法,定义个相对的关联

例如一个用户有多篇文章是一对多的关系,然后可以设置通过这篇文章反向查找到这个所属用户

既然是反向查找,那么就是在文章这个model里面做处理

app/Articles.php    public function user()    {        return $this->belongsTo('App\User');  //belongsto就是这个关键字,也是跟意思一样,是属于的意思,文章里面属于User model的文章,返回user                                              //他们的机制依然是通过外键来完成的,通过articles的外键user_id和 user的id 进行关联    }
Nach dem Login kopieren
php artisan tinkerPsy Shell v0.7.2 (PHP 5.5.34 — cli) by Justin Hileman>>> $user = App\Articles::find(1)->user;=> App\User {#696     id: 1,     name: "123456",     email: "123161769@qq.com",     created_at: "2016-05-25 15:31:07",     updated_at: "2016-05-25 15:41:53",   }>>> 
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!