本文整理匯總了PHP中app\models\Tag::findornew方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tag::findornew方法的具體用法?PHP Tag::findornew怎麽用?PHP Tag::findornew使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app\models\Tag
的用法示例。
在下文中一共展示了Tag::findornew方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: store
/**
* Store a cluster
*
* @param type
* @return Response
*/
public function store($type = null)
{
if (!Input::has('category') && !Input::has('tag')) {
return new JSend('error', (array) Input::all(), 'Tidak ada data cluster.');
}
$errors = new MessageBag();
DB::beginTransaction();
//1. Validate Cluster Parameter
if ($type == 'category') {
$cluster = Input::get('category');
$cluster_data = \App\Models\Category::findornew($cluster['id']);
} else {
$cluster = Input::get('tag');
$cluster_data = \App\Models\Tag::findornew($cluster['id']);
}
//1a. Get original data
if (is_null($cluster['id'])) {
$is_new = true;
} else {
$is_new = false;
}
$cluster_rules = ['category_id' => 'numeric|exists:categories,id', 'name' => 'required|max:255', 'slug' => 'max:255|unique:categories,slug,' . (!is_null($cluster['id']) ? $cluster['id'] : '')];
//1b. Validate Basic Cluster Parameter
$validator = Validator::make($cluster, $cluster_rules);
if (!$validator->passes()) {
$errors->add('Cluster', $validator->errors());
} else {
//if validator passed, save cluster
$cluster_data = $cluster_data->fill(['name' => $cluster['name'], 'type' => $cluster_data->type, 'category_id' => isset($cluster['category_id']) ? $cluster['category_id'] : 0]);
if (!$cluster_data->save()) {
$errors->add('Cluster', $cluster_data->getError());
}
}
//End of validate cluster
if ($errors->count()) {
DB::rollback();
return new JSend('error', (array) Input::all(), $errors);
}
DB::commit();
$final_cluster = \App\Models\Cluster::id($cluster_data['id'])->with(['category', 'products'])->first()->toArray();
return new JSend('success', (array) $final_cluster);
}