I am getting either of 2 errors one after the other.
While submitting the form either I get the error below
The page has expired due to inactivity. Please refresh and try again.
Or this error
InvalidArgumentException
Route [register/user/image] not defined.
I have cross checked everything unable to find out the real cause.
Routes/web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::prefix('manage')->middleware('role:superadministrator|administrator|editor|member')->group(function() {
Route::get('/', 'ManageController#index');
Route::get('/carezone', 'ManageController#carezone')->middleware('role:superadministrator|administrator')->name('manage.dashboard');
Route::get('/dashboard', 'ManageController#dashboard')->name('manage.dashboard');
});
Route::middleware('role:superadministrator|administrator|editor|member|subscriber')->group(function() {
Route::get('register/user/details', 'RegisterUserController#showUserDetailsForm')->name('register.user.details');
Route::post('register/user/details', 'RegisterUserController#postUserDetailsForm')->name('register.user.details');
Route::get('/api/username/unique', 'RegisterUserController#apiCheckUniqueUserName')->name('api.username.unique');
Route::get('register/user/image', 'RegisterUserController#showUserImageForm')->name('register.user.image');
Route::post('register/user/image', 'RegisterUserController#postUserImageForm')->name('register.user.image');
});
Route::get('/email/unique', 'RegisterUserController#checkUniqueEmail')->name('email.unique');
Route::get('/get/city', 'RegisterUserController#ajaxGetCity')->name('get.city');
Route::get('/registration', 'RegisterUserController#showRegistrationForm')->name('registration');
Route::get('/home', 'HomeController#index')->name('home');
RegisterUserController.php
/*function to redirect user to user details page after register page*/
public function showUserDetailsForm() {
$states = State::all();
return view('pages.registration.registerUserDetails', ['states'=> $states]);
}
/*function to post and save data to the database*/
public function postUserDetailsForm(Request $request) {
$validatedData = $request->validate([
'username' => 'required|alpha_num|min:6|max:20|unique:details,username',
'dob' => 'required|date',
'gender' => 'required|string',
'state' => 'required|numeric',//validation rule for max min values
'city' => 'required|numeric',//validation rule for max min values
]);
$post = new Detail;
$post->user_id = Auth::user()->id;
$post->username = $request->username;
$post->dob = $request->dob;
$post->gender = $request->gender;
$post->state_id = $request->state;
$post->city_id = $request->city;
$post->save();
return redirect()->route('register/user/image');
}
Small Portion Of my form
<form id="registerUserDetails" class="form-horizontal clearfix" method="POST" action="{{ route('register.user.details') }}" role="form" novalidate>
{{ csrf_field() }}
.
.
.
.
<button type="submit" class="tabButton">Next</button>
</form>
<script>Javascript Validation here</script>
To solve the second error you must fix your route name on your controller method (and everywhere):
Actual:
return redirect()->route('register/user/image');
Correct:
return redirect()->route('register.user.image');
For solving the first error add the csrf field {{ csrf_field() }} to form
And for the second one, you route's name is register.user.image
so set the form's action like this
<form action=" {{ route('register.user.image') }} ">
Related
I have this form
<form method='POST' action='/products/{{ $product->id }}/reviews'>
{{ csrf_field() }}
<div>
<textarea name = 'review' placeholder ='Post a review'>{{ old('review') }}</textarea>
</div>
<div>
<button type ='submit'>Save</button>
</div>
</form>
The data then gets passed to this function in my controller
public function store(products $product)
{
$product->addReview(request('review'));
return back();
}
The addReview method is found in my products model
public function addReview($review)
{
return reviews::create([
'product_id' => $this->id,
'review' => $review
]);
}
I think the problem lies here
'product_id' => $this->id,
Once I fill in the form and submit, no data is added to the 'product_id' field. It's not included in any of the post data.
In your Review model:
public function product() {
return $this->belongsTo(products::class);
}
I renamed it to a product because it is one result not multiple.
Then here:
public function addReview($review)
{
return reviews::create([
'product_id' => $review->product->id,
'review' => $review
]);
}
Or even better, if you are adding a review, you should have the Product selected not the review, as you are adding a review to the product, so the method should be something like this:
public function addReview($product)
{
$review = request('review');
return $product->reviews()->create([
'review' => $review
]);
}
EDIT I see that you already have this.. so this code should work. If in the reviews table you have a product_id column, the relationship should be correct.
There are routes
Route::get('posts', 'PostsController#index');
Route::get('posts/create', 'PostsController#create');
Route::get('posts/{id}', 'PostsController#show')->name('posts.show');
Route::get('get-random-post', 'PostsController#getRandomPost');
Route::post('posts', 'PostsController#store');
Route::post('publish', 'PostsController#publish');
Route::post('unpublish', 'PostsController#unpublish');
Route::post('delete', 'PostsController#delete');
Route::post('restore', 'PostsController#restore');
Route::post('change-rating', 'PostsController#changeRating');
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');
Route::put('dashboard/posts/{id}', 'PostsController#update');
Route::get('dashboard', 'DashboardController#index');
Route::get('dashboard/posts/{id}', 'DashboardController#show')->name('dashboard.show');
Route::get('dashboard/published', 'DashboardController#published');
Route::get('dashboard/deleted', 'DashboardController#deleted');
methods in PostsController
public function edit($id)
{
$post = Post::findOrFail($id);
return view('dashboard.edit', compact('post'));
}
public function update($id, PostRequest $request)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return redirect()->route('dashboard.show', ["id" => $post->id]);
}
but when I change post and click submit button, I get an error
MethodNotAllowedHttpException in RouteCollection.php line 233:
What's wrong? How to fix it?
upd
opening of the form from the view
{!! Form::model($post, ['method'=> 'PATCH', 'action' => ['PostsController#update', $post->id], 'id' => 'edit-post']) !!}
and as result I get
<form method="POST" action="http://mytestsite/dashboard/posts?6" accept-charset="UTF-8" id="edit-post"><input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden" value="aiDh4YNQfLwB20KknKb0R9LpDFNmArhka0X3kIrb">
but why this action http://mytestsite/dashboard/posts?6 ???
Try to use patch instead of put in your route for updating.
Just a small tip you can save energy and a bit of time by declaring the Model in your parameters like this:
public function update(Post $id, PostRequest $request)
and get rid of this
$post = Post::findOrFail($id);
EDIT
You can use url in your form instead of action :
'url'=> '/mytestsite/dashboard/posts/{{$post->id}}'
Based on the error message, the most probable reason is the mismatch between action and route. Maybe route requires POST method, but the action is GET. Check it.
Try to send post id in hidden input, don't use smt like that 'action' => ['PostsController#update', $post->id]
It contribute to result action url.
I've been working on a webapp recently in laravel and i wanted to have a eddit function within tthe application. but im getting this error Missing required parameters for [Route: producten.update] [URI: producten/{producten}], and i dont know what i've done wrong.
This is the Routes im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);
This is the controller function im using for showing the edit page and updating.
The Edit function
public function edit(Request $request, Product $product)
{
// $product = Product::FindorFail($id);
// Product is a table with all products, with sellprice and buy price
// fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
The Update Function:
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
and this is the view i use for it.
{{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController#update', $model ]) }}
{{ Form::label('naam:')}}
{{ Form::text('naam') }} <br>
{{ Form::label('inkoopPrijs:')}}
{{ Form::text('inkoopPrijs') }} <br>
{{ Form::label('verkoopPrijs:') }}
{{ Form::text('verkoopPrijs') }} <br>
{{Form::label('Fabrieken', 'Fabrieken Id:') }}
{{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
{{ Form::submit('edit')}}
{{ Form::close() }}
if there is anything else that i need to add to the question just let me know and i'll add it
Missing thing is the id you are not getting id there in your edit function
your edit function should as i am assuming that you are just showing the form from this method where user can edit
public function edit($id)
{
$product = Product::FindorFail($id);
//Product is a table with all products, with sellprice and buy price
//fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
your update method should seem like this
public function update(Request $request, $id)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
your routes should like this no need for only
Route::resource('/producten', 'productionController');
edit route will be as
<a href="{{ route('production.edit', $model->id) }}">
Try this hope it will help
Hello i am using createProduct page for my create form. And after i created a product i want to redirect to the same page with parameters. Can you help me with the following error please ?
web.php
Route::get('/admin/products/create','productsController#createProduct');
Route::post('/admin/products/creating','productsController#creatingProduct');
creating function
public function creatingProduct(){
$product = new Product();
$product->name = Input::get('name');
$product->description = Input::get('description');
$product->price = Input::get('price');
$categories = Category::all();
try {
$product->save();
$pageMessage = prepareMessage("alert-success","Yahoooo!!","Eklendiii");
} catch ( \Illuminate\Database\QueryException $e) {
$pageMessage = prepareMessage("alert-danger","Üzgünüz!!","Ürününüz eklenemedi");
}
// return view('admin.createProduct',compact('categories','pageMessage'));
return Redirect::route('/admin/products/create')->with( 'pageMessage', $pageMessage );
}
create function
public function createProduct(){
$categories = Category::all();
return view('admin.createProduct',compact('categories'));
}
createProduct.bladde.php
#if(isset($pageMessage))
{!!$pageMessage!!}
#endif
<form class="well form-horizontal" action=" {{url('admin/products/creating')}}" method="POST" id="contact_form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<!--{{ Form::open(['url' => '/admin/products/create', 'files' => true]) }}-->
<div class="form-group">
<label class="col-md-4 control-label">Ürün İsmi</label>
<div class="col-md-8 inputGroupContainer">
ERROR
InvalidArgumentException in UrlGenerator.php line 314:
Route [/admin/products/create] not defined.
You're passing a URL into Redirect::route() which expects the name of a route instead.
return redirect('/admin/products/create')->with( 'pageMessage', $pageMessage );
If you're using an older version of Laravel I believe it would be
return Redirect::to('/admin/products/create')->with( 'pageMessage', $pageMessage );
You can set up a named route and use that also, it's quite simple:
Route::post( '/admin/products/creating', [
'uses' => 'productsController#creatingProduct',
'as' => 'products.create'
]);
The benefits are that you can reference the route name throughout your application and if you decide to change the format of the URL you only have to do it in the one spot.
You should use route name instead of url if you're using route():
return redirect()->route('products.create');
So you can name your route:
Route::get('/admin/products/create', ['as' => 'products.create', 'uses' => 'productsController#createProduct']);
As alternative, you can use url like this:
return redirect('/admin/products/create');
You defined Redirect::route() which is not define you inside your route file.
It should be like below:
return Redirect::to('/admin/products/create')->with( 'pageMessage', $pageMessage );
On my page events.index, I first display a list of events for the logged on user.
On my index page I have a form with option/select to let the user select and display the events of another user. When he submits that form, I would like my index function (controller) to use the $user_id value (from the form) and display the events.index page again, but for events of that selected user.
I'm not sure what would be the best approach:
Set a session variable to keep the user_id value? Not sure how to do that with a form.
Submit the form with a get method (and get an ugly ?user_id=1 URL)
Change my index route to accept the post method (although I already have that post/events route taken (by Route::post('events', 'EventsController#store'))
Not sure what would be a clean way to do this:
My route for events/index:
Route::get('events', [
'as' => 'event.index',
'uses' => 'EventsController#index'
]);
Events Controller
public function index()
{
// How to get the $user_id value from form?
if (empty($user_id))
{
$user_id = \Auth::user()->id;
}
$events = Event::where('events.user_id','=','$user_id');
$users = User::all();
return view('events.index')->with(['events' => $events])->with(['users' => $users]);
}
View for index
{!! Form::open(['route' => 'events.index', 'method' => 'get']) !!}
<select id="user_id" name="user_id">
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
</select>
{!! Form::submit('Show events for this user') !!}
{!! Form::close() !!}
#foreach($events as $event)
...
#endforeach
You can get the user_id from a Request object, you just need to inject it in the index method:
public function index(Request $request)
{
$user_id = $request->get('user_id') ?: Auth::id();
$events = Event::where('events.user_id','=','$user_id')->get();
$users = User::all();
return view('events.index')->with(['events' => $events])->with(['users' => $users]);
}