我在laravel上使用leftJoin,但是出现错误
未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id
这里是Controller上的示例代码
$news = News::find($id)
->leftJoin('categories', 'news.category_id', '=', 'categories.id')
->get();
//dd($news);
return view('news.update')
->with('news', $news);
我一直在尝试使用get() - > first()但它只显示第一条记录。 如果我像这样在刀片上使用foreach,那么错误就是一样的
<form class="form-horizontal" action="/news/{{$news->id}}" method="post" enctype="mulipart/form-data">
<select name="category_id">
<option> - </option>
@foreach($news as $news)
<option value="{{ $news->category_id }}" selected>{{ $news->category }}</option>
@endforeach
</select> </form>
答案 0 :(得分:2)
尝试这样做
$news = DB::table('news')
->leftJoin('categories', 'categories.id', '=', 'news.category_id')
->select('news.*', 'categories.*')
->get();
- &gt; select('news。','categories。')//从新闻和类别中选择所有记录
<form class="form-horizontal" action="/news/{{$news[0]->id}}" method="post" enctype="mulipart/form-data">
<select name="category_id">
<option> - </option>
@foreach($news as $news_data)
<option value="{{ $news_data->category_id }}" selected>{{ $news_data->category }}</option>
@endforeach
</select>
</form>