首页 > 解决方案 > 在 Laravel 搜索中显示来自多个不同表的数据

问题描述

我一直在为我的库应用程序开发搜索功能,这是我的第一个 Laravel 项目,所以我有点挣扎。我终于找到了搜索功能,我可以在其中按书名搜索一本书,但是,我无法显示搜索中不在该表中的任何数据。如果我运行搜索,我会收到以下错误消息:

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

我在“书籍”和“作者”之间创建了一个数据透视表,如果我只想在我的索引页面上显示数据而不搜索它可以工作,但搜索后我无法让它工作。此外,如果我从“books”表之外的 index.blade.php 中删除所有数据,则搜索工作正常。

你能帮我解决这个问题吗?

图书控制器:

<?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);
}

}

标签: phpmysqllaraveleloquent

解决方案


首先,您只是在这里覆盖自己:

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

我怀疑你想要两个languageand user,你可能想要authors, 以及你试图在视图中获取的其他一些数据?这称为急切加载,它在多个关系上完成,如下所示:

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

不确定您遇到的错误,$book应该是App\Bookand not的一个实例,stdClass即使没有急切加载,关系也应该仍然可用。我怀疑您没有显示某些代码,或者您的模型定义不正确。


推荐阅读