php - 在 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);
}
}
解决方案
首先,您只是在这里覆盖自己:
$books = Book::with('language')->get();
$books = Book::with('user')->get();
我怀疑你想要两个language
and user
,你可能想要authors
, 以及你试图在视图中获取的其他一些数据?这称为急切加载,它在多个关系上完成,如下所示:
$books = Book::with(['language', 'user', 'authors', 'publishers', 'genres'])->get();
不确定您遇到的错误,$book
应该是App\Book
and not的一个实例,stdClass
即使没有急切加载,关系也应该仍然可用。我怀疑您没有显示某些代码,或者您的模型定义不正确。
推荐阅读
- c# - C# DNS 服务器重定向
- python - 错误缺少 1 个必需的位置参数 python 和线程
- linux - 如何更改 Linux 目录中文件的默认权限?
- html - CodeSandbox.io标签未加载图像
- ocr - Google Vision API - 我们能否在检测文档文本中包含自定义预处理逻辑并将其直接用作 Vision API
- web - 什么是最推荐的网络 360 度视频播放器?
- python - 字符串操作函数的基本python代码在jupyter笔记本中不起作用
- lab-color-space - 如何从 LAB 色彩空间中找到 Delta A、B、C 和 L?
- android - 当应用程序在本机反应中处于后台时,getInitialNotification 方法不会被调用
- git - Git:是否存在将分支 A 合并到 B 会产生冲突,但将 B 合并到 A 不会产生冲突的情况?