本文整理汇总了PHP中app\Question::tags方法的典型用法代码示例。如果您正苦于以下问题:PHP Question::tags方法的具体用法?PHP Question::tags怎么用?PHP Question::tags使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Question
的用法示例。
在下文中一共展示了Question::tags方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: update
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Question $question
* @return Response
*/
public function update(Request $request, Question $question)
{
if ($question->user->id != Auth::user()->id) {
return Redirect::route("home");
}
$rules = ["title" => "required", "tags" => "required", "question" => "required"];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return Redirect::route("ask.edit", $question->id)->withErrors($validator)->withInput();
}
$question->title = $request->input("title");
$question->slug = Str::slug($request->input("title"));
$question->question = $request->input("question");
$question->save();
$tags = [];
foreach (explode(",", $request->input("tags")) as $tagtext) {
if ($tag = Tag::where("name", $tagtext)->first()) {
$tags[] = $tag->id;
} else {
$tag = new Tag();
$tag->name = $tagtext;
$tag->save();
$tags[] = $tag->id;
}
}
$question->tags()->sync($tags);
return Redirect::route("ask.show", [$question->id, $question->slug])->with("success", "Soru başarıyla oluşturuldu!");
}