繁体   English   中英

调用数组上的成员 function toArray() -Laravel

[英]Call to a member function toArray() on array -Laravel

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

你在正确的轨道上。 您可以使用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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM