Home > OS >  Laravel - looking for view with 'http://localhost' in filename
Laravel - looking for view with 'http://localhost' in filename

Time:11-14

I'm getting an error on one of my views - 'View [http:..localhost:8000.bookings.create] not found.' This view was originally working as bookings.index, but I realised I wanted to use that page to display existing bookings so I renamed the file to bookings/create.blade.php and updated the Controller and web.php to match. The index page now works, but the create file is 'missing.'

After reading similar problems on this site, I've cleared the cache with 'php artisan cache:clear', and I've tried restarting the server, but the problem remains.

Here's my code.

web.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'));
    }
}
?>

resources/views/bookings/create.blade.php

<x-app-layout>
    <!-- Session Status -->
    <x-auth-session-status  :status="session('status')" />

    <div >
        @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"  type="date" name="start" :value="old('start')" required autofocus />

                    <x-input-error :messages="$errors->get('start')"  />
                </div>

                <!-- End Time -->
                <div >
                    <x-input-label for="end" :value="__('End Date')" />

                    <x-text-input id="end"  type="date" name="end" :value="old('end')" required />

                    <x-input-error :messages="$errors->get('end')"  />
                </div>
                <div >
                    <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 >
                        {{ __('Create Booking Slots') }}
                    </x-primary-button>
                </div>
            </form>
        @endif
    </div>
</x-app-layout>

One of the 'vendor frames' shown in the error page relates to this function in 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.");
}

I added a line before the 'if' to display $path.'/'.$file and the problem becomes clearer. When I navigate to the root, it shows "/home/mag/code/soccer/resources/views/welcome.blade.php", but if I navigate to bookings/create, it shows "/home/mag/code/soccer/resources/views/http://localhost:8000/bookings/create.blade.php". Clearly the 'http://localhost:8000/' shouldn't be there, but I don't know where it's coming from or why it's not being stripped.

This function is called by public function find($name) in the same file, but from there I'm lost. Searching for 'FileViewFinder->find(' returns many instances in the log files, but not in the actual code.

Any advice on how to proceed is appreciated. Did I break something by renaming the file? I'm sure if I rebuilt this project in another directory, the problem wouldn't appear, but I'd rather know why this happened so I can fix it and avoid it happening again.

CodePudding user response:

As @lagbox said You dont need to use route while redirecting to view

public function create()
{
    return view('bookings.create', [
        'fields' => Field::get(),
        'timeslots' => Timeslot::get(),
    ]);
}
  • Related