Append data to form using through DOM? - laravel

Form which uses POST method:
<div>
<form action="{{ route('post.store') }}" method="POST">
<input type="text" name="name">
<input type="text" name="text">
<button type="submit">Submit</button>
</form>
#foreach ($comments as $comment)
{{ $comment->text }}
Reply
#endforeach
</div>
Array of $comments is being returned when the view itself is returned.
Basically, I am intrested if there is a way to append $comment->id to the data that will be sent to server on Submit button click using Laravel Blade or AlpineJS, but without creating any additional functions between <script> tags? I mean, is it possible to do something like this: Reply?
EDIT:
I expressed myself wrongly. I don't want to append new id every time Reply is clicked, but to overwrite corresponding property in data which is going to be sent to server when Submit button is clicked with the $comment->id for which Reply was clicked for.

I assume you are using Laravel Resource Controller
Not sure I totally understand why you want to send all comment ids when submitting the form but when calling the route post.store you should better send the post id
<form action="{{ route('post.store', ['post' => $post]) }}" method="POST">
If you want to get all comment id and have proper model relationships set up in your models you can get all comments for a post in your controller.
To send a specific id in your form create a new <input name="comment_id" value=""> and let that field be populated.

Related

Laravel Post method is not working because of the special characters

I am facing a very abnormal situation here.
Below are the image of simple HTML form, with input type files and text and text area.
<form method="POST" action="{{ route('vendor.package.store') }}" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="text" class="form-control" id="title" name="package_name" value="{{ old('title')) }}" required>
</form>
When I enter "On-The-Day (OTD) Vacation In Bali Island", and hit "Submit Button", Laravel redirect me to the index page, means the below function does not trigger at all.
public function store(Request $request){
return $request->all();
}
So, I tried to answer enter "(OTD)" & "On-The-Day", Laravel redirect me to the store function
But whenever I tried to enter "On-The-Day (OTD)", fails.
When I try to enter "-Day (OTD)", fails
When I try to enter "-DaZ (OTD)", success.
P/S: my local development can accept On-The-Day (OTD) Vacation In Bali Island, only in production side cannot process the word.
Appreciate if you could help

laravel 5.5 | old() empty in view unless $request->flash() used

I've run into an odd issue where the helper function old() always returns null in a blade view unless $request->flash() is used prior to loading the view. I have never had to do this when using laravel in the past. Did something change or is there something that I have forgotten to set/configure. Below is a simple example of the behavior:
web.php
Route::get('/test', function(){
return view('testView');
});
Route::post('/test', function(Illuminate\Http\Request $request){
$request->flash(); // if uncommented old() works, if commented old() does not work
return view('testView');
});
form in testView.blade.php
<form action="/test" method="POST">
{{csrf_field()}}
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
after form submitted without $request->flash()
after form submitted with $request->flash()
EDIT
Thinking this might have something to do with using a single route name for both post and get methods, the form was changed so to submit via get, and the issue persists. For example:
web.php
Route::get('/test', function(function(Illuminate\Http\Request $request){
return view('testView');
});
form in testView.blade.php
<form action="/test" method="GET">
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
Use redirect back() instead of loading view directly in a post method.
return redirect()->back()->withInput();
You need to flash request data to put old input into session, otherwise old() will return empty result. See official doc here.

Laravel 5.3 Form post error session store not set on request

I have a public view with a form. To access the form my route is admin.new_athlete when I post the form the action calls admin.create_athlete
I have this statement in form {{ csrf_field() }} just before the first label. This is after the submit button <input type="hidden" name="_token" value="{{ Session::token()}}">
Attached is web.php and controller.
Please advise on the error.

How to populate edit form fields in laravel 5.3

I have a problem in populating edit form fields in my first Laravel 5.3 application. In examples I have found online, they have used Form facade to do it. However, I prefer to use plain html.
This is my controller action
public function edit()
{
$profile = Profile::find(Auth::user()->id);
return view('profile.edit', ['profile' => $profile]);
}
In my view file I have a form like this
<form method="POST" action="{{ url('profile/update') }}">
{{ csrf_field() }}
<input id="name" type="text" name="name" value="{{ old('name') }}">
...
</form>
Problem is, above input field is not populated with the value of the $profile->name.
I can get it to work, setting the value attribute like
{{ (old('name')) ? old('name') : $profile->name }}
Since I'm new to Laravel framework I want to know if there is a better way to do that.
Use old($key, $default). It will return $default if $key is not found.

Get value other than input polymer+laravel

I am trying to use laravel with polymer. I have built a form that looks like this
<form action="{{ url('settings/update') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<h4>Choose your theme</h4>
<paper-radio-group selected="1">
<paper-radio-button name="1" label="Theme Default"></paper-radio-button><br/>
<paper-radio-button name="2" label="Theme Violet"></paper-radio-button><br/>
<paper-radio-button name="3" label="Theme Orange"></paper-radio-button><br/>
</paper-radio-group><br/>
<button type="submit" class="mui-btn mui-btn-primary">Change Theme</button>
</form>
But I have no idea how to retrieve those fields in my controller. Any help would be appreciated.
Form elements don't know how to work with custom elements like paper-radio-button. You could replace your form with ajax-form or use some JavaScript to handle the form submit event, gather up the values, and send the request yourself.

Resources