在Laravel搜索中显示来自多个不同表的数据

问题描述 投票:0回答:2

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

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

我已经在'books'和'authors'之间创建了一个数据透视表,如果我只想在索引页面上显示数据而不进行搜索,则可以使用,但是在搜索之后,我无法使其正常工作。另外,如果我从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 mysql search laravel-6
2个回答
0
投票

您将获得$ authors为未定义状态,因为它未通过索引函数传递给视图,并且在视图中不可访问。

将其传递到紧凑视图

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

这可能会解决问题


0
投票

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

$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,但我怀疑您没有显示某些代码,或者您的模型没有正确定义。

© www.soinside.com 2019 - 2024. All rights reserved.