本文整理汇总了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);
}