Dropzonejs with Laravel 5 (Request does not get any data) - ajax

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":{}}

Related

Redirect to another route with data Laravel 5.8

Good day everyone. I am encountering an error in my code. I am trying to redirect to another route and passing data with it.
Controller code:
return redirect()->route('customer.success', compact('data'));
Route:
Route::get('success-customer', 'CustomerController#showSuccess')->name('customer.success');
Blade:
Your assistant: {{Session::get($data['assistant'])}}
Now my error is, it shows the error of undefined data yet I used the compact function.
Answers and advices are highly appreciated!
In laravel 5.8 you can do the following:
return redirect('login')->with('data',$data);
in blade file The data will store in session not in variable.
{{ Session::get('data') }}
You can use this:
return redirect()->route('profile', ['id' => 1]);
To redirect to any controller, use this code
return redirect()->action('DefaultController#index');
If you want to send data using redirect, try to use this code
return Redirect::route('customer.success)->with( ['data' => $data] );
To read the data in blade, use this one
// in PHP
$id = session()->get( 'data' );
// in Blade
{{ session()->get( 'data' ) }}
Check here for more info
TRy this
return Redirect::route('customer.success')->with(['data'=>$data]);
In blade
Session::get('data');

Laravel Cannot Decrypt Encrypted ID On Controller

I cannot decrypt the encrypted value on a controller after clicking on the submit button on my blade file below.
Controller :
public function edit($id)
{
$encrypted_id = encrypt($id);
return view('my.blade.edit', compact('encrypted_id'));
}
public function update(Request $request, $id)
{
$decrypted_id = decrypt($id);
dd($decrypted_id);
}
Blade: (my.blade.edit)
{{ Form::open(['route' => ['route.update', $encrypted_id ], 'method' => 'PATCH']) }}
{{ Form::button('Update', ['type' => 'submit', 'name' => 'update']) }}
{{ Form::close() }}
I am expecting an integer value on my dd(); but I still getting an encrypted string.
Well, as I've already written in the comments, first and simple is to check expected output and exact output.
So far we discovered, that value was sent to view isn't equal to value received in update() method.
id was encrypted twice, but we don't see two encrypt() calls in the code from the question. Probably some other code layer was making that.

Form submit going to the wrong route

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.

How to clear form input after save?

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

Laravel not reaching update method and returns edit view again – route wrong

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.

Resources