【问题标题】:调用数组-Laravel 上的成员函数 toArray()
【发布时间】:2022-01-22 21:30:29
【问题描述】:
   // $result=[];
 foreach ($chapter['chapter_content'] as $row) {
    $result = CoursePublishChaptercontent::create([
                                            'courseId' => $postdata[$i]['courseId'],
                                            'course_chapter_id' => $postdata[$i]['course_chapter_id'],
                                            'file_id' => $postdata[$i]['file_id'],
                                            'course_chapter_content_id' => $postdata[$i]['course_chapter_content_id'],
                                        ]);
}
dd($result->toArray());

dd($result) 显示以下数据。我不能在foreach 之外调用$result->toArray(); - 它显示错误Undefined variable: result。在 foreach 之前将$result 声明为$result=[]; 时,会显示错误Call to a member function toArray() on array。我该如何解决?有人可以告诉我如何实现吗?我还尝试在 foreach 之前将 $result 声明为 ''null。两者都分别显示错误Call to a member function toArray() on stringCall to a member function toArray() on null

App\Models\CoursePublishChaptercontent {#441
  #table: "course_publish_chapter_contents"
  #fillable: array:9 [
    0 => "course_chapter_id"
    1 => "file_id"
    2 => "courseId"
    3 => "course_chapter_content_id"
  ]
  #connection: "pgsql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:12 [
    "courseId" => 1
    "course_chapter_id" => 18
    "content_type_id" => 1
    "file_id" => null
    "course_chapter_content_id" => 17
    "id" => 106
  ]
  #original: array:12 [
    "courseId" => 1
    "course_chapter_id" => 18
    "content_type_id" => 1
    "course_chapter_content_id" => 17
    "content_description" => null
    "id" => 106
  ]
  #changes: []
  #casts: array:1 [
    "deleted_at" => "datetime"
  ]
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
  #forceDeleting: false
  #enableLoggingModelsEvents: true
  #oldAttributes: []
}

【问题讨论】:

    标签: php arrays laravel eloquent laravel-8


    【解决方案1】:

    你在正确的轨道上。您可以使用 collect() 帮助器将结果数组转换为集合,然后可以使用 toArray() 方法。 这是一种方法:

     $result = [];
     foreach ($chapter['chapter_content'] as $row) {
        $result[] = CoursePublishChaptercontent::create([
                                                'courseId' => $postdata[$i]['courseId'],
                                                'course_chapter_id' => $postdata[$i]['course_chapter_id'],
                                                'file_id' => $postdata[$i]['file_id'],
                                                'course_chapter_content_id' => $postdata[$i]['course_chapter_content_id'],
                                            ]);
    }
    dd(collect($result)->toArray());
    

    【讨论】:

    • 行得通。但是你不能直接在$result 上调用 toArray() 吗?实际上,collect() 在这里做什么?
    • 阅读有关集合对象的文档,我认为这可以澄清很多。 laravel.com/docs/8.x/collections $result 包含刚刚创建的 CoursePublishChaptercontent 模型对象,并有一个 toArray() 方法。数组没有任何方法。所以需要先将数组转化为集合对象。
    猜你喜欢
    • 2020-03-16
    • 2017-05-22
    • 2018-06-28
    • 1970-01-01
    • 2019-01-22
    • 2019-01-18
    • 2021-06-15
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多