簡體   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