passing data via redirect laravel 5 - variables

I have some problems when passing data via redirect in laravel 5, i get some samples code.. here my code
controller
if($validator->fails()){
return redirect()->back()->withErrors($validator->errors())->withInput();
}else{
if (Hash::check($request->old_password, $employee->password)){
$user = [
'username' => $input['username'],
'password' => Hash::make($input['password']),
];
$employee->fill($user)->save();
return redirect('/employees');
}else{
dd('test');
$error = 'Your old password is incorrect';
return redirect()->back()->with('error',$error);
}
}
view
<div class="form-group">
<label for="old_password" class="col-sm-2 control-label">Password Lama</label>
<div class="col-sm-5">
<input type="password" class="form-control" name="old_password" placeholder="Password Lama" required />{{ $errors->first('old_password') }}{{ $error = session('error') }}
</div>
</div>
there is no error message, but $error cannot display my messages.. anyone can help me?
thanks a lot...

}else{
dd('test');
$error = 'Your old password is incorrect';
return redirect()->back()->with('error',$error);
}
with dd('test'), you are exiting the application before a value was stored in session. remove that and let redirect.
and secondly, while dealing with session, do not use dd(). it may create undesirable result as session is a terminable middleware.

The DD command returns info to the browser and then stops executing code. Try commented this out.

Related

Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP) laravel 6

Hi I was create one view and created one controller.
I want to give validation to form input field using bydeault Laravel error but I m getting error
"Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)"
My code of controller and view as follow:
index.blade.php
#extends('app')
#section('title','Services Page')
#section('content')
<h1>Welcome to laravel 6 Services Section</h1>
<h4>lets do something better</h4>
<form action="/service" method="POST">
<input type="text" name="fname" autocomplete="off">
#csrf
<button>Add Service</button>
</form>
#error('fname') {{ message }} #enderror
<ul>
#forelse($services as $service)
<li>{{ $service->name }}</li>
#empty
<li>No services Available</li>
#endforelse
</ul>
#endsection
nd controller as follows :
ServiceController.php
public function store()
{
$name_validate = request()->validate([
'fname' => 'required'
]);
$service = new \App\Service();
$service->name = request('fname');
$service->save();
return redirect()->back();
// dd(request('fname'));
}
But I am getting error when form submit button not validate.
Any issue with this code let me know. I am new learner in laravel
Add the $ to the message:
#error('fname') {{ $message }} #enderror

How to make cypress different test depending on if there are rows

In #vue/cli 4.0.5 app making cypress for a bloc like
<fieldset class="blocks m-1 p-1">
<legend class="blocks block-wrapper-for-data-listing">Events </legend>
<div class="table-responsive table-wrapper-for-data-listing" v-if="taskRow.events.length">
<table class="table table-striped table-data-listing">
...
<!--- DATA TABLE -->
</table>
</div>
<p class="alert alert-info m-1 p-1 wrapper-for-no-rows-data-listing" role="alert" v-if="!taskRow.events.length">
This task has no events yet!
</p>
</fieldset>
I want to make different conditions depending on if there are taskRow.events rows.
I made rules when there are taskRow.eventsL :
var view_event_details_found = false;
cy.get(".block-wrapper-for-data-listing")
.get(".table-wrapper-for-data-listing")
.then(listing => {
alert("::-1");
cy.get(".tr_view_event_details")
.find(".view_event_details")
.first()
.click();
cy.contains(".modal-title", "Event Details");
view_event_details_found = true;
});
alert("view_event_details_found::" + view_event_details_found);
if (!view_event_details_found) {
}
I tried when view_event_details_found = false to make other test on
search “This task has no events yet!” text.
But I got error :
CypressError: Timed out retrying: Expected to find element: '.table-wrapper-for-data-listing', but never found it.
as without div with “.table-wrapper-for-data-listing” defined.
Which is valid way to make this test ?
Thanks!
Reading this https://docs.cypress.io/guides/core-concepts/conditional-testing.html#The-DOM-is-unstable
docs I made tests :
cy.get('.block-wrapper-for-data-listing').then(($legend) => {
let $nxtElement = $legend.next()
if ($nxtElement.hasClass('table-wrapper-for-data-listing') ) {
cy.get('.tr_view_event_details').find('.view_event_details').first().click()
cy.contains('.modal-title', 'Event Details')
} else {
cy.get('p.wrapper-for-no-rows-data-listing').contains('This task has no events yet!')
}
})
and that works for me in both cases whe taskRow.events.length == 0 and taskRow.events.length> 0 !

Unkown column error for _method and _token in Laravel PUT form request. What is the cause?

Column not found: 1054 Unknown column '_method' in 'field list'
Getting the above unknown column error for _method and _token but I have $fillable set in my model. What could be causing the error? I know the solution is Input::except('_method') in my controllers but I would like to understand the context of the issue.
Here is my code
protected $table = 'personaldetails';
protected $fillable = [
'address',
'city',
'country',
'postcode',
];
The relavant top part of my blade form...
<form action="{{ route('students.update', [$student->id]) }}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" value="{{ Session::token() }}" name="_token">
<div class="row">
also tried with laravel { { csrf_field() } }.
public function update(Request $request, $id)
{
$inputData = $request->all();
$Student = Student::find($id);
$Student->personaldetail()->update($inputData);
return redirect()->route('Student.index')->with('message','Studenthas been updated'); }
Anyone else come across this too?
Per your comment response:
#JanWillem and tam I have added my controller that is bringing this
error up. I am unclear why it tries to store _method and _token. These
are not form values I want to store.
It's trying to store _method and _token because you're attempting to update the model w/ these attributes: $Student->personaldetail()->update($inputData);
As you've alluded to, you'll need to prune the list of attributes you'd like to update within the model from $inputData = $request->all(); to $inputData = $request->except('_method', '_token');

Google Script for Uploading File Not Working

I wrote a script for uploading files to my Google Drive using the Google Script. I deployed it as a WebApp but it's not working and I don't know why. The button just gets stuck on "Subiendo..." and never changes anything inside my Drive. I'm sure I gave the script all the permissions and I already tried to see what's happening on my own without any sucess. Can you maybe help me find what should I do? I post the code here:
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('main').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function serverFunc(theForm) {
try{
var folder_name = "publicaciones";
var folder,folders = DriveApp.getFoldersByName(folder_name);
if(folders.hasNext()){
folder = folders.next();
}else{
folder = DriveApp.createFolder(folder_name);
}
Logger.log("TheForm", theForm == null);
var blob = theForm.theFile;
Logger.log("Blob!", blob == null);
var file = folder.createFile(blob);
Logger.log("File:", file.getName());
return "Archivo subido exitosamente: " + file.getUrl();
} catch(error){
Logger.log("error: ", error.toString());
return error.toString();
}
}
** main.html **
<div>
<form id="myForm">
<input type="file" name="theFile">
<input type="hidden" name="anExample">
<br>
<input type="button" value="Subir Archivo" onclick="this.value='Subiendo...';
google.script.run.withSuccessHandler(fileUploaded).serverFunc(this.parentNode);
return false;">
</form>
</div>
<div id="output">
</div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
I'd appreaciate any help or pointers you can give me. Thanks a lot!
Looks like there is a bug currently that Google is working on. I found a possible fix:
Change your input tag to this:
<input type="button" value="Subir Archivo" onclick="callServerCode()"/>
Add a function to the <script> tag:
<script>
function callServerCode() {
var formToSend = document.getElementById('myForm');
google.script.run
.withSuccessHandler(fileUploaded)
.serverFunc(formToSend);
};
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
Note that inside the new function, it gets the form using var formToSend = document.getElementById('myForm');
And change IFRAME to NATIVE.
It's a doc issue needs to fix with Google when using SandBoxMode=IFRAME currently. See here. I've tested it works by swapping IFRAME to NATIVE. You can simply do it:
function doGet() {
return HtmlService.createHtmlOutputFromFile('main');
}
Google has turned of NATIVE for SandBoxMode. It is now set to IFRAME only.
See here

how to add input value from session using ajaxlink in yii

I am kind of lost on how to do this in my view.
I want to add the quantity. Right now, it adds but how do I get the input value into my ajaxlink?
My controller is using session to add.
<input id="quantity" type="text" value="1" class="span1">
<div id="cart-text">
<?php echo CHtml::ajaxLink(' Add ',
Yii::app()->createUrl('controller/basketAjax',array('id'=>$_GET['id'])),
array('success'=>'function(data){...
controller:
$session=new CHttpSession;
$session->open();
if (!isset(Yii::app()->session['cart']))
{
$quantity = 1;
$session->add('cart',array(
'product_id.'.$id=>array("id"=>$id,
'quantity'=>$quantity)
));
}
else
{
$cart = Yii::app()->session['cart'];
$array = $cart['product_id.'.$id];
if (isset($array)){
$array['quantity']=$array['quantity']+1;
} else {
$t = array('product_id.'.$id=>array("id"=>$id,'quantity'=>$quantity));
array_push($cart,$t);
}
$session->add('cart', $products);
}
you can do this by using keyup function of jquery. First set the id of link as "mylink". Compute the url by using createUrl.
<script type="text/javascript" >
$("#quantity").keyup(function(){
var url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>;
$("#mylink").attr("href","");
$("mylink").attr("href",url +$(this).val());
});
</script>
Now i explain whats happening above. first i catch the event keyup on input you are using. It will be called each time you press a key on input. Now url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>; this code is returning you the base url to your action without any parameters being passed.this line $("#mylink").attr("href",""); will set the href of your link to " "( means nothing). Now this line $("mylink").attr("href",url +$(this).val()); is appending the value of the input you are getting from the input.
Remember you have to put the separator in between like
$("mylink").attr("href",url+"/" +"id"+"/"+$(this).val());
Above i have assumed that the href in your case looks like "projectname/index.php/controller/action/id/something". Thats y i have used separators in between but you can customize it according to your needs.
ajaxLink will not work (from what I know), just build it the old fashion way with jquery. Your best solution is actually to put it in a form and submit the form. Something like
<?php
Yii::app()->clientScript->registerCoreScript('yiiactiveform');
$form=$this->beginWidget('CActiveForm', array('action'=>Yii::app()->createUrl('cart/addProduct')));?>
<input type="text" name="quantity" value="1">
<input type="hidden" name="Product_id" value="<?php echo $model->id;?>">
<?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('cart/addProduct','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data) {
if(data.status=="success"){
$("#formResult").html("form submitted successfully.");
}
else{
$.each(data, function(key, val) {
$("#user-form #"+key+"_em_").text(val);
$("#user-form #"+key+"_em_").show();
});
}
}',
)); ?>
<?php $this->endWidget(); ?><!-- .contact -->
Sorry for the indenting, hard to indent properly here.