Php 操纵来自laravel Elount的结果集
在决定转移到php框架之后,我刚刚了解了laravel。我正在使用雄辩的查询从数据库检索文章的结果集:Php 操纵来自laravel Elount的结果集,php,laravel,eloquent,Php,Laravel,Eloquent,在决定转移到php框架之后,我刚刚了解了laravel。我正在使用雄辩的查询从数据库检索文章的结果集: $posts = Article::select(array('articles.id','articles.article_date','articles.image_link','articles.headline','articles.category', 'articles.published')); 这工作很好,结果如预期的那样 但是,我现在想将整个集合的文章日期格式从mysql日
$posts = Article::select(array('articles.id','articles.article_date','articles.image_link','articles.headline','articles.category', 'articles.published'));
这工作很好,结果如预期的那样
但是,我现在想将整个集合的文章日期格式从mysql日期格式更改为另一种格式
我曾考虑过迭代对象并使用常用的php方法进行更改,但我想知道在启动查询时还是在大量使用对象本身时,是否有更简单的方法来操作数据
我看过变异基因?但我不确定这是否合适,或者如何实施
感谢您的帮助,指针我不确定这是否有效,但请尝试一下
$posts = Article::select(array('articles.id',DB::raw('CAST(articles.article_date as date)'),'articles.image_link','articles.headline','articles.category', 'articles.published'));
你说得对,变异是最好的选择。(Laravel 3语法) 获取属性
$articles = Articles::get(array(
'article_date',
'image_link',
'headline',
'published'
));
foreach($articles as $article)
{
echo $article->article_date;
}
每次从模型中获取日期时,它都会首先通过mutator返回修改后的结果
绝对不需要对这样的东西运行原始查询
编辑设置好并弄混了。。。需要更多咖啡(答案编辑)太好了-这几乎达到了要求的结果-我不确定raw,但将您的答案更改为:$posts=Article::select(数组('articles.id',DB::raw('DATE_FORMAT(Article_DATE),'Article.image_link','articles.headline','articles.category','articles.published');以正确的格式提供日期。谢谢你的关键-两个解决方案!谢谢-我也会尝试一下,然后发回。我现在还不能100%确定tom是如何实现这个解决方案的。我把这段代码放在我的文章模型中,但到目前为止似乎没有像预期的那样改变日期格式——对吗?它是如何被调用的?我猜(正确或错误)您在使用Laravel4。你能证实这一点吗?如果L3的语法有点不同。嗨-拉雷维尔3目前…作为一个完全的新手拉雷维尔我应该切换到4,而现在学习???不,一点也不坚持3目前。4仍在测试中:)将更改我的答案。
$articles = Articles::get(array(
'article_date',
'image_link',
'headline',
'published'
));
foreach($articles as $article)
{
echo $article->article_date;
}