本文整理汇总了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();
}
示例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;
}
示例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');
}
}
示例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());
}
示例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);
}
}
示例6: createTag
/**
* @param $title
* @return \Tag
*/
protected function createTag($title)
{
$tagCreate = \Tag::firstOrCreate(['title' => $title, 'user_id' => \Auth::user()->id, 'visibility' => 0]);
return $tagCreate;
}
示例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;
}
示例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');
}