Laravel post route with parameters - php

Im trying to do a post request via a form in my blade template with a parameter in the url .
Here is my form:
<form action="/products/{{$produit->id}}" method="post">
{{ csrf_field() }}
<input type="text" name="avis_body" />
<input type="submit" value="Send" />
</form>
Here is my web.php:
Route::post('/products/{$id}', function($id){
return $id;
});
I don't know what im doing wrong. Im receiving this error:

Remove the $ from route declaration:
Route::post('/products/{id}', function($id){
return $id;
});

Related

Redirect() Doesnt work after POST Submit with #csrf, keeps on reloading the page

i just started laravel and i am trying to create a simple post form for practive, when submitted it gives an 419 Error so I used #csrf inside the form.
HTML Form:
<form method="POST" action="/posts">
#csrf
<input type="text" name="title" placeholder="Enter Title">
<input type="submit" name="submit">
</form>
Route:
Route::resource('/posts', 'PostController');
Store Function:
public function store(Request $request)
{
//
$post = new Post;
...
$post->save();
return redirect('/posts'); //Tried
return redirect()->route('posts.index');
}
#csrf fixed the 419 error and made the post work.
The Post works only when there is no redirect(). I think the #csrf is making the redirect() not work.
I've been searching and couldn't find a solution. How do I fix this?
make action="{{ route('posts.store') }}" instead of action="/posts"
make return redirect()->route('posts.index');

why comming from <a> route "ruta/{{$var}}/edit" and a 'action': "ruta/{{$var}} my final route is: ""ruta/{{$var}}/"ruta/{{$var}}"?

I have a view whit the next code:
<h1>Edicion</h1>
#foreach ($usuarios as $usu)
<h4>{{$usu->nombre}}</h4>
editar
#endforeach
This route:
Route::resource('/prurequests','PruebasControllers\PrurequestsController');
The method edit:
public function edit($slug)
{
$usuario = Usuario2::where('slug','=',$slug)->firstOrFail();
return view('vistaspruebas.edit', compact('usuario'));
}
In this route my URL is:
/public/prurequests/vaca/edit
The code in this view 'vistaspruebas.edit' is:
<form action="prurequests/suma" method="POST">
#method('PUT')
#csrf
<label for="nombre">ingrese nombre</label>
<input type="text" name="nombre" value="{{$usuario->slug}}">
<br />
<button type="submit" name="" value="submit">Actualiza</button>
</form>
instead of looking for this route: "prurequests/suma " Laravel look for /public/prurequests/vaca/prurequests/suma
someone knows why after the tag and call other route it removes 'edit' and change it by and other route i put here?
Please use action() helper method in your form as the following:
<form action="{{ action('Controller#method') }}" method="POST">
Or you can use the route helper method as the following:
<form action="{{ route('route_name') }}" method="POST">
Also you can use url() helper if you want to use a given path:
<form action="{{ url('path_here') }}" method="POST">

upload file didn't work in laravel 5.1

I want to upload a .CSV file to insert some records to my Database.
Unfortunately, it doesn't work.
My Routes:
Route::get('excel/import','Backend\ExcelController#getImport');
Route::post('excel/import','Backend\ExcelController#postImport');
My Controller:
public function getImport(){
$csrf_field = csrf_field();
$postUrl='import';
$html = <<<CREATE
<form action="$postUrl" method="post">
$csrf_field
<input type="file" name="importCsv" formenctype="multipart/form-data" ><br/><br/>
<input type="submit" value="submit"/>
</form>
CREATE;
return $html;
}
public function postImport(Request $request){
//get file
$file = Input::file('importCsv');
dd($file);
$upload=$request->file('importCsv');
dd($upload);
}
It just print null if I use :
Input::file('importCsv');<br>
And nothing if I use :
$request ->file('importCsv');<br><br>
So, I've tried to print like dd($request->all()); <br>
array:2 [▼
"_token" => "wPuAXUvSItR4MFJ4bAjQhanaf0W9avrqR2PgjxcU"
"importCsv" => "T_OpusDef.csv"
]
I could get only the name, and when I want the the path by getRealPath(), It told me :
FatalErrorexception: call to a memeber function getRealPath() on null
I need your help, Thanks a lot
The form should look like this:
<form action="{{ $postUrl }}" method="post" enctype="multipart/form-data">
{{ $csrf_field }}
<input type="file" name="importCsv"><br/><br/>
<input type="submit" value="submit">
</form>
use {{csrf_field()}} instead of $csrf_field if its in blade and csrf_field() if its in controller

Laravel Route resource MethodNotAllowedHttpException on destroy method

I am using a resource Laravel route defined by the following line in my routes.php :
Route::resource('test', 'App\Controllers\Teacher\TestController', ['only' => ['index', 'create', 'destroy']]);
The index method works fine. In the template of index I have created a form in order to remove an item of the list.
<form method="DELETE" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="submit" value="Remove" />
</form>
The URL is correctly generated by Laravel but when I post this form I get the following error :
exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in /var/www/project/bootstrap/compiled.php:5365
I have already try to change DELETE by POST in the method attribute of my form but it doesn't work.
I also read this post but it doesn't help me : MethodNotAllowedHttpException on resource defined method Laravel-4
When you manually create your form you should use POST as method, and use _method input with delete value this way:
<form method="POST" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Remove" />
</form>
Reference in Laravel documentation for form method spoofing
Try this:
<form action="/test" ....>

How to get data transfer from the form into database Laravel 5

I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">

Categories