如何通过使用Laravel雄辩的Relatinoships获取数据 [英] How to fetch data by using Laravel Eloquent Relatinoships

查看:150
本文介绍了如何通过使用Laravel雄辩的Relatinoships获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手。目前正试图在我的应用程序中使用雄辩。我有3个数据库表..旅行,trips_album,album_images。表列如下。



跳闸 - id(自动增量),trip_title,about_place,created_by

trips_album - id(auto_increment),trips_id(指向旅行表的id),album_status

album_images - album_id(指向id trip_album表),img_name



所以,现在我想要所有属于任何特定用户的专辑图片。我如何通过使用Laravel雄辩的关系来做到这一点。我正在使用Laravel 4.2。

解决方案

第一个,你可以设置一些。

  class Trip扩展Eloquent {
public $ table =tripps;
}
class Album Enloquent {
public $ table =trips_album;
}
class Image扩展Eloquent {
public $ table =album_images;
}

之后,使用有很多通过。文件此处

  class Trip扩展Eloquent {
...

public function Images()
{
return $ this-> ; hasManyThrough('Image','Album','trip_id','album_id');
}
}


I'm new to Laravel. Currently trying to use eloquent in one of my application. I've 3 database tables.. trips, trips_album, album_images. Table column are as follows..

trips - id(auto increment), trip_title, about_place, created_by
trips_album - id(auto_increment), trips_id(pointing to 'id' of trip table), album_status
album_images - album_id(pointing to 'id' of trip_album table), img_name

So, now i want all album images of trips, which belong to any particular user. How can i do this by using Laravel Eloquent Relationships. I'm using Laravel 4.2.

解决方案

The first, you may setting somethings.

class Trip extends Eloquent {
    public $table = "trips";
}
class Album extends Eloquent {
    public $table = "trips_album";
}
class Image extends Eloquent {
    public $table = "album_images";
}

After that, using "Has Many Through". Document Here

class Trip extends Eloquent {
    ...

    public function Images()
    {
        return $this->hasManyThrough('Image', 'Album', 'trip_id', 'album_id');
    }
}

这篇关于如何通过使用Laravel雄辩的Relatinoships获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆