i wanted access the value of content_type_id
.i tried like $courseChapters['chapter_content']['content_type_id']
, $courseChapters['content_type_id']
, $courseChapters[0]['content_type_id']
& $courseChapters['chapterContent']['content_type_id']
.All these shows the error ErrorException: Undefined index: content_type_id
. I also tried everything in the foreach
loop i’ve commented below. Nothing works. Can someone tell me how to fix this?
/*foreach($courseChapters as $key){ // $contentTypeId=$key->first(); //Call to a member function first() on int // $contentTypeId=$key->first()->chapter_content; //Call to a member function first() on int // $contentTypeId=$key['chapter_content']['content_type_id']; //error - Illegal string offset // $contentTypeId=$key[0]['content_type_id']; ////error - Illegal string offset // $contentTypeId=$key['content_type_id']; //error - Illegal string offset 'content_type_id' }*/
$courseChapters = courseChapter::with(['chapterContent' => function ($q) use ($id) { $q->where('course_chapter_id', $id)->select('course_chapter_id','file_id','content_type_id');}]) ->select('id','courseId', 'chapter_title', 'isExam', 'points') ->get()->toArray()[0];
Heading ## dd($courseChapters);
shows array like given below:
array:6 [ "id" => 4 "courseId" => 24 "chapter_title" => "chapter1" "isExam" => false "points" => 8 "chapter_content" => array:1 [ 0 => array:3 [ "course_chapter_id" => 4 "file_id" => 1 "content_type_id" => 1 ] ] ]
Advertisement
Answer
Like this to access the first
echo $courseChapters['chapter_content'][0]['content_type_id'];
or like this to access all of them
foreach ($courseChapters['chapter_content'] as $chapter) { echo $chapter['content_type_id']; }