我的一個視圖出現錯誤 - “找不到視圖 [http:..localhost:8000.bookings.create]。” 這個視圖最初是作為 bookings.index 作業的,但我意識到我想使用該頁面來顯示現有的預訂,所以我將檔案重命名為 bookings/create.blade.php 并更新了 Controller 和 web.php 以匹配。索引頁面現在可以作業,但創建檔案“丟失”。
在閱讀了此站點上的類似問題后,我使用 'php artisan cache:clear' 清除了快取,并嘗試重新啟動服務器,但問題仍然存在。
這是我的代碼。
網頁.php
Route::resource('bookings', BookingController::class)
->only(['index', 'store', 'create', 'edit', 'update', 'destroy'])
->middleware(['auth', 'verified']);
BookingController.php
<?php
namespace App\Http\Controllers;
use App\Model\User;
use App\Models\Booking;
use App\Models\Field;
use App\Models\Timeslot;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class BookingController extends Controller
{
public function index(Request $request)
{
$validated = $request->validate([
'date' => 'required|date|after:yesterday',
]);
return view('bookings.index', [
'fields' => Field::get(),
'timeslots' => Timeslot::get(),
'date' => $request->date,
'bookings' => Booking::where('date', $request->date)->get(),
]);
}
public function create()
{
return view(route('bookings.create', [
'fields' => Field::get(),
'timeslots' => Timeslot::get(),
]));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validated = $request->validate([
'start' => 'required|date|after:yesterday',
'end' => 'required|date|after:start',
'field' => 'required|array',
'timeslot' => 'required|array',
]);
// create Models
$start = $request->start;
$end = $request->end;
$period= CarbonPeriod::create($start, $end);
// dd($period);
$data = array();
foreach ($period as $date) {
foreach ($request->field as $f) {
foreach ($request->timeslot as $ts) {
$booking = Booking::firstOrCreate([
'date' => $date->format('Y-m-d'),
'field_id' => $f,
'timeslot_id' => $ts,
]);
}
}
}
Booking::insert($data);
return redirect(route('bookings.create'));
}
}
?>
資源/視圖/bookings/create.blade.php
<x-app-layout>
<!-- Session Status -->
<x-auth-session-status class="mb-4" :status="session('status')" />
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
@if(auth()->user()->roles()->where('role', 'admin')->exists())
<form method="POST" action="{{ route('bookings.store') }}">
@csrf
<!-- Select Dates -->
<div>
<x-input-label for="start" :value="__('Start Date')" />
<x-text-input id="start" hljs-number">1 w-full" type="date" name="start" :value="old('start')" required autofocus />
<x-input-error :messages="$errors->get('start')" hljs-number">2" />
</div>
<!-- End Time -->
<div hljs-number">4">
<x-input-label for="end" :value="__('End Date')" />
<x-text-input id="end" hljs-number">1 w-full" type="date" name="end" :value="old('end')" required />
<x-input-error :messages="$errors->get('end')" hljs-number">2" />
</div>
<div hljs-string">">
<div>
<x-input-label for="field[]" value="Field" />
@foreach ($fields as $field)
<input type="checkbox" name="field[]" id="" value="{{ $field->id }}" /> {{ $field->name }}<br />
@endforeach
</div>
<div>
<x-input-label for="timeslot[]" value="Timeslot" />
@foreach ($timeslots as $timeslot)
<input type="checkbox" name="timeslot[]" value="{{ $timeslot->id }}" />{{ $timeslot->start }} - {{ $timeslot->end }}<br />
@endforeach
</div>
</div>
<div>
<x-primary-button hljs-number">3">
{{ __('Create Booking Slots') }}
</x-primary-button>
</div>
</form>
@endif
</div>
</x-app-layout>
錯誤頁面中顯示的“供應商框架”之一與 vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php 中的此功能有關
protected function findInPaths($name, $paths)
{
foreach ((array) $paths as $path) {
foreach ($this->getPossibleViewFiles($name) as $file) {
if ($this->files->exists($viewPath = $path.'/'.$file)) {
return $viewPath;
}
}
}
throw new InvalidArgumentException("View [{$name}] not found.");
}
我在 'if' 之前添加了一行來顯示 $path.'/'.$file 并且問題變得更加清晰。當我導航到根目錄時,它顯示“/home/mag/code/soccer/resources/views/welcome.blade.php”,但如果我導航到預訂/創建,它顯示“/home/mag/code/soccer /resources/views/http://localhost:8000/bookings/create.blade.php”。顯然“http://localhost:8000/”不應該在那里,但我不知道它來自哪里,或者為什么它沒有被剝離。
此函式由同一檔案中的公共函式 find($name) 呼叫,但從那里我迷路了。搜索“FileViewFinder->find(”會在日志檔案中回傳許多實體,但不會在實際代碼中回傳。
任何關于如何進行的建議表示贊賞。我是否通過重命名檔案破壞了某些東西?我確定如果我在另一個目錄中重建這個專案,問題就不會出現,但我寧愿知道為什么會這樣,這樣我就可以修復它并避免它再次發生。
uj5u.com熱心網友回復:
正如@lagbox所說,您route在重定向查看時不需要使用
public function create()
{
return view('bookings.create', [
'fields' => Field::get(),
'timeslots' => Timeslot::get(),
]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532635.html
