【问题标题】:Display data from multiple different tables in Laravel search在 Laravel 搜索中显示来自多个不同表的数据
【发布时间】:2020-06-16 17:17:17
【问题描述】:

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

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

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

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

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

}

【问题讨论】:

  • 您正在尝试访问运行查询后未获取的属性。首先使用 dd() 检查您实际从数据库中获得的内容。
  • 我回来了 "select * from books where title like ?"

标签: php mysql laravel eloquent


【解决方案1】:

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

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

我怀疑您想要languageuser,并且您可能想要authors,以及您尝试在视图中获取的其他一些数据?这称为eager loading,它在多个关系上完成,如下所示:

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

不确定您遇到的错误,因为$book 应该是App\Book 而不是stdClass 的实例,并且即使没有预先加载,关系也应该仍然可用。我怀疑有些代码您没有显示,或者您的模型定义不正确。

【讨论】:

  • 感谢您的反馈!据我所知(这并不多),我无法将所有数据写入 $books 变量,因为它们来自不同的表。我现在已经发布了我的书模型,所以它更容易理解。
  • 当然它们是不同的表,关系就是这样工作的。
  • 谢谢,现在更有意义了!
  • 我已经通过文档链接进行了澄清。试一试,它应该可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多