Form submit going to the wrong route - laravel

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.

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');

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 undefine variable in view

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.

Javascript file not being able to make an Ajax call to laravel

Hi I was hoping someone can help me. my root folder for my app is cloudapp.azure.com/index.php. cloudapp.azure.com/ is my public folder. So all my views are being rendered like so cloudapp.azure.com/index.php/welcome1 from my routes and controllers. It all workrs fine.
Route::post('/welcome1a', [
'uses'=>'UserController#postRegister',
'as' => 'register',
]);
Route::post('/welcome1', [
'uses'=>'UserController#postRegister1',
'as' => 'register1',
]);
public function postRegister1(Request $request){
$data1 = $request->all();
$data = JSON.parse($data1);
return response()->json(['result' => 'data1']);
}
The issue I'm facing is I will be working on phone gap so my views need to be in html. My HTML files are stored in my public folder (ie cloudapp.azure.com/) so a view will be like cloudapp.azure.com/register.html.
I am trying to send data from cloudapp.azure.com/js/script1.js in my cloudapp.azure.com/register.html view like so to the url that is recognised by my laravel application;
$.ajax({
url: url,
type: 'POST',
data: details,
dataType: "json",
success: function(result) {
console.log('result');
}
});
but it seems that the js file and the url are not communicating to each other. Is my file structure wrong? when I use blade view to do an Ajax call its fine but with my html files calling the url via ajax, it doesnt work. Even when I try to get my old laravel views via;
$.get("cloudapp.azure.com/index.php/welcome1"); it doesnt work.
I have triend making the ajax call to the following urls;
cloudapp.azure.com/index.php/welcome1
index.php/welcome1
and /welcome1.
There are no errors, but just not communication to each other.
If anyone can help you will be a LIFE SAVER
public function postRegister1(Request $request){
$data1 = $request->all();
$data = json_decode($data1);
return response()->json(['result' => 'data1']);
}
JSON.parse is javaScript not PHP.
json_decode() is what you use in PHP.
http://php.net/manual/en/function.json-decode.php
But you shouldn't have to json_decode it either, $request->all() returns an array of the posted values.
I would say, remove the whole json stuff and then see if your ajax call is getting back the return value.
public function postRegister1(Request $request){
return response()->json(['result' => 'testing...']);
}
Then go from there...
You are calling a JS function JSON.parse in PHP.
So instead do
public function postRegister1(Request $request){
return response()->json([ 'result' => $request->all() ]);
}
Or if you simply return just an array, laravel will automatically cast it into JSON for you
public function postRegister1(Request $request){
return [ 'result' => $request->all() ];
}
You should try removing the <base> tag on your <head>

How call route resource in Laravel

I have problem. My route definition contains:
route::resource('admin/settings/basic','admin\settings\BasicController');
but I don't know how can I call the edit action from basiccontroller in my a href link.
href='{{ link_to_route('admin/settings/basic/edit') }}'
Please give me some advice.
Given a route like the following:
app/Http/routes.php
Route::resource('profile', 'ProfileController');
Your controller could look something like this:
app/Http/Controllers/ProfileController
public function edit($id)
{
$profile = Profile::all(); // Grab some data
return view('profile.edit', [$profile]); // Pass some data to the Edit view
}
In the view, you might have a form for editing like so:
resources/views/profile/edit.blade.php
<?= Form::model($profile, ['route' => ['profile.update', $profile->id], 'method' => 'PUT', 'class' => 'form-horizontal']) ?>
That form routes to ProfileController#update
For other routes, such as an index, it is handled all for you. You just have to make sure you return the correct view in your ProfileController#index, and hitting the route for /profile will be passed through that method
You can always refer to the documentation as well - RESTful Resource Controllers

Resources