我一直在為我的庫應(yīng)用程序開發(fā)搜索功能,這是我的第一個 Laravel 項(xiàng)目,所以我有點(diǎn)掙扎。我終于找到了搜索功能,我可以在其中按書名搜索一本書,但是,我無法顯示搜索中不在該表中的任何數(shù)據(jù)。如果我運(yùn)行搜索,我會收到以下錯誤消息:Facade\Ignition\Exceptions\ViewExceptionUndefined property: stdClass::$authors (View: /Users/krisz/code/project/resources/views/books/index.blade.php)我在“書籍”和“作者”之間創(chuàng)建了一個數(shù)據(jù)透視表,如果我只想在我的索引頁面上顯示數(shù)據(jù)而不進(jìn)行搜索,它可以工作,但搜索后我無法讓它工作。此外,如果我從“books”表之外的 index.blade.php 中刪除所有數(shù)據(jù),則搜索工作正常。你能幫我解決這個問題嗎?圖書控制器:<?phpnamespace 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')); }}
在 Laravel 搜索中顯示來自多個不同表的數(shù)據(jù)
HUH函數(shù)
2022-10-14 16:01:47