简体   繁体   中英

I am creating edit page using PHP Laravel 5.8. I have created routes and url, but error 404 not found shows up

I am learning the Laravel by building a job portal app. And I am coding for the company side to edit their post. Now I could go to url, ex) http://localhost:8000/jobs/23/edit . But on the screen shows me 404|not found error message

This is an screen shot

for edit page, I created views/jobs/edit.blade.php.

Of course, data is clearly inserted in my job table.

I tried to clear all the cache and, executed following commands.

1. composer dump-autoload

2. php artisan clear-compiled

3. php artisan optimize

4. php artisan config:cache

web.php

Route::get('/jobs/{id}/edit', 'JobController@edit')->name('job.edit');

JobController.php

public function edit($id)
{
    $jobs = Job::findOrFail($id);
    return view('jobs.myjob',compact('jobs'));
}

my-job.blade.php I have an edit button and following code is a link it has.

{{route('job.edit',[$job->id])}}

My environment is Windows, XAMPP and local MySQL server.

try this way :

<a href="{{route('job.edit',['id'=>$job->id])}}">Edit Job {{$job->id}}</a>

you can clear route cache by :

php artisan route:clear

update : run following command to clear cache

php artisan config:clear
php artisan cache:clear

I finally could solve this problem.

  1. I changed to a new route.

    Route::get('/test-jobs/{id}/edit', 'JobController@edit')->name('test-job.edit');

  2. I created new blade.php like this, views/test-jobs/edit.blade.php

  3. I also changed JobController.php

    public function edit($id){ $jobs = Job::findOrFail($id); return view('test-jobs.edit',compact('jobs')); }

And finally, my-job.blade.php which has an edit button like this.

 <a href="{{route('test-jobs.edit',[$job->id])}}"><button class="btn btn-dark">Edit</button></a>

But still, I don't know why it worked.

And also, I think I need another solution. Because in this way, I needed to create another new folder in views.

So if someone knows another solution, please let me know. Thank you.

The problem is probably in the line return view('jobs.myjob',compact('jobs')); in your JobController.php , it returns the following view: resources/views/jobs/myjob.blade.php (which probably doesn't exist).

try changing the line return view('jobs.myjob',compact('jobs')); to return view('jobs.edit',compact('jobs')); , after the change it refers to resources/views/jobs/edit.blade.php and that should solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM