本文整理汇总了PHP中app\models\Post::findOrFail方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::findOrFail方法的具体用法?PHP Post::findOrFail怎么用?PHP Post::findOrFail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Post
的用法示例。
在下文中一共展示了Post::findOrFail方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$post = Post::findOrFail($id);
$post->tags()->detach();
$post->delete();
return redirect()->route('admin.post.index')->withSuccess('Post deleted.');
}
示例2: update
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
$this->validate($request, ['title' => 'required', 'content' => 'required', 'author' => 'required']);
$input = $request->all();
$post->fill($input)->save();
return redirect()->route('posts.index');
}
示例3: show
public function show($id)
{
$post = Post::findOrFail($id);
// if($post==null){
// abort('404');
// }
return view('post.post')->with('post', $post);
}
示例4: fieldsFromModel
/**
* Return the field values from the model
*
* @param integer $id
* @param array $fields
* @return array
*/
protected function fieldsFromModel($id, array $fields)
{
$post = Post::findOrFail($id);
$fieldNames = array_keys(array_except($fields, ['tags']));
$fields = ['id' => $id];
foreach ($fieldNames as $field) {
$fields[$field] = $post->{$field};
}
$fields['tags'] = $post->tags()->lists('tag')->all();
return $fields;
}
示例5: destroyPost
public function destroyPost($id)
{
try {
$post = Post::findOrFail($id);
$post->tags()->detach();
$oldImage = public_path() . config('model.posts.path_folder_photo_post') . $post->images;
if (File::exists($oldImage)) {
File::delete($oldImage);
}
$post->delete();
} catch (Exception $e) {
throw new Exception("Error Processing Request", 1);
}
}
示例6: editPost
public function editPost()
{
$postValue = \Input::get('Post');
if (is_null($postValue)) {
$postValue = \Input::get('id');
$post = Post::findOrFail($postValue);
if ($post->user_id != \Auth::user()->id) {
return abort(403, 'Unauthorized');
}
return view('home.editPost', compact('post'));
} else {
$post = Post::findOrFail($postValue['id']);
$post->fill($postValue);
$post->save();
return redirect(route('home'));
}
}
示例7: show
/**
* Show a post
* @param integer $id id of a post
* @return Respos
*/
public function show($id)
{
try {
DB::beginTransaction();
$post = Post::findOrFail($id);
$nview = $post->nview;
if (is_string($nview)) {
$nview = (int) $nview;
}
$nview++;
$post->nview = $nview;
$post->save();
DB::commit();
//Related post
$relatedPost = Post::where('id', '!=', $id)->where('is_active', 1)->orderBy('created_at', 'desc')->take(5)->get();
$url = config('media.url');
return view('website.show', compact('post', 'relatedPost', 'url'));
} catch (ModelNotFoundException $e) {
DB::rollback();
$message = "Không tồn tại bài viết hoặc bài viết chưa active.";
$alertClass = "alert-danger";
return redirect()->back()->with(compact('message', 'alertClass'));
}
}
示例8: destroy
/**
* Remove the specified resource from storage.
*
* @param int $idx
* @return Response
*/
public function destroy($idx)
{
$this->checkParametersEmpty();
$comment = Post::findOrFail($idx);
$comment->delete();
return $this->getCodeResponse(Response::HTTP_NO_CONTENT);
}
示例9: destroy
/**
* Destroy a post if it exists.
*
* @param $id
* @return Response
*/
public function destroy($id)
{
$post = Post::findOrFail($id);
$post::destroy($id);
// Delete comments whos post_id matches the current post that is being deleted
Comment::where('post_id', '=', $id)->delete();
return redirect()->action('PostController@index');
}
示例10: find
/**
* Returns one post by id
* @param int $id
* @return Post
*/
public function find($id)
{
return Post::findOrFail($id);
}
示例11: show
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$post = Post::findOrFail($id);
return view('frontend.post.show', compact('post'));
}
示例12: seenPost
/**
* Switch status seen of a post
*/
public function seenPost($idPost)
{
if (Request::ajax()) {
DB::beginTransaction();
try {
$post = Post::findOrFail($idPost);
$post->seen = $post->seen ? 0 : 1;
$post->save();
} catch (ModelNotFoundException $e) {
DB::rollback();
return response()->json(['error' => true]);
}
DB::commit();
}
}
示例13: destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$comment = Post::findOrFail($id);
$comment->delete();
return $this->getCodeResponse(Response::HTTP_NO_CONTENT);
}
示例14: show
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$post = Post::findOrFail($id);
return view('site.blog.show')->with('post', $post);
}