laravel model(创建、更新、删除)

namespace App\Models\Common;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CommonModel extends Model {
	/**
	 * 创建
	 * @param  String			   $table     表名
	 * @param  Array			   $condition 条件
	 * @return Boolean
	 */
	public static function create($table = 'demand', $condition = array()) {
		if (empty($condition)) {
			return false;
		}
		return DB::table($table)->insert($condition);
	}
	
	/**
	 * 创建(返回主键ID)
	 * @param  String			   $table     表名
	 * @param  Array			   $condition 条件
	 * @return Integer || Boolean
	 */
	public static function create($table = 'demand', $condition = array()) {
		if (empty($condition)) {
			return false;
		}
		return DB::table($table)->insertGetId($condition);
	}

	/**
	 * 更新
	 * @param String  $table     表名
	 * @param String  $fieldName 字段名
	 * @param Integer $value     值
	 * @param Array   $condition 条件
	 * @return Boolean
	 */
	public static function toUpdate($table = 'demand', $fieldName = 'demand_id', $value = 0, $condition = array()) {
		if (empty($value) || empty($condition)) {
			return false;
		}
		return DB::table($table)->where($fieldName, $value)->update($condition);
	}

	/**
	 * 删除
	 * @param String  $table     表名
	 * @param String  $fieldName 字段名
	 * @param String  $type      类型
	 * @param Integer $value     值
	 * @return Boolean
	 */
	public static function toDelete($table = 'demand_version_detail', $fieldName = 'dvd_id', $type = '=', $value = 0) {
		if (empty($value)) {
			return false;
		}
		return DB::table($table)->where($fieldName, $type, $value)->delete();
    }
}

你可能感兴趣的:(Laravel)