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


PHP Post::firstOrCreate方法代碼示例

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


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

示例1: postCreate

 /**
  * Processes the Create Post request.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function postCreate(Request $request)
 {
     // validate request
     $this->validate($request, ['provider' => 'required', 'uri' => 'required|url', 'source_time' => 'required']);
     // parse request
     $uri = $request->uri;
     // create a post if it does not exist
     $post = Post::firstOrCreate($request->except('_token'));
     // get the user logged in
     $user = \Auth::user();
     // if a user is logged in and the post is not already associated,
     // then associate the user with the post
     if (isset($user) && !$user->posts->contains('id', $post->id)) {
         $user->posts()->save($post);
     }
     // indicate saving of the post
     \Session::flash('flash_message', 'Saved post <a href=\'' . $uri . '\' target=\'_blank\'>' . $uri . '</a>.');
     // redirect to the previous page
     $view = redirect(back()->getTargetUrl() . '#' . $post->id);
     return $view;
 }
開發者ID:jasonjyu,項目名稱:cscie15-p4,代碼行數:27,代碼來源:PostController.php

示例2: findByTagOrCreate

 public function findByTagOrCreate($postData)
 {
     return Post::firstOrCreate($postData);
 }
開發者ID:ncnlinh,項目名稱:scribl,代碼行數:4,代碼來源:PostRepository.php

示例3: testCreate

 public function testCreate()
 {
     // test firstOrCreate
     $post = Post::firstOrCreate(['content' => 'post 1']);
     // record exists
     $this->assertEquals(1, $post->id);
     $post = Post::firstOrCreate(['content' => 'post xxx']);
     // record does not exist
     $post = Post::where('content', '=', 'post xxx')->first();
     $this->assertEquals('post xxx', $post->content);
     // test firstOrNew
     $post = Post::firstOrNew(['content' => 'post 1']);
     // record exists
     $this->assertEquals(1, $post->id);
     $post = Post::firstOrNew(['content' => 'post yyy']);
     // record does not exist
     $this->assertEquals('post yyy', $post->content);
     $this->assertFalse(property_exists($post, 'user_id'));
     $post = Post::where('content', '=', 'post yyy')->first();
     $this->assertNull($post);
 }
開發者ID:richarddlu,項目名稱:test_eloquent,代碼行數:21,代碼來源:EloquentTest.php


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