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