我正在接收此消息。 當我調用非數據庫的東西時,我想要一條錯誤消息。如果關鍵字不存在,則顯示Laravel 5錯誤消息
我是laravel的新人,我知道我必須寫elseif條件,但我不知道如何。
FatalErrorException
方法照亮\查看\查看:: __的toString()不得拋出一個異常, 抓ErrorException:未定義的變量:YYY(查看: /用戶/ XXX /工作/ YYY /資源/views/yyy/index.blade.php)
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class SearchController extends Controller {
public function getSearch(Request $request) {
$search = $request->input('search');
if(empty($search)) {
return "please search something!";
}
$searchResult = DB::table('yyy')->where('dateOfReport', 'LIKE', "%$search%")->get()->toArray();
return view('search.searchResult', ['searchResult' => $searchResult, 'search' => $search]);
}
}
而且,這裏是我的view.blade文件:
@extends('layout')
@section('content')
<h1>Detaily yyy Reports</h1>
<h2>{{$yyy->temperature }}°C</h2>
<p>{{$yyy->dateOfReport}}
<a href="/yyy/{{$yyy->id}}/delete" onclick="return confirm('Are you sure to delete?');"><font color="red">Delete</font></a>
Report created at: {{$yyy->created_at}}
@if($yyy->created_at != $yyy->updated_at)
Report updated at: {{$yyy->updated_at}}
{{ csrf_field() }}
@endif
@endsection
謝謝你的時間!
如果這是一個搜索結果頁面,您需要在視圖中處理它。查看https://laravel.com/docs/5.4/blade#loops。 '@ forelse'用'$ searchResult'看起來好像會做你需要的東西 – apokryfos
請不要污衊你的帖子,人們有時間回答它,如果你喜歡,你可以刪除它,鏈接在你的問題 –