本文整理汇总了PHP中app\Post::firstOrNew方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::firstOrNew方法的具体用法?PHP Post::firstOrNew怎么用?PHP Post::firstOrNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Post
的用法示例。
在下文中一共展示了Post::firstOrNew方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: normalize
/**
* @param $postData
* @return mixed
*/
public static function normalize(&$postData)
{
$postId = sprintf('%d_%d', $postData['from_id'], $postData['id']);
$post = PostModel::firstOrNew(['external_id' => $postId]);
self::$__count_imports_posts++;
if (!$post->id) {
self::$__count_imports_new_posts++;
}
$text = '';
if ($postData['post_type'] == 'copy') {
$text = isset($postData['copy_text']) && !empty($postData['copy_text']) ? $postData['copy_text'] : '';
}
$text .= !empty($text) && !empty($postData['text']) ? '<br>' : '';
$text .= !empty($postData['text']) ? $postData['text'] : '';
$post->date = Carbon::createFromTimestamp($postData['date']);
$post->text = $text;
$post->save();
// dump($text, $post->text);
if (isset($postData['attachments'])) {
$attachments = self::attachmentsPost($postData['attachments']);
if (sizeof($attachments) > 0) {
$post->attachments()->sync($attachments, false);
}
}
return $post;
}
示例2: handle
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$yaml = new Parser();
$posts = $yaml->parse(file_get_contents(base_path($this->argument('file'))));
foreach ($posts['posts'] as $post) {
$slug = Str::slug($post['title']);
$newPost = Post::firstOrNew(['slug' => $slug]);
$newPost->timestamps = false;
$newPost->title = $post['title'];
$newPost->subtitle = $post['subtitle'];
$newPost->body = $post['body'];
$newPost->frontpage = $post['frontpage'];
if (isset($post['coverImage'])) {
$newPost->coverImage = $post['coverImage'];
}
$date = Carbon::createFromTimestamp($post['created_at']);
$newPost->created_at = $date;
$newPost->updated_at = Carbon::now();
$newPost->save();
$newPost->events()->detach();
if (isset($post['events']) && is_array($post['events'])) {
$events = (new Event())->whereIn('id', $post['events'])->get();
if (!$events->isEmpty()) {
$newPost->events()->saveMany($events);
}
}
$this->info("Loaded: {$newPost->title}");
}
}
示例3: __normalizePosts
/**
* @param array $postRaw
* @return \App\Post
*/
protected function __normalizePosts(&$postRaw)
{
$postId = sprintf('%d_%d', $postRaw['from_id'], $postRaw['id']);
$post = Post::firstOrNew(['external_id' => $postId]);
$post->date = Carbon::createFromTimestamp($postRaw['date']);
$post->text = $postRaw['text'];
$post->save();
$attachments = $this->__attachmentsPost($postRaw['attachments']);
if (sizeof($attachments) > 0) {
$post->attachments()->sync($attachments, false);
}
return $post;
}
示例4: updatePosts
private function updatePosts($data)
{
foreach ($data as $row) {
$post = Post::firstOrNew(['id' => $row->id]);
$post->authour_id = $row->authour_id;
$post->slug = $row->slug;
$post->title = $row->title;
$post->subtitle = $row->subtitle;
$post->content_raw = $row->content_raw;
$post->content_html = $row->content_html;
$post->page_image = $row->page_image;
$post->meta_description = $row->meta_description;
$post->is_draft = $row->is_draft;
$post->layout = $row->layout;
// $post->published_at = $row->published_at;
$post->thumb_url = $row->thumb_url;
$post->save();
}
return '/admin/post';
}
示例5: 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);
}