提问者:小点点

在array-larvel上调用成员函数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($result->toArray());

dd($result)显示以下数据。我无法调用$result-

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: []
}

共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());