Laravel検索で、複数の異なるテーブルからのデータを表示し

mekol:

私は、私はちょっと苦労していますので、私の最初のLaravelプロジェクトですマイライブラリアプリケーションのための検索機能に取り組んできました。私は最終的にしかし、私はそのテーブルにない検索から任意のデータを表示することができない、私はそのタイトルで書籍を検索できる検索機能を、考え出しました。私は、検索を実行した場合、私は、次のエラーメッセージが表示されます。

Facade\Ignition\Exceptions\ViewException
Undefined property: stdClass::$authors (View: /Users/krisz/code/project/resources/views/books/index.blade.php)

私はそれが動作検索せずにのみ、私のインデックスページ上のデータを表示したい場合、私は「ブック」と「作者」との間で、ピボットテーブルを作成しましたが、検索した後、私は仕事にそれを得ることができません。私は「本」テーブルの外にある私のindex.blade.phpからすべてのデータを削除した場合も、検索が正常に動作します。

あなたはこの問題で私を助けていただけますか?

BookController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Book;
use App\Language;
use App\Genre;
use App\Author;
use App\Publisher;
use App\User;
use Illuminate\Support\Facades\DB;

class BookController extends Controller
{ 
    public function index(Request $request)
    {
        $books = Book::with('language')->get();
        $books = Book::with('user')->get();
        $languages = Language::all();
        $genres = Genre::all();
        $publishers = Publisher::all();
        $users = User::all();
        $authors = Author::all();

        return view('books/index', compact('books','languages','genres','publishers','users'));
    }

    public function search(Request $request)
    {
        $authors = Author::all();
        $books = Book::with('language')->get();
        $books = Book::with('user')->get();
        $languages = Language::all();
        $genres = Genre::all();
        $publishers = Publisher::all();
        $users = User::all();

        $search = $request->get('search');
        $books = DB::table('books')
            ->where('title','like','%' .$search. '%')
            ->paginate(5);
        return view('books/index')
             ->with(compact('books','languages','genres','authors','publishers','users'));
    }
}

index.blade.php:

@extends('layout')


@section('title')
<title>Alle Bücher</title>
@section('content')
<style>
  .uper {
    margin-top: 40px;
  }
</style>
<div class="uper">
  @if(session()->get('success'))
  <div class="alert alert-success">
    {{ session()->get('success') }}
  </div><br />
  @endif
  <div align="left">
            <div class="col-md-4">
                <h1>Policy</h1>


            </div>
            <div class="col-md-4">
                <form action="{{ route('search') }}" method="get" role="search">
                    {{ csrf_field() }}
                    <div class="input-group">
                        <input type="text" class="form-control" name="search" placeholder="Search Title" <span class="input-group-btn">
                            <button type="submit" class="btn btn-primary">Search</button></span>
                    </div>
                </form>
            </div>
        </div>
  <table class="table table-hover">
    <thead>
      <tr>
        <td>ID</td>
        <td>Titel</td>
        <td colspan="2">Autor</td>
        <td>Jahr</td>
        <td colspan="2">Verlag</td>
        <td colspan="2">Genre</td>
        <td>Sprache</td>
        <td>ISBN</td>
        <td>Seitenzahl</td>
        <td>Ausgeliehen von:</td>
        <td colspan="2">Funktionen</td>
      </tr>
    </thead>
    <tbody>
      @foreach($books as $book)
      <tr>
        <td>{{$book->id}}</td>
        <td>{{$book->title}}</td>
        @foreach($book->authors as $author)
        <td>{{$author->name}}</td>
        @endforeach
        <td>{{$book->year}}</td>
        @foreach($book->publishers as $publisher)
        <td>{{$publisher->name}}</td>
        @endforeach
        @foreach($book->genres as $genre)
        <td>{{$genre->name}}</td>
        @endforeach
        <td>{{$book->language->name}}</td>
        <td>{{$book->isbn}}</td>
        <td>{{$book->pages}}</td>
        <td>{{$book->user->name}}</td>

        <td><a href="{{ route('books.edit', $book->id)}}" class="btn btn-primary">Bearbeiten</a></td>
        <td>
          <form action="{{ route('books.destroy', $book->id)}}" method="post">
            @csrf
            @method('DELETE')
            <button class="btn btn-danger" type="submit">Löschen</button>
          </form>
        </td>
      </tr>
      @endforeach
    </tbody>
  </table>
  <div>
    @endsection

ブックモデル:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $fillable = ['title', 'year', 'language_id', 'isbn', 'pages', 'user_id'];

public function publishers(){
     return $this->belongsToMany(Publisher::class);
}

public function authors(){
   return $this->belongsToMany(Author::class);
} 

public function genres(){
     return $this->belongsToMany(Genre::class);
}

public function language(){
    return $this->belongsTo(Language::class);
}
public function user(){
    return $this->belongsTo(User::class);
}

}
miken32:

まず、あなたはちょうどここに自分自身を上書きしています:

$books = Book::with('language')->get();
$books = Book::with('user')->get();

私はあなたが両方をしたい疑うlanguageuser、あなたはおそらくしたいauthorsビューでフェッチしようとするだけでなく、他のいくつかのデータを?これが呼び出され熱心にロードし、それがこのような複数の関係で行われています:

$books = Book::with(['language', 'user', 'authors', 'publishers', 'genres'])->get();

わからないあなたが取得しているエラーについて、など$bookのインスタンスでなければならApp\BookないstdClass、との関係はまだ熱心にロードされていない場合でも利用可能であるべきです。私はあなたが表示していないか、お使いのモデルが正しく定義されていないいくつかのコードがあると思います。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=24987&siteId=1