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