本文整理汇总了PHP中app\Tag::firstOrNew方法的典型用法代码示例。如果您正苦于以下问题:PHP Tag::firstOrNew方法的具体用法?PHP Tag::firstOrNew怎么用?PHP Tag::firstOrNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Tag
的用法示例。
在下文中一共展示了Tag::firstOrNew方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addTags
public function addTags($lead_id, Request $tags)
{
foreach ($tags as $tag) {
$item = Tag::firstOrNew(['name' => $tag, 'lead_id' => $lead_id]);
}
return \Response::make('OK', 200);
}
示例2: postEdit
public function postEdit(Request $request)
{
$userID = Auth::user()->id;
//Make sure title and tags are provided
$validator = $this->editValidator($request->all());
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
$tags = explode(",", $request->tags);
try {
//Find the bookmark with ID=$id
$bookmark = Bookmark::findOrFail($request->id);
if ($bookmark->owner_id != $userID) {
return "This is not your bookmark";
}
//Update the bookmark's title
$bookmark->title = $request->page_title;
$bookmark->colour = $request->colour;
$bookmark->save();
//Now create a new tag object for each of the tags
//Only if it doesn't already exist in tags
foreach ($tags as $tag_name) {
// Retrieve the tag by the attributes, or instantiate a new instance...
$tag = Tag::firstOrNew(array('tag_name' => $tag_name));
$tag->save();
$bookmarkTag = BookmarkTag::firstOrNew(array('bookmark_id' => $bookmark->bookmark_id, 'tag_id' => $tag->tag_id));
$bookmarkTag->save();
}
} catch (ModelNotFoundException $e) {
return "Bookmark not found: " . $e->getMessage();
}
return redirect($this->bookmarksPath);
}
示例3: update
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$token = $request->header('token');
$user = $this->api ? User::where('token', $token)->first() : Auth::user();
$userID = isset($user) ? $user->id : 0;
if (!$userID) {
return $this->api ? response()->json(['data' => ['message' => 'Not logged in']], 401) : redirect(route('login'));
}
$post = PostBlog::whereRaw('id = ? AND userID = ?', [$id, $userID])->first();
$fields = $request->all();
$validator = Validator::make($fields, ['name' => 'required', 'description' => 'required', 'text' => 'required']);
if ($validator->fails()) {
if (!$this->api) {
return redirect(route('edit', ['id' => $id]))->withErrors($validator);
} else {
return response()->json(['data' => ['errors' => $validator->errors()->getMessages()]], 400);
}
}
$post->userID = $userID;
$post->name = $fields['name'];
$post->description = $fields['description'];
$post->text = $fields['text'];
$post->active = isset($fields['active']) ? 1 : 0;
$post->save();
$tags = explode(',', $fields['tags']);
foreach ($tags as $tag) {
$tagObj = Tag::firstOrNew(['tagName' => $tag]);
$tagObj->save();
PostBlogTag::firstOrNew(['postID' => $post->id, 'tagID' => $tagObj->id])->save();
}
return $this->api ? response()->json(['data' => ['message' => 'Edit successfully']], 200) : redirect(route('home'));
}