I want to ask how to fix this error. I have this error Missing required parameters for [Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\xampp\htdocs\code\task1esources\views\userprofile\create.blade.php)
I have user informations in view userprofile\create.blade.php
and after submitting the button I want to relocate the page on the userprofile\edit.blade.php
This is my userProfileController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class userProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view("userprofile.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Also the route for this views
Route::group(['namespace' => 'User'], function(){
Route::get('/userprofile/{id}/edit', ['as' => 'userprofile.edit', 'uses' => 'userProfileController@edit']);
});
Route::resource('userprofile','userProfileController');
And my route action in the button in create.blade.php
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
If you have any idea why is this error, please write me! Thank you.
instead of
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
use
<a href="{{url('/userprofile/'.PROFILE ID.'/edit')" class="btn btn-primary" >Edit profile</a>
PROFILE ID must be id
You can't add action to a input field. Add action to form
tag. You are not passing any id in the form action. Add id to your action like I have passed id = 1
<form method="post" action="{{ route('userprofile.edit', ['id' => 1]) }}">
Update
First of all you have to create a user with given data and after that you redirect to the edit page with the generated id.
You need to set a default value for the id passed in to the edit
and other methods, like so:
public function edit($id = null)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
Here, we are setting the default value of id
to null
. Next, check in your method if it is null and if so, return with an error or whatever you wanna do in that situation:
public function edit($id = null)
{
if(!$id)
return back()->with(['error'=>'No document selected for editing.']);
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
if(!$id) return back()->with(['error'=>'No document selected for editing.']);
This is your check and if the id is null, we simply return back and notify the user (You must check for the error in your blade view and display it if it exists).