I am trying to pass in a value from a form into the controller.
my blade
<div class="modal fade" id="endModals{{ $example->ID }}" tabindex="-1" role="dialog" aria-labelledby="endModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header"
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Are you sure you want to End this Task?
Please Select a day to End this Task.
<input type="date" id="EndDate" name="EndDate" title="Date" class="form-control" required
value="{{ isset($searchParams['EndDate']) ? $searchParams['EndDate'] : Carbon\Carbon::now() }}"
>
</p>
</div>
{{ Form::model($example, ['route' => [‘example.end', $example->ID ?? 0], 'method' => 'PUT']) }}
#method('PUT')
<div class="modal-footer">
<button type="button" onclick="end()" class="btn btn-primary">End</button>
{{ Form::submit('End', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
I am trying to get the value of EndDate :
function giveDate() {
let Date = $('#EndDate').val();
$.ajax({
url: 'example/end',
type: 'POST',
data: {
'Date': Date,
},
success: function (response) {
console.log(Date)
},
error: function () {
alert('An error occurred');
},
});
}
window.giveDate = giveDate
And in my controller I have this function public function
$EndDate = $request->input('EndDate');
$end = ClientMedicationRecurrence::end(
$id,
$EndDate
);
I however keep getting back the error of
Argument 2 passed to App\Example::end() must be an
instance of DateTime, null given, called in
How do I get the value of EndDate back into the controller ?
Date is actually a reserved JavaScript keyword so avoid using it. In your ajax call do like this:
let get_date = $('#EndDate').val();
$.ajax({
url: 'example/end',
type: 'POST',
data: {
'EndDate': get_date,
},
success: function (response) {
console.log(Date)
},
error: function () {
alert('An error occurred');
},
headers: {
// Add The CSRF Token to the Ajax header.
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
});
Also, notice that I have changed the name of the data being passed as "EndDate", so that you can access it in your controller.
You have also forgotten to submit CSRF token as well which is not the Laravel way of form submission. So, I have added it in the Ajax header.
I found solution that does not require AJAX or jquery
I needed to place the input under the #method('PUT') that was already in my blade
so this:
<div class="modal fade" id="endModals{{ $example->ID }}" tabindex="-1" role="dialog" aria-labelledby="endModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header"
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Are you sure you want to End this Task?
Please Select a day to End this Task.
<input type="date" id="EndDate" name="EndDate" title="Date" class="form-control" required
value="{{ isset($searchParams['EndDate']) ? $searchParams['EndDate'] : Carbon\Carbon::now() }}"
>
</p>
</div>
{{ Form::model($example, ['route' => [‘example.end', $example->ID ?? 0], 'method' => 'PUT']) }}
#method('PUT')
<div class="modal-footer">
<button type="button" onclick="end()" class="btn btn-primary">End</button>
{{ Form::submit('End', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
becomes this :
<div class="modal fade" id="endModals{{ $example->ID }}" tabindex="-1" role="dialog" aria-labelledby="endModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header"
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Are you sure you want to End this Task?
Please Select a day to End this Task.
</p>
</div>
{{ Form::model($example, ['route' => [‘example.end', $example->ID ?? 0], 'method' => 'PUT']) }}
#method('PUT')
<input type="date" id="EndDate" name="EndDate" title="Date" class="form-control" required
value="{{ isset($searchParams['EndDate']) ? $searchParams['EndDate'] : Carbon\Carbon::now() }}"
>
<div class="modal-footer">
<button type="button" onclick="end()" class="btn btn-primary">End</button>
{{ Form::submit('End', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
my controller function then just needed a slight tweak
$EndDate = $request->input('EndDate');
$end = ClientMedicationRecurrence::end(
$id,
$EndDate
);
Related
So i want to make admin can change any user's password. but i got this error
Call to a member function update() on null
I have no idea what to do
this is my controller
public function resetPassword(Request $req, $id)
{
$req->validate([
'new_password' => ['required'],
'new_confirm_password' => ['same:new_password'],
],
[
'new_password.required' => 'Password Baru Harus Diisi !',
'new_confirm_password.same' => 'Password Baru Harus Sama Dengan Confirm Password !',
]);
User::find($id)->update(['password'=> Hash::make($req->new_password)]);
return redirect('/admin/list_user')->with('status', 'Password Berhasil di Ubah !');
}
this my route
Route::put('/admin/{id}/reset/password', 'PageController#resetPassword')->name('resetPassword');
this is my view modal
<div class="modal fade" id="resetModal" tabindex="-1" role="dialog" aria-labelledby="resetModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Reset Password</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="/admin/{id}/reset/password" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="form-group">
<label for="password">Password Baru</label>
<input name="new_password" type="password" class="form-control" id="new_password" required>
</div>
<div class="form-group">
<label for="password">Confirm Password</label>
<input name="new_confirm_password" type="password" class="form-control" id="new_confirm_password" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
<button type="submit" class="btn btn-primary">Buat</button>
</form>
</div>
</div>
</div>
</div>
</div>
<form action="/admin/{id}/reset/password" method="POST">
You're not passing any id. The route picks up '{id}' as the id, then tries to find an User with the id '{id}' and finds none. (->first() returns null).
Just change that opening form tag's action attribute:
<form action="{{ route('resetPassword', ['id' => Auth::id()]) }}" method="POST"> (or instead of Auth::id() the user_id you're trying to update.
You could also use findOrFail($id) instead of find($id) so you'll get a clearer error message if an User is not found.
In my Laravel-5.8, I have:
public function manager_employee_goal_list($id)
{
$goal = AppraisalGoal::findOrFail($id);
$goaldetails = AppraisalGoalDetail::where('appraisal_goal_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal_list')
->with('goal', $goal)
->with('goaldetails', $goaldetails);
}
public function manager_employee_goal_approve(Request $request, $id)
{
$goal = AppraisalGoal::find($id);
$goal->is_approved = 3;
$goal->line_manager_comment = $request->line_manager_comment;
$goal->save();
Session::flash('success', 'Goal is approved');
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
}
view : manager_employee_goal_list
<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#approve{{ $goal->id }}" data-original-title="Approve">
Approve
</a>
<div class="modal fade" id="approve{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{route('appraisal.appraisal_goals.manager_employee_goal_approve',['id'=>$goal->id])}}" method="post">
{{ csrf_field() }}
<div class="modal-header">
Approve Goal
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Comment</label>
<textarea rows="2" name="line_manager_comment" class="form-control" placeholder="Enter Comment here" value="{{old('line_manager_comment')}}" required data-validation-required-message="This field is required"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success btn-ok">Approve Goal</button>
</div>
</form>
</div>
</div>
</div>
route/web.php
Route::get('appraisal_goals/manager_employee_goal_list/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_list')->name('appraisal.appraisal_goals.manager_employee_goal_list');
Route::post('appraisal_goals/manager_employee_goal_approve/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_approve')->name('appraisal.appraisal_goals.manager_employee_goal_approve');
When I submit on the modal form, it saves but couldn't redirect to:
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
It displays this error
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal_list(), 0 passed and exactly 1 expected
How do I resolve it?
Thanks
manager_employee_goal_list method expects an ID, all you have to do is to pass that ID, like so:
return redirect()->route(
'appraisal.appraisal_goals.manager_employee_goal_list',
['id' => $id]
);
Also I suggest you to either remove question mark after id in your defined route, or update arguments from your method by making them nullable
public function manager_employee_goal_list($id = null)
and of course you have to adjust code too
i have created a modal for file uploading, this is a client request, so i need to do it this way, i have already this:
{{-- Single Take Photo modal --}}
<div class="modal modal-success fade" tabindex="-1" id="takephoto_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="{{ __('voyager::generic.close') }}"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="voyager-photo"></i> Tomar Foto</h4>
</div>
<div class="modal-footer">
<form action="#" id="takephoto_form" method="POST" enctype="multipart/form-data" method="post" files="true">
{{ csrf_field() }}
<input type='file' accept="image/x-png, image/jpeg" capture="camera"/>
<input type="submit" class="btn btn-success pull-right" value="Tomar Foto">
</form>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">{{ __('voyager::generic.cancel') }}</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Now, the js:
$('td').on('click', '.takephoto', function (e) {
$('#takephoto_form')[0].action = '{{ route('voyager.'.$dataType->slug.'.submitphoto', ['id' => '__id']) }}'.replace('__id', $(this).data('id'));
$('#takephoto_modal').modal('show');
});
And the function in controller:
// POST
public function submitPhoto(Request $request, $id)
{
$input = Input::all();
dd($input);
die();
}
Ok, everything is working, the js is taking the action to the function, and sending the id so i can update then the db, this is an edit function, this is what im getting, no files ,
Im using Laravel Voyager, its Laravel...so i can use any Laravel class.
Already fixed by adding an id on html:
<input type='file' accept="image/x-png, image/jpeg" name="photo" id="photo" capture="camera"/>
Then you can do $request->hasFile('photo');
Here is my button:
my $post->comment contained id, comment, email and name;
I want to pass the comment to the form inside the modal.
I also want to use the id to the 2nd parameter of the route to update the comment
#foreach($post->comments as $comment)
<button class="btn btn-success btn-xs " data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>
#endforeach
Here is my modal:
<!-- Modal -->
<div class="modal fade modal-md" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×
</button>
<h4 class="modal-title">Comment Update!</h4>
</div>
<div class="modal-body">
<div id="comment-form" class="">
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
<div class="row">
<div class="col-md-12">
<b>{{ Form::label('comment', "Comment:") }}</b>
{{ Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '5', 'id'=>'comment ']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
<div class="modal-footer">
<a href="{{route('comments.store',$post->id) }}"
onclick="event.preventDefault();
document.getElementById('comment-update').submit();" class="btn btn-primary">
Update </a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
I tried this Script but it wont work:
<script type="text/javascript" charset="utf-8">
$('#myModal').on('show', function(e) {
var link = e.relatedTarget();
var id = link.data("id");
var comment = link.data("comment");
var modal = $(this);
modal.find("#id").val(id);
modal.find("#comment").val(comment);
});
you must using on click event in javascript
example:
<button class="btn btn-success btn-xs add" data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>
this script.
$(document).on('click', '.add', function() {
$('#id_kategori').val($(this).data('id'));
$('#comment').val($(this).data('comment'));
$('.form-horizontal').show();
$('#myModal').modal('show');
});
i hope my answer helping you
I'm new to Vue JS and I'm building an application with Laravel Spark and trying to utilize Vue as much as possible.
I have a form to simply add an 'Asset Type' with a component. Once the Asset Type is successfully created, a list of properties is grabbed from the database and set to a 'data' attribute. In my view(I'm using an inline template), I have a 'v-for' that creates a form for each property that has two hidden inputs for the property id and the type id, and one "Add" button that assigns the property to the newly created type.
THE PROBLEM:
I can't seem to assign the value of the hidden inputs within the view while using v-models. When I submit one of the forms, the form request data always returns the initial data value from the new SparkForm object.
In other words, I need to assign the hidden input values within the v-for loop in the view.
Here's my component:
Vue.component('new-asset-type', {
props: [],
data() {
return {
// type_id: this.type_id,
properties: this.properties,
newAssetType: new SparkForm({
label: null,
enabled: false,
}),
assignPropForm: new SparkForm({
prop_id: "",
type_id: "",
}),
};
},
methods: {
createType: function () {
Spark.post('/asset-types', this.newAssetType)
.then(response => {
this.type_id = response.type_id;
axios.get('/getTypeNotProps/' + this.type_id).then((response) => {
this.properties = response.data;
console.log(this.properties);
});
})
.catch(response => {
console.log("fail");
});
},
assignProp: function () {
Spark.post('/asset-properties/add', this.assignPropForm)
.then(response => {
console.log(response);
})
.catch(response => {
console.log("fail");
});
}
}
});
And here's my view:
#extends('spark::layouts.app')
#section('content')
<new-asset-type inline-template>
<div class="container">
<!-- Application Dashboard -->
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Add a New Asset Type</div>
<div class="panel-body" id="addTypeForm">
<div class="form-horizontal">
<div class="form-group" :class="{'has-error': newAssetType.errors.has('label')}">
{{ Form::label('label', 'Label', ['class' => 'col-md-4 control-label']) }}
<div class="col-md-6" >
<input type="test" name="label" v-model="newAssetType.label">
<span class="help-block" v-show="newAssetType.errors.has('label')">
#{{ newAssetType.errors.get('label') }}
</span>
</div>
</div>
<div class="form-group">
{{ Form::label('enabled', 'Enabled?', ['class' => 'col-md-4 control-label']) }}
<div class="col-md-6">
<input type="checkbox" name="enabled" v-model="newAssetType.enabled" >
</div>
</div>
<!-- Submit -->
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button class="btn btn-primary" #click="createType" :disabled="newAssetType.busy">
Create Asset Type
</button>
</div>
</div>
<div id="assignProps" v-if="newAssetType.successful">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Add Property
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add New Property to Asset Type</h4>
</div>
<div class="modal-body">
<assign-asset-prop v-for="property in properties" class="panel panel-info property-item">
<div class="panel-heading">#{{ property.label }}</div>
<div class="panel-body"><strong>Input Type: </strong>#{{ property.input_type }}
<div class="pull-right">
<input type="hidden" name="prop_id" v-bind:value="property.p_id" v-model="assignPropForm.prop_id">
<input type="hidden" name="type_id" v-bind:value="property.p_id" v-model="assignPropForm.type_id">
<button class="btn btn-primary" #click="assignProp" :disabled="assignPropForm.busy">
Add
</button>
</div>
</div>
</assign-asset-prop>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</new-asset-type>
#endsection
Thanks to the helpful comments, I learned that I shouldn't have been using hidden inputs at all. Instead, I simply passed the variables to the function on the click event.
<button class="btn btn-primary" #click="assignProp(type_id, property.p_id)" >
Add
</button>
Then in my component
methods: {
assignProp: function (type_id, property_id) {
var assignPropForm = new SparkForm({
propvalue: property_id,
typevalue: type_id,
});
Spark.post('/asset-properties/add', assignPropForm)
.then(response => {
console.log(response);
})
.catch(response => {
console.log("fail");
});
}
}
You need store variables at local data() dep., and geting it by getters function.