简体   繁体   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) shows the below data. dd($result)显示以下数据。 I cannot call $result->toArray();我不能调用$result->toArray(); outside the foreach - it shows the error Undefined variable: result .foreach之外 - 它显示错误Undefined variable: result When declaring $result before foreach as $result=[];在 foreach 之前将 $ $result声明为$result=[]; , it shows the error Call to a member function toArray() on array . ,它显示错误Call to a member function toArray() on array How can i fix it?Can someone tell me how to achieve this?我该如何解决?有人可以告诉我如何实现吗? I also tried declaring $result as '' & null before foreach.我还尝试在 foreach 之前将$result声明为'' & null Both shows errors Call to a member function toArray() on string & Call to a member function toArray() on null respectively.两者都显示错误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: []
}

You are on the right track.你在正确的轨道上。 You can use the collect() helper to convert the result array into a collection and than you can use the toArray() method.您可以使用collect()帮助器将结果数组转换为集合,然后可以使用toArray()方法。 This is one way to do it:这是一种方法:

 $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