简体   繁体   English

Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id

[英]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.

相关问题 Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3 - Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3 Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails PHP错误:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ retails - PHP error: Undefined property: Illuminate\Database\Eloquent\Collection::$retails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM