簡體   English   中英

Laravel 錯誤 leftJoin:未定義的屬性:Illuminate\\Database\\Eloquent\\Collection::$id

[英]Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id

我在 Laravel 上使用 leftJoin,但出現錯誤

未定義的屬性:Illuminate\\Database\\Eloquent\\Collection::$id

這里是控制器上的示例代碼

$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>

嘗試這樣做

$news = DB::table('news')
          ->leftJoin('categories', 'categories.id', '=', 'news.category_id')
          ->select('news.*', 'categories.*')
          ->get();

->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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM