本文整理匯總了PHP中app\Post::categories方法的典型用法代碼示例。如果您正苦於以下問題:PHP Post::categories方法的具體用法?PHP Post::categories怎麽用?PHP Post::categories使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app\Post
的用法示例。
在下文中一共展示了Post::categories方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: store
public function store(PostFormRequest $request)
{
$post = new Post(['title' => $request->get('title'), 'content' => $request->get('content'), 'slug' => Str::slug($request->get('title'), '-')]);
$post->save();
$post->categories()->sync($request->get('categories'));
return redirect('/admin/posts/create')->with('status', 'The post has been created!');
}
示例2: removeCategory
/**
* Remove given category from given post.
*
* @param Post $post
* @param Category $category
* @return void
* @throws NotFoundHttpException
*/
public function removeCategory(Post $post, Category $category)
{
if (!$post->categories->contains($category->id)) {
throw new NotFoundHttpException("Category doesn't exist on Post #" . $post->id);
}
$post->categories()->detach($category->id);
}
示例3: store
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//handel the request
$validator = Validator::make($request->all(), ['title' => 'required|unique:posts|max:67', 'image' => 'required|image|mimes:jpeg,png|max:5000']);
if ($validator->fails()) {
return redirect('seodashboard/posts/create')->withErrors($validator)->withInput();
}
$post = new Post();
$post->title = $request->input('title');
$post->content = $request->input('content');
$post->published = $request->input('status');
$post->excrypt = str_limit($request->input('excrypt'), 155);
$tags = $request->input('tags');
$categories = $request->input('categories');
$tags = $this->ConvertTagsToList($tags);
//save image path in database
if ($request->hasFile('image')) {
$destinationPath = 'images';
$file = $request->file('image');
$fileName = $file->getClientOriginalName();
$file->move($destinationPath, $fileName);
$post->image = $fileName;
}
$post->save();
$post->tags()->attach($tags);
$post->categories()->attach($categories);
return redirect('seodashboard/posts');
}