當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Tag::firstOrCreate方法代碼示例

本文整理匯總了PHP中app\Tag::firstOrCreate方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tag::firstOrCreate方法的具體用法?PHP Tag::firstOrCreate怎麽用?PHP Tag::firstOrCreate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在app\Tag的用法示例。


在下文中一共展示了Tag::firstOrCreate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: newTweet

 public function newTweet(Request $request)
 {
     $this->validate($request, ['content' => 'required|max:140']);
     $newTweet = new Tweet();
     $newTweet->content = $request->content;
     $newTweet->user_id = \Auth::user()->id;
     $newTweet->save();
     //Process Tags
     $tags = explode('#', $request->tags);
     $tagsFormatted = [];
     //clean up the tags
     foreach ($tags as $tag) {
         if (trim($tag)) {
             $tagsFormatted[] = strtolower(trim($tag));
         }
     }
     //Loop over each tag
     foreach ($tagsFormatted as $tag) {
         //Grab the first matching result OR insert this new unique tag
         $theTag = Tag::firstOrCreate(['name' => $tag]);
         $allTagIds[] = $theTag->id;
     }
     //Attacth the Tag IDs to the tweet
     $newTweet->tags()->attach($allTagIds);
     return redirect('profile');
 }
開發者ID:RichardHpa,項目名稱:twitter-clone,代碼行數:26,代碼來源:ProfileController.php

示例2: newTweet

 public function newTweet(Request $request)
 {
     $this->validate($request, ['content' => 'required|min:2|max:140']);
     $newTweet = new Tweet();
     // point to columns in db
     $newTweet->content = $request->content;
     // user who is logged in take their id
     $newTweet->user_id = \Auth::user()->id;
     // Save into database
     $newTweet->save();
     // Process the tags
     $tags = explode('#', $request->tags);
     $tagsFormatted = [];
     // clean up tags, remove white spaces
     foreach ($tags as $tag) {
         // if after trimming something remains
         if (trim($tag)) {
             $tagsFormatted[] = strtolower(trim($tag));
         }
     }
     $allTagIds = [];
     // Loop over eah tag
     foreach ($tagsFormatted as $tag) {
         // Grab the first matching result or insert this new unique tag
         // if tag is present will not insert into db. If not present, inserts into db.
         $theTag = Tag::firstOrCreate(['name' => $tag]);
         $allTagIds[] = $theTag->id;
     }
     // Attach the tag ids to the tweet
     $newTweet->tags()->attach($allTagIds);
     return redirect('profile');
 }
開發者ID:sarahkirby,項目名稱:twitter-clone,代碼行數:32,代碼來源:ProfileController.php

示例3: updatePost

 public function updatePost(Request $request, $post_id)
 {
     $new_post = $request->only('title', 'body', 'visible');
     $new_tags = $request->only('tags');
     // base64 encode to avoid dealing with HTML entities
     $new_post['body'] = base64_encode($new_post['body']);
     // set visible
     $new_post['visible'] = is_null($new_post['visible']) ? true : false;
     \DB::transaction(function () use($post_id, $new_post, $new_tags) {
         $post = Post::with('tags')->where('id', '=', $post_id)->first();
         if (!$post->tags->isEmpty()) {
             // remove all previous tags
             foreach ($post->tags as $tag) {
                 $post->tags()->detach($tag);
             }
         }
         // update the old post
         $post->update($new_post);
         // add the tags
         if (!empty($new_tags['tags'])) {
             $new_tags = explode(',', $new_tags['tags']);
             foreach ($new_tags as $key => $tag) {
                 $new_tags[$key] = $tag = trim($tag);
                 if (!empty($tag)) {
                     $post->tags()->save(Tag::firstOrCreate(['name' => $tag]));
                 }
             }
         }
         $post->save();
     });
     return redirect(route('admin'));
 }
開發者ID:andycrockett,項目名稱:andycrockett,代碼行數:32,代碼來源:AdminController.php

示例4: boot

 public static function boot()
 {
     parent::creating(function ($m) {
         $m->id = sha1(str_random(128));
     });
     parent::created(function ($m) {
         preg_match_all("/(^|[^a-zA-Z0-9])@([a-zA-Z0-9_])+/i", $m->conteudo, $matches);
         $locais = array();
         if (!empty($matches[0])) {
             foreach ($matches[0] as $match) {
                 array_push($locais, preg_replace('/[^a-zA-Z0-9_]/i', "", $match));
             }
         }
         preg_match_all("/(^|[^a-zA-Z0-9])#([a-zA-Z0-9_])+/i", $m->conteudo, $matches);
         $tags = array();
         if (!empty($matches[0])) {
             foreach ($matches[0] as $match) {
                 array_push($tags, preg_replace('/[^a-zA-Z0-9_]/i', "", $match));
             }
         }
         foreach ($tags as $tag) {
             $t = \App\Tag::firstOrCreate(['tag' => $tag]);
             $m->tags()->save($t);
         }
         foreach ($locais as $local) {
             $l = \App\Local::firstOrCreate(['local' => $local]);
             $m->locais()->save($l);
         }
     });
 }
開發者ID:edsonmichaque,項目名稱:grafite-ng,代碼行數:30,代碼來源:Mensagem.php

示例5: tagIDs

 private function tagIDs($listaTags)
 {
     $tags = array_filter(array_map('trim', explode(",", $listaTags)));
     $tags_ids = [];
     foreach ($tags as $tag) {
         $tags_ids[] = Tag::firstOrCreate(['name' => $tag])->id;
     }
     return $tags_ids;
 }
開發者ID:rpc1910,項目名稱:LaravelBlog,代碼行數:9,代碼來源:PostsAdminController.php

示例6: getTagsIds

 private function getTagsIds($tags)
 {
     $tagList = array_filter(array_map('trim', explode(',', $tags)));
     $tagsIDs = [];
     foreach ($tagList as $tagName) {
         $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id;
     }
     return $tagsIDs;
 }
開發者ID:ricardovicente,項目名稱:laravel-express,代碼行數:9,代碼來源:PostAdminController.php

示例7: getTagsIds

 private function getTagsIds($tagsRequest)
 {
     $tags = array_filter(array_map('trim', explode(',', $tagsRequest)));
     $tagsIds = [];
     foreach ($tags as $eachTagName) {
         $tagsIds[] = Tag::firstOrCreate(['name' => $eachTagName])->id;
     }
     return $tagsIds;
 }
開發者ID:kzoty,項目名稱:blog-laravel,代碼行數:9,代碼來源:PostsAdminController.php

示例8: syncTags

 /**
  * Sync up the list of tags in the database.
  *
  * @param Article $article
  * @param array   $tags
  */
 private function syncTags(Question $question, array $tags)
 {
     // Create new tags if needed and get ids
     $tagIds = [];
     foreach ($tags as $tag) {
         $tagId = Tag::firstOrCreate(['name' => mb_strtolower($tag, 'UTF-8')])->id;
         $tagIds[] = $tagId;
     }
     // Sync tags based on ids
     $question->tags()->sync($tagIds);
 }
開發者ID:nilstr,項目名稱:stackoverflow-clone,代碼行數:17,代碼來源:QuestionsController.php

示例9: createAndReturnArrayOfTagIds

 public static function createAndReturnArrayOfTagIds($string, $delimiter = ',')
 {
     $tagsArray = explode($delimiter, $string);
     $ids = [];
     foreach ($tagsArray as $tag) {
         $tag = trim($tag);
         $theTag = \App\Tag::firstOrCreate(['title' => $tag]);
         array_push($ids, $theTag->id);
     }
     return $ids;
 }
開發者ID:hugoleodev,項目名稱:WhatTheTag,代碼行數:11,代碼來源:Tag.php

示例10: getTagsIds

 private function getTagsIds($tags)
 {
     $tagList = array_filter(array_map("trim", explode(",", $tags)));
     $tagsIDs = [];
     foreach ($tagList as $tagName) {
         if ($tagName != "") {
             $tagsIDs[] = Tag::firstOrCreate(["name" => $tagName])->id;
         }
     }
     return $tagsIDs;
 }
開發者ID:stanleygomesdasilva,項目名稱:blog-laravel-5.1,代碼行數:11,代碼來源:PostsAdminController.php

示例11: update

 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $post = Post::find($id);
     $post->fill($request->input())->save();
     $tags = tags_to_array($request->get('tags'));
     foreach ($tags as $tag) {
         $tag = Tag::firstOrCreate(['name' => $tag]);
         $post->tags()->detach($tag);
         $post->tags()->attach($tag);
     }
     return redirect(route('admin.posts'));
 }
開發者ID:Philwn,項目名稱:Laravel-AdminLTE,代碼行數:19,代碼來源:PostController.php

示例12: verifyTags

 /**
  * Verifica los tag en el arreglo ( si no existe lo crea) no esta funcionado actualmente
  * @param $tagsArray
  */
 private function verifyTags($tagsArray)
 {
     $tagIds = [];
     foreach ($tagsArray as $tag) {
         $tag = trim($tag);
         if ($tag == '') {
             continue;
         }
         $fTag = Tag::firstOrCreate(['id' => $tag]);
         $tagIds[] = $fTag->id;
     }
 }
開發者ID:alons182,項目名稱:guanacastevende,代碼行數:16,代碼來源:ProductRepository.php

示例13: getTagsIds

 private function getTagsIds($tags)
 {
     $tagsList = array_filter(array_map('trim', explode(',', $tags)));
     $tagsIDs = [];
     /*
       explode = funcao em php para colocar os dados em um array ex: oi, mundo = array[0 => "oi", 1 => " mundo"]
                 array_map + trim = funcao em php para ignorar os espacos de cada dado ex: array[0 => "oi", 1 => "mundo"]
                 array_filter = funcao em php para igonar um dado vazio ex: oi, ,mundo = array[0 => "oi", 1 => " ", 2 => " mundo"]
                 usando a funcao: array[0 => "oi", 1 => " mundo"]
     */
     foreach ($tagsList as $tagName) {
         $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id;
     }
     return $tagsIDs;
 }
開發者ID:HugoTargino,項目名稱:laravel-express,代碼行數:15,代碼來源:PostsAdminController.php

示例14: update

 public function update($data, $slug)
 {
     $tag_ids = array();
     if (isset($data['tags'])) {
         !is_array($data['tags']) ? $data['tags'] = explode(',', str_replace(" ", "", $data['tags'])) : null;
         foreach ($data['tags'] as $key => $tagname) {
             $tag_ids[$key] = Tag::firstOrCreate(['name' => $tagname])['id'];
         }
     }
     $post = Post::where('slug', $slug)->first();
     $post->fill(array('title' => $data['title'], 'body' => $data['body'], 'video_id' => $data['video_id']))->save();
     $post->tags()->detach();
     $post->tags()->attach($tag_ids);
     return $this->getBySlug($post->slug);
     // Publish event 'post_updated': Redis will tell NodeJS, who will tell the users
 }
開發者ID:emeyer161,項目名稱:TheThirstyTerp,代碼行數:16,代碼來源:PostsRepository.php

示例15: attach

 public function attach(Request $request)
 {
     if ($request->has('video')) {
         $video = Video::firstOrCreate(['title' => $request->input('video')]);
     }
     if ($request->has('video_id')) {
         $video = Video::findOrFail($request->input('video_id'));
     }
     if ($request->has('tags')) {
         foreach ($request->input('tags') as $tag) {
             $tag = Tag::firstOrCreate(['tag' => $tag]);
             $video->tags()->sync([$tag->id], false);
         }
     }
     if ($request->has('tag')) {
         $tag = Tag::firstOrCreate(['tag' => $request->input('tag')]);
         $video->tags()->sync([$tag->id], false);
     }
 }
開發者ID:andrewhood125,項目名稱:super-potato,代碼行數:19,代碼來源:VideosController.php


注:本文中的app\Tag::firstOrCreate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。