Eloquent ORM - 条件列的名称

时间:2016-04-15 14:39:19

标签: laravel eloquent laravel-5.2

我正在使用Eloquent,我有事件模型。它有created_at字段。我正在尝试选择一些事件,并为每个事件设置一个属性type,其值基于列created_at。例如,如果created_at&lt;今天该事件已经过去,type属性已被'传递'。有没有办法通过查询构建器Event::select('if created_at < NOW()传递else if .... as type');

来执行此操作

1 个答案:

答案 0 :(得分:0)

There are a couple of ways to solve your problem. You can use either eloquent scopes or mutators.

https://laravel.com/docs/5.2/eloquent#query-scopes -> check for Local Scopes https://laravel.com/docs/5.2/eloquent-mutators -> check for Accessors & Mutators

I'm guessing you're using the latest Laravel LTM version.

The local scope solution will allow you to do Event::passed()->etc() and the Accessors append a flag to the Event object saying passed => true.