当前位置: 首页>>代码示例>>PHP>>正文


PHP Post::firstOrNew方法代码示例

本文整理汇总了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;
 }
开发者ID:stcoder,项目名称:uf-vova,代码行数:30,代码来源:Post.php

示例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}");
     }
 }
开发者ID:melbournecocoa,项目名称:website,代码行数:34,代码来源:LoadPosts.php

示例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;
 }
开发者ID:stcoder,项目名称:uf-vova,代码行数:17,代码来源:Imports.php

示例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';
 }
开发者ID:punkrockio,项目名称:Sample-Web-App,代码行数:20,代码来源:CmsController.php

示例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);
 }
开发者ID:richarddlu,项目名称:test_eloquent,代码行数:21,代码来源:EloquentTest.php


注:本文中的app\Post::firstOrNew方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。