How to implement this moment in Laravel using Eloquent. I already asked a similar question today, but didn’t quite accurately describe it. So I need a similar solution that I used on Codigniter. I called these methods by simply passing the table name. Can I implement something like this in Laravel using Eloquent?
class MY_Model extends CI_Model{ const USERS = 'users'; const ORGS = 'organizations'; const DEPS = 'departments'; const CONTRS = 'countries'; public function save($table, $data){ $query = $this->db->insert($table,$data); //$this->db->where_in('id', $array_row); if($query) return $this->db->insert_id(); else return false; } public function saveAll($tabl, $data){ $query = $this->db->insert_batch($table,$data); if($query) return true; else return false; } public function update($table, $data, $id = null){ if($id){ if(is_numeric($id)) $this->db->where('id_'.$table,$id); else $this->db->where($id); } $result = $this->db->update($table,$data); //$count = $this->db->affected_rows(); return $result; } public function delete($table, $cond){ $query = $this->db->delete($table, $cond); return $query; } public function getRow($table, $arr = null){ if($arr) $this->db->where($arr); $query = $this->db->get($table); return $query->row_array(); } public function getAll($table, $cond_arr = null, $limit1 = null, $limit2 = null, $by_title = null, $order = null){ if(empty($limit2)) $limit2 = 0; if($limit1) $this->db->limit($limit1,$limit2); //$this->db->limit(5,5); if($cond_arr) $query = $this->db->where($cond_arr); if($order and $by_title) $this->db->order_by($by_title, $order); $query = $this->db->get($table); return $query->result_array(); } So far, this has turned out to be implemented. The general model and all the others expand it.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class General extends Model{ public static function getAll(){ return self::all(); } } ?> <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Department extends General{ protected $table = 'departments'; } ?> <?php namespace App\Http\Controllers; use App\Models\Department; use App\Models\User; class MainController extends Controller{ public function index(){ $data['title'] = 'Blade Template'; $data['departments'] = Department::getAll(); $data['users'] = User::getAll(); return view('main', $data); } } ?>