[英]Laravel: Does every table need a model?
I am building an occasion system with Laravel. 我正在用Laravel建立一个场合系统。 I have several tables like:
我有几个表,如:
Now i have a model called Occasion
which represents the table occasions, it also has a foreign key to the occasion_categories table. 现在我有一个名为
Occasion
的模型,它代表了表的场合,它还有一个对于occasion_categories表的外键。
I want to retrieve all occasion categories, but 我想检索所有场合类别,但是
do i need to make a seperated model called OccasionCategory
for the occasion_categories and define all relations in these models, 我是否需要为occasion_categories创建一个名为
OccasionCategory
的独立模型,并定义这些模型中的所有关系,
or can i just make a method in the Occasion
class like getCategories()
and then use the DB
class like DB::table('occasion_categories')->get()
to retrieve all possible categories? 或者我可以在
Occasion
类中创建一个方法,如getCategories()
,然后使用DB
类如DB::table('occasion_categories')->get()
来检索所有可能的类别?
Actually you need to create two Eloquent
models for both of your tables if you are using Eloquent
, for example: 实际上,如果您使用
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');
}
}
Now create the OccasionCategory
model: 现在创建
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');
}
}
Now you may retrieve the occasions with it's parent occasions_category using something like this: 现在,您可以使用以下内容检索具有父级occasion_category的场合:
// 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;
Read more about relationship on Laravel
website. 阅读有关
Laravel
网站关系的更多信息。
If you use Query Builder
directly then you may use something like this (without a model): 如果您直接使用
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();
Read more about Query Builder on Laravel
website. 在
Laravel
网站上阅读有关Query Builder的更多信息。
The laravelish way to do database is using the Query Builder or Eloquent : Laravel is a framework and it always makes sense to leverage the resources it exposes. 该laravelish方式做数据库使用的是查询生成器或机锋 :Laravel是一个框架,它总是有道理利用它暴露的资源。
The short answer: 简短的回答:
- No, it doesn't as long as you don't use Eloquent .
不,只要你不使用Eloquent就没有。
The long answer: 答案很长:
The Query Builder is the way to go if you still want to stick to laravel convention.
如果您仍想坚持laravel约定,那么查询生成器就是您的选择。
You may also use the plain old PDO : Remember Laravel is a actually a PHP framework!
您也可以使用普通的旧PDO :记住Laravel实际上是一个PHP框架!
It's always rewarding to be consistent : 保持一致总是有益的:
Do it the Eloquent
ways (Models + relationships definition) despite you can mix Eloquent
and some Query Builder
as you suggested. 尽管你可以按照你的建议混合
Eloquent
和一些Query Builder
,但这样做是Eloquent
方式(模型+关系定义)。
Doing it using Query Builder
exclusively is also consistent and possible. 使用
Query Builder
专门执行此操作也是一致且可行的。
My personal preference goes to the Eloquent
option. 我个人的偏好是
Eloquent
选项。
WereWolf - The Alpha answer provides excellent possible solutions using both options. WereWolf - Alpha 答案使用这两个选项提供了出色的解决方案。
Just wanted to chime in here with an update. 只是想在这里发布一个更新。
In this particular instance, I'm not sure things have changed since this was posted (could be wrong, always more to discover), but in the case of Many-to-Many relationships, the one-to-one relationship between table and model is broken, thanks to the pivot() method . 在这个特定的例子中,我不确定自发布之后事情是否已经发生了变化(可能是错误的,总是更多地发现),但在多对多关系的情况下,表与表之间的一对一关系由于pivot()方法,模型被破坏了。
Taking from one of my own projects: 从我自己的一个项目中获取:
public function fees()
{
return $this->belongsToMany(Service::class)->withPivot("description");
}
In this way, you can have two models ("Fee" and "Service"), but you can access data on the intermediary table without needing a "ServiceFee" model. 通过这种方式,您可以拥有两个模型(“费用”和“服务”),但您可以访问中间表上的数据,而无需“ServiceFee”模型。
In Blade: 在刀片中:
{{ $service->pivot->description }}
At a glance, this may seem trivial, but get enough many-to-many relationships going and the number tables could even surpass the number of models. 乍一看,这看起来似乎微不足道,但是获得足够的多对多关系,数字表甚至可以超过模型的数量。 (Whether this hypothetical scenario is necessarily well-designed is a matter I am sadly lacking the time to ponder.)
(这个假设情景是否必须精心设计,我很遗憾没有时间去思考。)
Although I am not positive, I am pretty sure that would work. 虽然我不积极,但我很确定这会奏效。 Or,
DB::raw('select * from occasion_categories')
should work. 或者,
DB::raw('select * from occasion_categories')
应该可以工作。
I don't know what the best practices here are, but if you're simply asking if it's possible then yes. 我不知道这里的最佳做法是什么,但如果你只是问是否可能,那么是的。 One of those two methods should work just fine.
这两种方法中的一种应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.