<?php
namespace Laonie\Models;
use Illuminate\Support\Facades\DB;
class Common {
public $table;
private static $_singletonDataObjects = NULL;
protected function __construct($table) {
$this->table = $table;
}
/**
* @param $table
* @return Common|null
*/
public static function getInstance($table) {
if (self::$_singletonDataObjects === NULL) {
self::$_singletonDataObjects = new self($table);
}
return self::$_singletonDataObjects;
}
/**
* 插入單條記錄
* @param $data
* @return mixed
*/
public function insert($data) {
return DB::table($this->table)->insertGetId($data);
}
/**
* 插入多條記錄(單條記錄也可)
* @param $data 二维数组
* @return bool
*/
public function multiInsert($data) {
DB::table($this->table)->insert($data);
return true;
}
/**
* * 刪除
* demo DB::table('users')->where('votes', '<', 100)->delete();
* @param $condition
* @return mixed
*/
public function delete($condition) {
$where = '';
foreach ($condition as $list) {
$where .= "where('{$list[0]}','{$list[1]}','{$list[2]}')->";
}
echo $eval = "return DB::table('{$this->table}')->" . $where . "delete();";
return eval($eval);
}
/**
* 更新
* @param $condition
* @param $data
* @return mixed
*/
public function update($condition, $data) {
$where = '';
$update = '[';
foreach ($condition as $list) {
$where .= "where('{$list[0]}','{$list[1]}','{$list[2]}')->";
}
foreach ($data as $list) {
foreach ($list as $k => $item)
$update .= "'$k' => '$item',";
}
$update = rtrim($update, ',');
$update .= ']';
$eval = "return DB::table('{$this->table}')->" . $where . "update($update);";
return eval($eval);
}
/**
* 查找
* @param $condition
* @return mixed
*/
public function get($condition) {
$where = '';
foreach ($condition as $list) {
$where .= "where('{$list[0]}','{$list[1]}','{$list[2]}')->";
}
$eval = "return DB::table('{$this->table}')->select()->" . $where . "get();";
return eval($eval);
}
}
0 个回复