本文整理汇总了PHP中app\Category::firstOrCreate方法的典型用法代码示例。如果您正苦于以下问题:PHP Category::firstOrCreate方法的具体用法?PHP Category::firstOrCreate怎么用?PHP Category::firstOrCreate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Category
的用法示例。
在下文中一共展示了Category::firstOrCreate方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
DB::table('divisions')->truncate();
DB::table('categories')->truncate();
DB::table('sub_categories')->truncate();
DB::table('brands')->truncate();
DB::table('skus')->truncate();
$reader = ReaderFactory::create(Type::XLSX);
// for XLSX files
$filePath = 'database/seeds/seed_files/Items.xlsx';
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
if ($sheet->getName() == 'SKU Data') {
$rowcnt = 0;
foreach ($sheet->getRowIterator() as $row) {
if ($rowcnt > 1) {
if (!is_null($row[0])) {
$division = Division::firstOrCreate(['division' => $row[8]]);
$category = Category::firstOrCreate(['category_short' => strtoupper($row[1]), 'category_long' => strtoupper($row[0])]);
$sub_category = SubCategory::firstOrCreate(['subcategory' => strtoupper($row[6])]);
$brand = Brand::firstOrCreate(['brand' => strtoupper($row[7])]);
$sku = Sku::firstOrCreate(['division_id' => $division->id, 'category_id' => $category->id, 'sub_category_id' => $sub_category->id, 'brand_id' => $brand->id, 'sku_code' => $row[2], 'item_desc' => $row[3], 'sku_desc' => $row[4], 'conversion' => $row[5]]);
}
}
$rowcnt++;
}
}
}
$reader->close();
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
Model::reguard();
}
示例2: postCategoryCreate
public function postCategoryCreate(Request $request)
{
$category = Category::firstOrCreate($request->only('name'));
$category->description = $request->description ?: null;
$category->save();
return redirect()->back();
}
示例3: parseExcelData
/**
* Parse excel data and insert it in database
*
* @param array $excelData
* @return void
*/
protected function parseExcelData($excelData)
{
foreach ($excelData as $key => $data) {
/* Skip first row */
if (1 == $key) {
continue;
}
$Category = Category::firstOrCreate(['parent_id' => null, 'name' => $data['A']]);
$SubCategory = Category::firstOrCreate(['parent_id' => (int) $Category->id, 'name' => $data['B']]);
Catalog::create(['category_id' => $SubCategory->id, 'part_number' => $data['C'], 'description' => $data['D']]);
}
}
示例4: store
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $request->except('_token');
$category = Category::firstOrCreate(['name' => $data['name']]);
$category->description = $data['description'];
$category->type = $data['type'];
$category->status = (int) $data['status'];
$path = 'images/categories/';
$destinationPath = public_path($path);
if ($request->hasFile('image_url')) {
$name = $request->file('image_url')->getClientOriginalName();
$request->file('image_url')->move($destinationPath, $name);
$category->image_url = $path . $name;
}
$category->save();
return redirect()->route('admin.categories.show', $category->id)->with(['message' => 'success!']);
}
示例5: prepareJobRecord
private static function prepareJobRecord($data)
{
$currentRecord = [];
// VALIDATE DATA
if (empty($data->external_job_id)) {
ExportRecordFailure::create(['job_info' => json_encode($data), 'reason' => '[ external_job_id is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]);
return null;
}
if (empty($data->job_title)) {
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ title is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]);
return null;
}
$end_date = null;
if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_end_date, $end_date_matches)) {
$end_date_mm = $end_date_matches[2];
$end_date_dd = $end_date_matches[3];
$end_date_yyyy = $end_date_matches[1];
$end_date = $end_date_yyyy . '-' . $end_date_mm . "-" . $end_date_dd;
}
if (empty($end_date)) {
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ end date is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]);
return null;
} else {
$jobObj = Job::firstOrNew(['external_job_id' => $data->external_job_id, 'source_id' => 1]);
if (strtotime($end_date) < strtotime(date('Y-m-d'))) {
if (!empty($jobObj->id)) {
$record_id = $jobObj->id;
$jobObj->delete();
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ]', 'action' => '[ record deleted ][ record id: ' . $record_id . ' ] [ exiting process ]', 'calling_function' => __FUNCTION__]);
return $record_id;
} else {
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ] ', 'action' => '[ will not create a record ] [ exiting process ]', 'calling_function' => __FUNCTION__]);
$jobObj = null;
return 1;
}
}
}
if (!empty($data->job_state) && $data->job_state == 'expired') {
if (!empty($jobObj->id)) {
$record_id = $jobObj->id;
$jobObj->delete();
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ]', 'action' => '[ record deleted ][ record id: ' . $record_id . ' ] [ exiting process ]', 'calling_function' => __FUNCTION__]);
return $record_id;
} else {
ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ] ', 'action' => '[ will not create a record ] [ exiting process ]', 'calling_function' => __FUNCTION__]);
$jobObj = null;
return 1;
}
}
// END OF DATA VALIDATION
if (!empty($jobObj->id)) {
$currentRecord = Job::getInfo($jobObj->id, ['JobState', 'JobCity', 'JobCompany', 'JobCategory', 'JobIndustry']);
}
$jobObj->source_id = 1;
$jobObj->external_job_id = $data->external_job_id;
if (!empty($data->job_name)) {
$jobObj->name = self::cleanUpStr($data->job_name);
}
if (!empty($data->job_title)) {
$jobObj->title = self::cleanUpStr($data->job_title);
}
if (!empty($data->org_name)) {
$company_name = trim(str_replace("'", "'", $data->org_name));
} else {
$company_name = 'Confidential';
}
if (empty($currentRecord) || $currentRecord['JobCompany'] != $company_name) {
$company_obj = Company::saveInstance(['name' => $company_name]);
$jobObj->company_id = $company_obj->id;
}
$jobObj->apply_url = 'http://jobview.monster.com/getjob.aspx?jobid=' . $data->external_job_id;
if ($company_name == 'Comcast Cable Communications Management, LLC') {
if (!empty($data->apply_url)) {
$jobObj->apply_url = $data->apply_url;
}
}
if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_begin_date, $begin_date_matches)) {
$begin_date_mm = $begin_date_matches[2];
$begin_date_dd = $begin_date_matches[3];
$begin_date_yyyy = $begin_date_matches[1];
$jobObj->begin_date = $begin_date_yyyy . '-' . $begin_date_mm . '-' . $begin_date_dd;
}
$jobObj->end_date = $end_date;
if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_post_date, $post_date_matches)) {
$post_date_mm = $post_date_matches[2];
$post_date_dd = $post_date_matches[3];
$post_date_yyyy = $post_date_matches[1];
$jobObj->post_date = $post_date_yyyy . '-' . $post_date_mm . '-' . $post_date_dd;
}
if (!empty($data->job_category)) {
if (empty($currentRecord) || $currentRecord['JobCategory'] != $data->job_category) {
$cat_obj = Category::firstOrCreate(['name' => $data->job_category]);
$jobObj->category_id = $cat_obj->id;
}
}
if (!empty($data->job_industries[0])) {
if (empty($currentRecord) || $currentRecord['JobIndustry'] != $data->job_industries[0]) {
$ind_obj = Industry::firstOrCreate(['name' => $data->job_industries[0]]);
$jobObj->industry_id = $ind_obj->id;
}
//.........这里部分代码省略.........
示例6: update
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id, Request $request)
{
$book = \App\Book::findOrFail($id);
// $book->update(\Input::all());
// return \Redirect::back()->with('message','Book updated.');
// if($book){
// $rules = \App\Book::$rules;
//$rules['name'] = 'required|min:2';
// $validator = \Validator::make(\Input::all(), $rules);
// if($validator->passes())
// {
$book->description = $request->description;
$book->publisher_id = \App\Publisher::firstOrCreate(['name' => ucfirst($request->publisher)])->id;
$book->category_id = \App\Category::firstOrCreate(['name' => ucfirst($request->category)])->id;
$book->language_id = \App\Language::firstOrCreate(['name' => ucfirst($request->language)])->id;
$book->release_date = $request->release_date;
$book->save();
//going over author names build up array of author ids by getting author ids and creating author if does not exist
$authorIds = [];
$authors = $request->authors;
if ($authors) {
foreach ($authors as $author) {
$authorIds[] = App\Author::firstOrCreate(['name' => ucfirst($author)])->id;
}
//atached book to authors
$book->authors()->sync($authorIds);
// $book->authors()->attach($authorIds);
}
if ($request->hasFile('image')) {
$this->setBookImage($book, $request);
}
flash('Book updated.');
return \Redirect::back();
// }
// return \Redirect::back()
// //->with('message','There were some errors. Please try again later..')
// ->withInput()
// ->withErrors($validator);
// }
//
// flash()->error('Book does not exist.');
// return \Redirect::back();
}
示例7: update
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(ArticleRequest $request, Guard $auth, $slug)
{
// find or create category
$category = Category::firstOrCreate(['name' => e($request->input('category')), 'slug' => e(str_slug(pinyin($request->input('category')))), 'user_id' => $auth->user()->id]);
// find or create tags
foreach ($request->input('tags') as $tag) {
$tagids[] = Tag::firstOrCreate(['name' => e($tag), 'slug' => e(str_slug(pinyin($tag))), 'user_id' => $auth->user()->id])->id;
}
// create article
$article = Article::where('slug', $slug)->firstOrFail();
$article->fill($request->all());
$article->slug = empty($article->slug) ? str_slug(pinyin($article->title)) : $article->slug;
$article->cover = $this->saveCover($request, $article);
$article->user_id = $auth->user()->id;
$article->category_id = $category->id;
$article->public = $request->has('public') ? 1 : 0;
$article->published_at = Carbon::now();
$article->update();
$article->tags()->sync($tagids);
flash()->success(trans('flash_messages.article_update_success'));
return redirect('admin/articles/hub');
}