我一直在为我的库应用程序开发搜索功能,这是我的第一个 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'));
}
}
青春有我