当前位置: 首页>>代码示例>>PHP>>正文


PHP Tag::firstOrCreate方法代码示例

本文整理汇总了PHP中Tag::firstOrCreate方法的典型用法代码示例。如果您正苦于以下问题:PHP Tag::firstOrCreate方法的具体用法?PHP Tag::firstOrCreate怎么用?PHP Tag::firstOrCreate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tag的用法示例。


在下文中一共展示了Tag::firstOrCreate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: store

 /**
  * Store a newly created post in storage.
  *
  * @return Response
  */
 public function store()
 {
     $validate = Validator::make(Input::all(), Question::$rules);
     if ($validate->passes()) {
         //save a new Question
         $question = new Question();
         $question->title = Input::get('title');
         $question->body = Input::get('body');
         $question->user_id = Auth::id();
         $question->save();
         $question_id = $question->id;
         //saving Tags in Tag Table
         /*	convert input to array 	*/
         $tags_arr = explode(',', Input::get('tag'));
         /*	
         save in Tag table and return object for saving in 
         Tagmap table
         */
         foreach ($tags_arr as $tag_str) {
             $tag_obj = Tag::firstOrCreate(array('title' => $tag_str));
             //this line will attach a tag ( insert and save automatically )
             $new_question->tags()->attach($tag_obj->id);
         }
         return Redirect::action('QuestionController@index');
     }
     return Redirect::back()->withErrors($validate)->withInput();
 }
开发者ID:ayooby,项目名称:whatisit,代码行数:32,代码来源:QuestionController.php

示例2: getTagIds

 public static function getTagIds($tags)
 {
     if (!is_array($tags)) {
         throw new TurnbackException('Tags incorrectas.');
     }
     $vdt = new Validate\Validator();
     $vdt->addRule('tags', new Validate\Rule\AlphaNumeric([' ']))->addRule('tags', new Validate\Rule\MinLength(2))->addRule('tags', new Validate\Rule\MaxLength(32));
     if (!$vdt->validate(['tags' => $tags])) {
         throw new TurnbackException($vdt->getErrors());
     } else {
         if (count($tags) > 8) {
             throw new TurnbackException('No pueden asignarse más de 8 tags.');
         }
     }
     $tagIds = array();
     foreach ($tags as $tag) {
         $tagIds[] = Tag::firstOrCreate(['nombre' => FilterFactory::normalizeWhitespace($tag)])->id;
     }
     return $tagIds;
 }
开发者ID:DiegoVI,项目名称:virtuagora,代码行数:20,代码来源:TagCtrl.php

示例3: store

 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     // create the validator
     $validator = Validator::make(Input::all(), Post::$rules);
     // attempt validation
     if ($validator->fails()) {
         Session::flash('errorMessage', 'Something went wrong.');
         return Redirect::back()->withInput()->withErrors($validator);
     } else {
         $post = new Post();
         $title = Input::get('title');
         $body = Input::get('body');
         if (Request::hasFile('file')) {
             $img = Imageupload::upload(Request::file('file'));
             $post->img_url = $img['filename'];
         }
         $post->title = $title;
         $post->body = $body;
         $post->user_id = Auth::id();
         $post->save();
         $post_id = $post->id;
         $tags = Input::get('tags');
         if (isset($tags)) {
             $tags = strtolower(Input::get('tags'));
             $tags = explode(', ', $tags);
             foreach ($tags as $tag) {
                 $tag = trim($tag);
                 $tag_id = Tag::firstOrCreate(array('name' => $tag));
                 $post->tags()->attach($tag_id, array('post_id' => $post_id));
             }
         }
         Log::info('Success: ', ['title' => $post->title, 'body' => $post->body]);
         Session::flash('successMessage', 'You created a post successfully');
         return Redirect::action('PostsController@index');
     }
 }
开发者ID:j-womack,项目名称:myblog,代码行数:41,代码来源:PostsController.php

示例4: postEditPost

 public function postEditPost($id)
 {
     $input = Input::all();
     $validator = Post::validate($input);
     if ($validator->fails()) {
         FlashHelper::message("Null title", FlashHelper::DANGER);
         return Redirect::to(URL::action('BlogController@getNewPost'))->withInput();
     }
     // Post
     $Parsedown = new Parsedown();
     $post = Post::findOrFail($id);
     if ($post->created_by != Auth::id()) {
         return View::make('error.Unauthorized');
     }
     $post->title = Input::get('title');
     $post->makrdown = Input::get('makrdown');
     if (!is_null($post->makrdown)) {
         $post->HTML = $Parsedown->text($post->makrdown);
     }
     $post->privacy_level = Input::get('privacy_level');
     $post->can_comment = Input::get('can_comment');
     // tags
     $topics = trim(Input::get('tags'));
     if ($topics != "") {
         $topics = explode(";", $topics);
         $tagIds = array();
         // Topic Tags
         foreach ($topics as $topic) {
             $tag = Tag::firstOrCreate(array('name' => trim($topic)));
             $tagIds[] = $tag->id;
         }
         // post <=> Tags
         $post->tags()->sync($tagIds);
     }
     // privacy
     if ($post->privacy_level == Post::_CUSTOM) {
         $friends = trim(Input::get('friends'));
         if ($friends == "") {
             $post->privacy_level = Post::_PRIVATE;
         } else {
             $friends = explode(",", $friends);
             $post->friends()->sync($friends);
         }
     }
     $post->save();
     return Redirect::to($post->getShowLink());
 }
开发者ID:sh1nu11bi,项目名称:CloMa,代码行数:47,代码来源:BlogController.php

示例5: setTagListAttribute

 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function setTagListAttribute($value, $post_id)
 {
     $post = Post::find($post_id);
     $tagIds = [];
     $tags = explode(',', $value);
     foreach ($tags as $tagName) {
         /* firstOrCreate uses first instance or creates a new instantiation - 
         			stops tags table from duplicating tag names*/
         $tag = Tag::firstOrCreate(array('name' => $tagName));
         $tagIds[] = $tag->id;
         /* sync is a Laravel method to attach related models; 
         			sync accepts array of ids to be placed on pivot table
         			only the ids in the array will be on the intermediate table post_tag_table.php*/
         $post->tags()->sync($tagIds);
     }
 }
开发者ID:JBSanAntonio,项目名称:LaravelBlog,代码行数:22,代码来源:PostsController.php

示例6: createTag

 /**
  * @param $title
  * @return \Tag
  */
 protected function createTag($title)
 {
     $tagCreate = \Tag::firstOrCreate(['title' => $title, 'user_id' => \Auth::user()->id, 'visibility' => 0]);
     return $tagCreate;
 }
开发者ID:Liongold,项目名称:paperwork,代码行数:9,代码来源:AbstractImport.php

示例7: upload

 public function upload()
 {
     if (!file_exists($_ENV['MALWARE_STORAGE_PATH'])) {
         mkdir($_ENV['MALWARE_STORAGE_PATH'], 0777, true);
     }
     $input = Input::all();
     $files = Input::file();
     foreach ($files as $file) {
         try {
             $filename = md5_file($file);
             $sample = Sample::firstOrNew(array('md5' => $filename));
             $sample->filename = $file->getClientOriginalName();
             $sample->filesize = $file->getSize();
             $file->move($_ENV['MALWARE_STORAGE_PATH'], $filename);
             if (isset($input['VTscan'])) {
                 $this->scanFileOnVirusTotal($sample, TRUE);
             } else {
                 $this->scanFileOnVirusTotal($sample, FALSE);
             }
             $sample->save();
             // tags processing
             $tag_name_str = 'tag_' . preg_replace('/[. ]/', "_", $sample->filename);
             if (isset($input[$tag_name_str])) {
                 $tags = trim($input[$tag_name_str]);
                 if ($tags != "") {
                     $tags = explode(";", $tags);
                     $tagIds = array();
                     // Tags
                     foreach ($tags as $tag) {
                         $tag = Tag::firstOrCreate(array('tag' => $tag));
                         $tagIds[] = $tag->id;
                     }
                     // Sample <=> Tags
                     $sample->tags()->sync($tagIds);
                 }
             }
             // writeups processing
             $writeup_name_str = 'writeup_' . preg_replace('/[. ]/', "_", $sample->filename);
             if (isset($input[$writeup_name_str])) {
                 $writeups = $input[$writeup_name_str];
                 foreach ($writeups as $url) {
                     if ($url == "") {
                         continue;
                     }
                     $writeup = new Writeup();
                     $writeup->url = $url;
                     $writeup->title = $this->getUrlTitle($url);
                     $writeup->sample()->associate($sample);
                     $writeup->save();
                 }
             }
             $samples[] = $sample;
         } catch (Exception $e) {
             Log::info($e);
             Response::json("Error occured :( Try to upload again");
         }
     }
     return $sample;
 }
开发者ID:wxdublin,项目名称:malwaRE,代码行数:59,代码来源:HomeController.php

示例8: store

 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     // create the validator
     $validator = Validator::make(Input::all(), Post::$rules);
     // attempt validation
     if ($validator->fails()) {
         // validation failed, redirect to the post create page with validation errors and old inputs
         Session::flash('errorMessage', 'Your new post was not successfully created. See errors below:');
         //log error to app/storage/logs/laravel.log
         Log::info('Unsuccessful attempt to create blog post: ', Input::all());
         return Redirect::back()->withInput()->withErrors($validator);
     } else {
         // validation succeeded, create and save the post
         //check to see if there is a cover image
         if (Input::hasFile('img_url')) {
             Input::file('img_url')->move(public_path() . '/img', Input::file('img_url')->getClientOriginalName());
             $post->img_url = '/img/' . Input::file('img_url')->getClientOriginalName();
         }
         $post = new Post();
         $post->title = strtoupper(Input::get('title'));
         $post->body = Input::get('body');
         $post->user_id = Auth::id();
         $post->save();
         Session::flash('successMessage', 'Your new post titled "' . $post->title . '" was successfully created.');
         //check to see if there are any tags
         if (Input::has('tags')) {
             $tagsArray = explode(",", Input::get('tags'));
             foreach ($tagsArray as $tagValue) {
                 //firstOrCreate keeps multiple tags with the same name of being made
                 $tag = Tag::firstOrCreate(array('name' => $tagValue));
                 $tagIds[] = $tag->id;
                 //sync also removes tags (only accepts array)
                 $post->tags()->sync($tagIds);
             }
         }
     }
     return Redirect::action('PostsController@index');
 }
开发者ID:asteph,项目名称:Laravel_Blog,代码行数:43,代码来源:PostsController.php


注:本文中的Tag::firstOrCreate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。