laravel 8 , How to display data from a FORM with post method? - php

my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image

Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.

Related

simple input type file not getting the file

Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;

Laravel 8: using the same form for create & edit (not inserting some data)

Possible duplicates:
Laravel 8 use same form for edit and create ,
Laravel use same form for create and edit
I refactored and simplified the controller by following this guide
The form works with insert and edit but some data aren't being sent to DB such as:
ZoneNumber, ClaimingDate, receivedDate, lostItem
I don't see any typos and can't find anything that prevents from saving these data.
I made sure the form renders based on condition:
#if($$lostFound->exists)
<form id="LostFoundForm" method="POST" action="{{ route('data-entry.lost-and-found.update', $lostFound) }}" enctype="multipart/form-data">
#method('put')
#else
<form id="LostFoundForm" method="POST" action="{{ route('data-entry.lost-and-found.store') }}" enctype="multipart/form-data">
#endif
#csrf
And I also have the old() helper in the blade like so:
<div class="form-floating mb-10">
<input type="text" min="0" class="form-control #error('lostItem') is-invalid #enderror" name="lostItem" placeholder="lostItem" value="{{ old('lostItem', $lostFound->lostItem) }}">
<label for="lostItem">Item Name</label>
</div>
My controller looks fine...I don't see any issues here?
public function create()
{
return $this->edit(new LostFound());
}
public function store(LostFoundRequest $request)
{
return $this->update($request, new LostFound());
}
public function edit(LostFound $lostFound)
{
$locationList = Locations::all();
return view('pages.dataEntry.lostFound._addForm', compact('lostFound', 'locationList'));
}
public function update(LostFoundRequest $request, LostFound $lostFound)
{
$request->persist($lostFound);
return redirect(route('data-entry.lost-and-found.index'))->with('success', 'LF updated successfully.');
}
The persists() function is for saving the data:
public function persist(LostFound $lostFound)
{
// TODO: See if this can be refactored more.
return LostFound::create( $this->validated() )
->addMedia($this->LFImage)
->toMediaCollection('lost-Items');
$this->save();
}
Yes, as Mash tan and EHF Shahab said in the comments. ZoneNumber, ClaimingDate, receivedDate, lostItem are not added in the validation array

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

Trying to submit one <form> with method="POST" duplicates the route and doesn't save the information with an store function()

Im trying to submit one form with method post to save one element in astore function() but when I click submit duplicates the route, for example looking like this:entradas/1/ventaEntrada/200000/montoTotal/entradas/1/ventaEntrada/200000/montoTotal and doesn't save the information, I dont know why this is happening
Below i will let the code of my route,my form,my create function() and my store function()
Also show404 Not Found when I click submit
Route
Route::resource('/entradas/{id_entrada}/ventaEntrada/{precio_entrada}/montoTotal', 'Venta_entradaController');
create function()
public function create(Request $request,$id_entrada,$precio_entrada){
$venta_entrada = DB::select(DB::raw(
"SELECT monto_total,fecha,fk_cliente_natural,fk_cliente_juridico
FROM venta_entrada "
)
);
return view('home.crearVenta_entrada')
->with('venta_entrada',$venta_entrada)
->with('id_entrada',$id_entrada)->with('precio_entrada',$precio_entrada);
}
store function()
public function store(Request $request,$id_entrada,$precio_entrada)
{
$venta_entrada=new Venta_entrada();
$venta_entrada->monto_total=$precio_entrada+$request->monto_total;
$now = new \DateTime();
$venta_entrada->fecha=$now->format('d-m-Y');
$venta_entrada->fk_cliente_natural=1;
$venta_entrada->save();
return back();
}
Form with the method POST
<form action="entradas/{{$id_entrada}}/ventaEntrada/{{$precio_entrada}}/montoTotal" method="POST">
#csrf
<input type="number" name="monto_total" placeholder="Monto total" class="form-control mb-2" required>
<button clas="btn btn-primary btn-block" type="submit">ADD</button>
BACK
</form>
I have check your problem and you have use "Resource Route" right.
so for routing run following command in your terminal/command prompt
php artisan route:list
It will show all routes lists and find your action name
Then you'll add in form action name like
<form action="{{ route('name') }}" method="post">
...
</form>
Example:
<form action="{{ route('Venta_entrada.store') }}" method="post">
...
</form>
I hope it will help you :)

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