我对Laravel 5和MVC框架一般都很陌生。我无法从数据库查询中获取结果并将其传递到我的刀片视图。以下是我对控制器功能的了解......
public function show(Project $project)
{
$technologies = DB::select('select * from technologies where id = ?', [$project->technology_id]);
return view('projects.show', compact('project','technologies'));
}
我认为......
@section('content')
<h2>{{ $project->name }}</h2>
@if ( !technologies() )
no technologies.
@else
<ul>
@foreach( $technologies as $technology )
<li><a href="#">{{ $technology->slug }}</a></li>
@endforeach
</ul>
@endif
@endsection
感谢您的帮助
答案 0 :(得分:3)
<强>控制器强>
public function show(Project $project)
{
$technologies = DB::table('technologies')
->select('*')
->where('id', $project->technology_id)
->get(); // you were missing the get method
return view('projects.show', compact('project', 'technologies'));
}
查看强>
这在您的视图中无效:@if ( !technologies() )
@section('content')
<h2>{{ $project->name }}</h2>
@if($technologies)
<ul>
@foreach($technologies as $technology)
<li><a href="#">{{ $technology->slug }}</a></li>
@endforeach
</ul>
@else
<p>no technologies.</p>
@endif
@stop
此外,如果您对以更简洁的方式循环访问数据或使用刀片显示“无数据”警告感兴趣,请查看本文使用的是@each https://laravel-news.com/2014/09/laravel-blade/
答案 1 :(得分:0)
将您的查询重写为,
$technologies = DB::table('technologies')->where('id',$project->technology_id)->get();