當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Posts::findOrFail方法代碼示例

本文整理匯總了PHP中app\Posts::findOrFail方法的典型用法代碼示例。如果您正苦於以下問題:PHP Posts::findOrFail方法的具體用法?PHP Posts::findOrFail怎麽用?PHP Posts::findOrFail使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在app\Posts的用法示例。


在下文中一共展示了Posts::findOrFail方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: update

 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Requests\PostRequest $request, $id)
 {
     $post = Posts::where('id', $id)->firstOrFail();
     if (!empty($request->file('image')) && $request->file('image')->isValid()) {
         $destinationPath = public_path() . "/images/";
         // upload path
         $extension = $request->file('image')->getClientOriginalExtension();
         // getting image extension
         $fileName = $post->url . '.' . $extension;
         // renameing image
         $request->file('image')->move($destinationPath, $fileName);
         // uploading file to given path
         $post->image = $fileName;
     }
     $post->fill($request->only(['title', 'post']));
     $post->save();
     Posts::findOrFail($id)->tags()->detach();
     $tags = [];
     foreach ($this->getTags($request->input('tags')) as $tag) {
         if (!empty($tag)) {
             $isFind = Tags::where('name', $tag)->get()->count();
             if ($isFind == 0) {
                 $tags[] = new Tags(['name' => $tag]);
             } else {
                 $tags[] = Tags::where('name', $tag)->first();
             }
         }
     }
     $post->tags()->saveMany($tags);
     return back();
 }
開發者ID:draaslan,項目名稱:codeman,代碼行數:38,代碼來源:PostController.php

示例2: update

 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Requests\PostsFormRequest $request)
 {
     $post = Posts::findOrFail($id);
     $input = $request->all();
     $input['alias'] = HelperFunctions::str2url($request->title);
     $post->update($input);
     $categories_ids = [];
     foreach ($input['categories_list'] as $value) {
         $category = Categories::findOrNew($value);
         if ($category->exists) {
             array_push($categories_ids, $value);
         } else {
             $category->name = $value;
             $category->save();
             array_push($categories_ids, $category->id);
         }
     }
     $post->categories()->sync($categories_ids);
     $tags_ids = [];
     foreach ($input['tags_list'] as $value) {
         $tag = Tags::findOrNew($value);
         if ($tag->exists) {
             array_push($tags_ids, $value);
         } else {
             $tag->name = $value;
             $tag->save();
             array_push($tags_ids, $tag->id);
         }
     }
     $post->tags()->sync($tags_ids);
     \Session::flash('success', 'Post updated');
     return redirect('/');
 }
開發者ID:Appsbender,項目名稱:blog-laravel,代碼行數:39,代碼來源:PostsController.php

示例3: postComment

 function postComment(Request $request)
 {
     $all = $request->all();
     $validator = Validator::make($all, ['comment' => 'required']);
     if ($validator->fails()) {
         $id = $all['id'];
         $post = Posts::findOrFail($id);
         $page_title = $post->title;
         $data = compact('post', 'page_title');
         return view('posts.show', $data)->withErrors($validator->errors());
     }
     $comment = Purifier::clean($all['comment'], 'markdown');
     $user_id = Auth::user()->id;
     $post_id = $all['id'];
     $post = Comments::create(['comment' => $comment, 'post_id' => $post_id, 'user_id' => $user_id]);
     return redirect(route('getPostShow', $post_id));
 }
開發者ID:feiliu3k,項目名稱:laravel5-learn-blog,代碼行數:17,代碼來源:PostsController.php


注:本文中的app\Posts::findOrFail方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。