[英]Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id
i'm using the leftJoin on laravel, but it get an error我在 Laravel 上使用 leftJoin,但出现错误
Undefined property: Illuminate\\Database\\Eloquent\\Collection::$id
未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id
here the sample code on Controller这里是控制器上的示例代码
$news = News::find($id)
->leftJoin('categories', 'news.category_id', '=', 'categories.id')
->get();
//dd($news);
return view('news.update')
->with('news', $news);
I've been trying using get()->first() but it just show the first record only.我一直在尝试使用 get()->first() 但它只显示第一条记录。 And if i using foreach on blade like this, the error is just the same
如果我像这样在刀片上使用 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>
Try to do like this尝试这样做
$news = DB::table('news')
->leftJoin('categories', 'categories.id', '=', 'news.category_id')
->select('news.*', 'categories.*')
->get();
->select('news. ', 'categories. ') // To select all records from news and category ->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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.