Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP) laravel 6 - 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

Related

Prestashop set a smarty variable on a hook

I would like to assign a smarty variable on the hook actionAuthentication which get triggered when an user successfully connects and unset it whenever the user disconnects.
so I can manage my content depending on the connexion with a condition like the following :
<ul id="menu">
<li>
<p>Name :</p>
{if $my_variable}
<span class="employee-name">{$my_variable}</span>
{else}
<span class="employee-name">Firstname Lastname</span>
{/if}
</li>
</ul>
How could I assign a global smarty variable from a hook ?
Here is what I have tried but it doesn't work:
public function hookActionAuthentication()
{
$result = 'Myself';
$this->context->smarty->assign([
'my_variable' => $result
]);
}
You don't need to use hookActionAuthentication($params), you should assign $this->context->customer->fistname.' '.$this->context->customer->lastname.
you can use function isLogged to check if customer is loggued or not
$this->context->customer->isLogged()

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 !

Load partial view through controller on Search button click

I am working on an ASP.NET Core 2.1 MVC app using razor. I have searchQuery.cshtml and a (individually working perfectly) viewQuery.cshtml pages. In my searchQuery page, I let user enter queryId and on clicking "Search" button I want to run the action of ViewQuery that displays the results in viewQuery.cshtml and show the viewQuery below the search button area.
I am not good working with Ajax or so. On Search btn click, I call the viewQuery Get action thru ajax. In the button click, I pass the entered queryId of type int. But, when I load searchQuery page, it throws null exception for passing the queryId. I searched few hous, but didn't get any solution.
searchQuery.cshtml UPDATED
<div>
<div class="col-md-6">
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.QueryId)
</dt>
<dd>
<input asp-for="QueryId" class="form-control" />
</dd>
</dl>
</div>
<input type="submit" value="Show" />
<!-- CHANGE IN CALL -->
Search
</div>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
×
<h3 class="modal-title">Query Answer</h3>
</div>
<div class="modal-body" id="myModalBodyDiv">
</div>
<div class="modal-footer">
Ok
</div>
</div>
</div>
</div>
<script>
function ShowResult() {
// Retrieve queryId
var queryId = $("#QueryId").val();
// DisplayS PROPERLY
alert("Entered ID " + queryId);
// TRIED '/query/viewQuery' ALSO
$.ajax({
type: 'GET',
url: '../query/viewQuery',
data: { queryId: queryId },
success: function (response) {
alert(response); // **DISPLAYS [Object: object]**
$("#myModalBodyDiv").html(response);
$('#myModal').modal("show");
}, error: function (response) {
alert("Error: " + response);
}
});
}
</script>
My ViewQuery action in controller UPDATED
[Route("[controller]/viewQuery/{queryId:int}")]
public async Task<IActionResult> ViewQuery(int queryId)
{
// Retrieve Data from api using HttpClient
....
return PartialView("ViewQuery", qVM); // PartialView(qVM); //View(qVM);
}
Search Query Action UPDATED
[Route("searchQuery")] // customer/searchQuery
public IActionResult SearchQuery()
{
return View();
}
Can anyone please help me how do I achieve my goal. Simple - a text box were user enters queryId. A button on click, want to pass the entered queryId, call a GET action on controller and get the response. Finally show the response below the search button. I was just trying with the above modal dialog, I prefer text and not dialog.
Try & isolate the issue.
Instead of using model.QueryId in the searchQuery.cshtml, simply hardcode any reference to "modelid" - that way at least you are eliminating the possibility that Model is null on that page. Then instead of onclick="ShowResult(#Model.QueryId)"> , hard code some known id instead of #Model.QueryId. Then debug to see if your ViewQuery action method id hit. If the method is hit, then you can take it from there.
Also, I noticed that your jquery calls may need to be modified:
Instead of: $('myModalBodyDiv').html(response); it should probably be $('#myModalBodyDiv').html(response); (the "#" is missing ..) - same for $('myModal').
You can use Partial Pages(ViewQuery page) , in your searchQuery page , you could use Ajax to call server side action with parameter ID . On server side , you can query the database with ID and return PartialView with models :
[HttpPost]
public IActionResult Students (StudentFilter filters)
{
List students = Student.GetStudents(filters);
return PartialView("_Students", students);
}
Then in success callback function of Ajax , you can load the html of partial view to related area in page using jQuery :
success: function (result) {
$("#searchResultsGrid").html(result);
}
You can click here and here for code sample if using MVC template . And here is code sample if using Razor Pages .

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

passing data via redirect laravel 5

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.