如何在Laravel中建立基于条件的查询? [英] How can I build a condition based query in Laravel?
本文介绍了如何在Laravel中建立基于条件的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在Code Igniter中执行此操作:
I can do this in Code Igniter:
$this->db->select();
$this->from->('node');
if ($published == true)
{
$this->db->where('published', 'true');
}
if (isset($year))
{
$this->db->where('year >', $year);
}
$this->db->get();
如何翻译此代码以便在Laravel中工作?
How can this code be translated so that it works in Laravel?
推荐答案
在Fluent中,您可以执行以下操作:
In Fluent you can do:
$query = DB::table('node');
if ($published == true)
$query->where('published', '=', 1);
if (isset($year))
$query->where('year', '>', $year);
$result = $query->get();
这篇关于如何在Laravel中建立基于条件的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文