How can I clear form input on form submit?
After URL is saved I return the view for creating a form so I can add new URL.
The problem is that this time URL is populated from previous save.
How can I empty it after save?
Isn't that the point of second parameter which is set to null?
Using laravel version 5.4.36.
In the "create" iew I have a form with URL input:
{!! Form::open(['route' => 'urls.store']) !!}
{{ Form::url('url', null, array('class' => 'form-control')) }}
{{ Form::submit('Add domain', array('class' => 'btn' )) }}
Store method saves URL to database and returns the same view:
public function store(Request $request)
{
// URL Validation
$this->validate($request, array(
'url' => 'required|url|unique:urls'
));
// Save
$url = new Url;
$url->url = $request->url;
$url->save()
return view('urls.create');
}
Your form view should be handled by a get request on Route and submitting form is a post request .So after submitting the form you should redirect the same page not just returning the view . Check the web.php is the Routes are ok or not . Try to
add it to the store method ,I think it may help you
return redirect()->back();
or with flash message
return redirect('urls.create')->with('success', 'URL has been added');
you can make value = some values come from the controller with empty values when it's the parameters is set i.e use isset function
or
you can make a flag in the view when it sends to controller check it if it is true means you submit the form, in this case, make the values with empty
Related
I am saving data from a simple form in my Laravel project.
While submitting, it should go to the route that is predefined for store() method. I use such code:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
It goes to the route that is for index() method. Any help?
In store() method, I have such code:
$posts = new Post;
$posts->title = $request->input('title');
$posts->body = $request->input('body');
$posts->save();
return redirect('/');
My web.php contains:
Route::resource('/','PostsController');
Your code is correct bro.. The only reason you're going to index is because of the
return redirect('/'); in the store function... Check whether youdata is saved in the database or not...
Have you tested to see if this actually saves the data still? With Route resources, the route will be the same for both store and index methods, just a different HTTP method.
Maybe your code is working well & data saved in the database. You return redirect('/') it to your index() method, so you don't understand the difference. Check your database.
I need to pass an id to the route (web.php) from the form. My application has comment section at opporunities/id (id in value) , Whenever non-Auth user submits comment , my app will ask login and redirects to /opportunities but i need /opportunities/id. In the form of comment i have submitted page id. I have setup my route as
Route::post('/opportunities', 'OpportunitiesController#postPost')->name('posts.post'); Now if i can pass that id to as /opportunities/id then after login user will automatically lands on that page. I have manually tested and attached id and it works. Do I need to use "use Illuminate\Http\Request;" to get form data to web.php (route)? to get request post id? All i need is to add id after Route:post('/opportunites/'). Any suggestion and help will be appropriated.
What I did was and figured out is that
action="{{route('opportunities',['returnvalue'=> $post['id']]) }}" I still got error #bipin answer but i passed it with as parameter and solved. Thanks bipin for suggestion though!
One solution could be, pass your post id inside the form
View Blade
{{ Form::hidden('post_id', 'value', array('id' => 'post_id')) }}
Controler
use Illuminate\Http\Request;
public function comment(Request $request)
{
//Validation
$this->validate($request, [
'post_id' => 'required'
]);
//Inside variable
$input = $request->all();
// OR
$request->post_id;
}
When I click Save on my edit view, my routing brings back my edit view instead of my index view and my update method is never reached.
I noticed that I reach the update method if I remove “UsersRequest $request” from the method parameters. Not sure why, and if it’s related, but I need $request to do my update (see controller code below):
Routes:
Route::get('/users', 'UsersController#index')->name('users.index');
Route::patch('/users/{id}',
[
'as' => 'users.update',
'uses' => 'UsersController#update'
]);
Route::get('/users/{id}/edit', 'UsersController#edit');
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UsersRequest;
//public function update($id, UsersRequest $request)
public function update($id) //- with $request removed, the index view is displayed
{
$user = \Auth::user();
$user->update($request->all());
return view('users.index');
}
Edit view:
{!! Form::model($user, ['method' => 'PATCH', 'action' => [ 'UsersController#update', 'user' => $user->id ] ]) !!}
{!! Form::submit('Save', ['class'=>'btn primary']) !!}
{!! Form::close() !!}
Network after save button clicked
URL Protocol Method Result
/myapp/public/users/1 HTTP POST 302 Goes for the update route
http://000.000.000.000/myapp/public/users/1/edit HTTP POST 200 Redirects to the edit route??
.env
APP_URL=http://000.000.000.000/myapp/public
You're failing whatever validation is present in your UsersRequest form request. When the validation fails, it redirects you back to where you came from, which is your edit view. Your edit view should be updated to show the validation errors so that your users know what fields need to be fixed.
The reason it works when you remove the UsersRequest $request parameter is that the validation is no longer being performed.
I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('test/{id}/edit','TestController#edit');
My controller:
public function edit($id)
{
$post = Post::find($id);
if(!$post)
abort(404);
return view('test/edit')->with('test', $post);
}
My form:
{{ Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}
You're assigning the post value to 'test', so should be accessible with $test rather than $post.
You probably want to do either of these two things instead:
return view('test/edit')->with('post', $post);
or
return view('test/edit', ['post' => $post]);
https://laravel.com/docs/5.4/views
Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.
I have been trying to implement a dropzone file upload module with Laravel 5.
The problem is that when a file is uploaded dropzone sends the full request to the file upload controller, however, the controller does not get any data.
What am I missing? I have included the code I'm using. Thanks in advance for your time/help.
The view
{!! Form::open( array( 'url' => 'admin/file/upload', "id"=>"file-upload-modal", 'class'=>"dropzone" ) ) !!}
{!! Form::close() !!}
Controller
public function upload(FileUploadRequest $request){
//addeed this line for testing. //returns request array with an empty data set.
return response()->json($request);
if($request->hasFile('file')){
$request->file('file')->move('images');
}
}
It returns a 200 status code and dropzone thinks the file gets uploaded. However the response is actually an empty data set
response:
{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}