本文整理汇总了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;
}
示例2: findByTagOrCreate
public function findByTagOrCreate($postData)
{
return Post::firstOrCreate($postData);
}
示例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);
}