Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP/Laravel中,如何在没有foreach的情况下访问值?_Php_Laravel_Foreach - Fatal编程技术网

在PHP/Laravel中,如何在没有foreach的情况下访问值?

在PHP/Laravel中,如何在没有foreach的情况下访问值?,php,laravel,foreach,Php,Laravel,Foreach,在我的PHP代码中,我注意到我只能使用foreach访问我的值。有人能解释为什么吗 return view('pages.temp_page_course', [ 'page' => $this->course($slug), ]); public function course($slug) { $course = Course::where('slug', $slug)->get(); return $course; } 使用此

在我的PHP代码中,我注意到我只能使用foreach访问我的值。有人能解释为什么吗

return view('pages.temp_page_course', [
        'page' => $this->course($slug),
    ]);


public function course($slug)
{
    $course = Course::where('slug', $slug)->get();
    return $course;
}
使用此代码,我可以访问该值

@foreach($page as $key => $course)
    {{ $course->title }}
@endforeach
如何在不进行foreach的情况下访问该值


非常感谢

$course=course::where('slug',$slug)->get()将获取课程数组

试试看,
$course=course::where('slug',$slug)->first()
将只获取1,并且将不再需要循环。

将get()替换为toArray(),这将把结果作为数组加载到
$course
中,以便您可以作为数组访问它

public function course($slug)
{
    $course = Course::where('slug', $slug)->toArray();
    return $course;
}

我们能看看
课程()
?是的!非常感谢,我更明白。很好@是的,我不得不等