当前位置: 首页>>代码示例>>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;未经允许,请勿转载。