php - Laravel:每张 table 都需要一个模型吗?

标签 php laravel model laravel-4 eloquent

我正在使用 Laravel 构建一个事件系统。我有几个表,例如:

  • 场合
  • occasion_categories(汽车、三轮车和其他)

现在我有一个名为 Occasion 的模型,它表示表 occasions,它还有一个指向 occasion_categories 表的外键。

我想检索所有场合类别,但是

  • 我是否需要为 occasion_categories 创建一个名为 OccasionCategory 的单独模型并定义这些模型中的所有关系,

  • 或者我可以在 Occasion 类中创建一个方法,例如 getCategories(),然后使用 DB 类,例如 DB::table('occasion_categories')->get() 来检索所有可能的类别?

最佳答案

2019 年 10 月 4 日更新:

简而言之,NO,您可以使用 Query Builder 而不是 Eloquent ORM 但是如果您想使用 Eloquent ORM 那么每个表都必须绑定(bind)到一个 model。此外,模型不一定必须是 Eloquent 模型,您可以在不扩展 Eloquent 模型的情况下创建模型,Eloquent 模型可能使用也可能不使用数据库。模型并不意味着数据库访问层,但...无论如何,这是另一个大话题。

原答案:

实际上,如果您使用 Eloquent,则需要为您的两个表创建两个 Eloquent 模型,例如:

class Occasion extend Eloquent {
    // Laravel will look for occasions table for Occasion model so following line
    // is optional if you don't want to use another table name for Occation model
    protected $table = 'occasions';

    // Now declare the relationship with "occasion_categories" table
    public function occasionCategory()
    {
        // Make sure you have used occasion_categories_id as the foreugn key
        return $this->belongsTo('OccasionCategory', 'occasion_categories_id', 'id');
    }
}

现在创建 OccasionCategory 模型:

class OccasionCategory extend Eloquent {

    protected $table = 'occasion_categories';

    // Now declare the relationship with "occasion_categories" table
    public function occasions()
    {
        // Make sure you have used occasion_categories_id as the foreign key
        return $this->hasMany('Occasion', 'occasion_categories_id', 'id');
    }
}

现在你可以用它的父 occasions_category 使用这样的东西检索 occasions:

// Use the Occasion model
$allOccasionsWithCategory = Occasion::with('occasionCategory')->get();

// Find one Occasion whose id is 1 with OccasionCategory
$oneOccasionsWithCategory = Occasion::with('occasionCategory')->find(1);

// You may use this to get the related occasionCategory model
$occasionCategory = $oneOccasionsWithCategory->occasionCategory;

// Use the OccasionCategory model
$allOccasionsWithCategory = OccasionCategory::with('occasions')->get();

// Find one OccasionCategory whose id is 1 with Occasion
$oneOccasionsWithCategory = OccasionCategory::with('occasions')->find(1);

// You may use this to get all the related occasions model
$occasions = $oneOccasionsWithCategory->occasions;

阅读更多关于 relationship 的信息在 Laravel 网站上。

如果您直接使用 Query Builder 那么您可以使用类似这样的东西(没有模型):

// All occations
$occations = DB::table('occations')->get();

// All occasions and related occasion_categories
$occationsAndCategories = DB::table('occations')
                            ->join('occasion_categories', 'occasions.occasion_category_id', '=', 'occasion_categories.id')
                            ->get();

阅读更多关于 Query Builder 的信息在 Laravel 网站上。

关于php - Laravel:每张 table 都需要一个模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24376908/

相关文章:

php - 如何使用mySQL添加 'A' ,'B' ,'C'这样的分数

php - 从 PHP 修改多行 mySQL 数据

javascript - Zend Framework 2 中根据请求类型在同一路由上触发不同的操作

php - 拉维尔 5.8 : Display eloquent items sorted based on timestamp

php - Laravel Excel 验证

magento - 获取magento中所有产品属性的列表

mysql - 在多步骤应用程序中保持数据完整性

来自数据库数组的 PHP TreeView

php - Zend Framework 2 RESTful Controller 操作

Laravel groupBy 分页