【发布时间】:2013-12-02 05:09:33
【问题描述】:
我是一名尝试采用 Laravel 的 CodeIgniter,但是我在理解如何使用 Eloquent 时遇到了很多问题。
我怀疑如果我能弄清楚如何将我的一些 CodeIgniter 模型方法转换为 Laravel Eloquent,我可能会做得更好。希望这能帮助其他有同样问题的人。
任何人都可以将以下代码从 CodeIgniter 重写为 Eloquent:
public function get_products($product_id = NULL, $category_id = NULL, $limit = NULL)
{
$this->db->select('*, product.id AS product_id');
$this->db->from('product');
$this->db->join('product_unit', 'product.source_unit_id = product_unit.id', 'left');
$this->db->join('stock_levels', 'product.stock_level_id = stock_levels.id', 'left');
$this->db->join('categories', 'product.category_id = categories.cat_id', 'left');
if(isset($product_id)) {
$this->db->where('product.id', $product_id);
}
if(isset($category_id)) {
$this->db->where('product.category_id', $category_id);
}
if(isset($limit)) {
$this->db->limit($limit);
}
#$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('categories.cat_name', 'ASC');
$this->db->order_by('product.name', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
【问题讨论】:
标签: codeigniter model laravel laravel-4 eloquent