laravel 7 route/url param in Blade - laravel

I tried to pass parameter type=Business via GET request to
RegisterForm.
In Welcome.blade
I have two links to RegisterForm.
#if (Route::has('register'))
Register Business
Register Applicant
#endif
In RegisterForm, I have hidden field like this:
#if (isset($type))
<input id="userType" type="hidden" class="form-control" name="userType" value="{{ $type }}">
#endif
Even Tried this way:
#if (isset($type == 'Business'))
<input id="userType" type="hidden" class="form-control" name="userType" value="{{ $type }}">
#endif
In Laravel side: Main page gets userTypes via:
public function index()
{
$userTypes = array(
'Applicant',
'Business'
);
return view('website::welcome', compact('userTypes'));
}
return view('website::welcome'
means I have own package called "website".
Q) What do I missing, what is wrong my code ?
I am getting error from registerForm:
ParseError
syntax error, unexpected '$type' (T_VARIABLE), expecting ',' or ')' (View: register.blade.php)

The error is comming from this below line.
#if (isset($type == 'Business'))
You need to call two conditions as below. for isset and comparison
#if (isset($type) && $type== 'Business')

Related

Undefined variable $attendance_status using radio button laravel

i am using laravel query builder but the problem is i am using radio buttons but i want to update the attendance at the radio button but it returns an error Undefined variable $attendance_status
i don't know why please help and how can i pass the $attendance_status variable
here is my code
my form
<form action="{{route('Attendances.update',$student->id)}}" method="post">
#csrf
#method('PUT')
<input type="hidden" name="id" value="{{$student->id}}">
<label class="block text-gray-500 font-semibold sm:border-r sm:pr-4">
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 1 ? 'checked' : '' }}
class="leading-tight" type="radio" value="presence">
<span class="text-success">حضور</span>
</label>
<label class="ml-4 block text-gray-500 font-semibold">
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }}
class="leading-tight" type="radio" value="absent">
<span class="text-danger">غياب</span>
</label>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">{{trans('Students_trans.Close')}}</button>
<button class="btn btn-danger">{{trans('Students_trans.submit')}}</button>
</div>
</form>
here is my controller
update
public function update(Request $request, $id)
{
// return $request;
if($request->attendances == 'absent'){
$attendance_status = 0;
}
else if($request->attendances == 'presence'){
$attendance_status = 1;
}
Attendance::find($id)->update([
'student_id'=> $id,
'grade_id'=> $request->grade_id,
'class_id'=> $request->classroom_id,
'section_id'=> $request->section_id,
'attendance_date'=> date('Y-m-d'),
'status' => $attendance_status,
]);
return back();
You have an if and an elseif in your Controller, so only 2 conditions would create a variable named $attendance_status. You probably want to add a default branch, else basically, to make sure that the variable gets created with some default value before you try to use it in your update call.
Not sure which one of those 2 options you want to be the default but this would simplify things:
$attendance_status = $request->attendences == 'presence';
https://laravel.com/docs/9.x/redirects#redirecting-with-flashed-session-data
You may use the withInput method provided by the RedirectResponse instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily retrieve it during the next request:
return back()->withInput();
Surely {{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }} logic does not work in this way.
try this instead
<input name="attendences" checked="{{ $student->attendances()->first()->attendence_status == 0 ? true : false}}" class="leading-tight" type="radio" value="absent">

[Contact form]How to convert object to string in Laravel?

I'm studying contact form script. It works fine but after I add this lines
I'm having this error.
htmlspecialchars() expects parameter 1 to be string, object given (View: /home/------/resources/views/mail.blade.php)
Here is my contact.blade.php
#php
$week = array( "日", "月", "火", "水", "木", "金", "土" );
$s_date = date("Y年m月d日 H時i分");
$dayofweek = "(".$week[date("w")].")";
$s2_date = $s_date . $dayofweek;
$k_no_raw = strval("km_".date("Y_md_His_A") ."_". date('w'));
#endphp
<br>
<input name="sdate" type="hidden" value="{{ $s2_date }}">
<input name="k_no" type="hidden" value="{{ $k_no_raw }}">
I tried to change object to string.
but I still got same error.
Controller part of this section
\Mail::send('mail', array(
'sdate' => $request->get('sdate'),
'k_no' => $request->get('k_no'),
),
Here is mail.blade.php
{{ $k_no }}
Could someone teach me right code please?
UPDATE

Error retrieving a checked Checkbox in Laravel as a boolean

I'm a bit new to laravel and I'm working on a laravel application which has a checkbox field which should have a boolean value of 1 when the user checks the checkbox, and 0 when the checkbox is unchecked.
I want to retrieve the boolean value as either 1 or 0 and save in a db.
Please assist?
View
<form method="POST" action="{{ route('b2c.getplans') }}" id="travel_form" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="check-now {{ $errors->has('spouse') ? ' has-error' : '' }}">
<h1 class="cover-travel">I am travelling with</h1>
<label class="spouse-me">
<h1 class="pumba">Spouse</h1>
<input id="spouse" type="checkbox" name="spouse">
<span class="checkmark" name="spouse"></span>
</label>
#if ($errors->has('spouse'))
<span class="help-block">
<strong>{{ $errors->first('spouse') }}</strong>
</span>
#endif
</div>
<button type="submit" class="form-b3c"> Get Plans</button>
</form>
Controller
public
function validatePlanEntries(Request $request)
{
$validation = $this->validate($request, [
'WithSpouse' => (\Input::has('spouse')) ? 1 : 0;
]
}
1st way is send correct value from frontend side
you can add jquery or javascript at frontend side on change event of checkbox :
<input type="checkbox" name="checkbox" id="myCheckbox" />
<script>
$(document).on('change','#myCheckbox',function(){
if($(this).is(':checked')){
$('#myCheckbox').val(1);
}else{
$('#myCheckbox').val(0);
}
});
</script>
at your backend side , now you can check :
$yourVariable=$request->input('checkbox');
2nd way is only check at your backend
you will get your checkbox value=on if it checked
if($request->input('checkbox')=='on'){
$yourVariable=1;
}else{
$yourVariable=0;
}
you can use ternary condition as well , like :
$yourVariable = $request->input('checkbox')=='on' ? 1:0;
You don't have to validate a checkbox value. because if its checked it sends the value of on, and if it's not checked it doesn't send anything.
So, all you need is that get whether the check box is checked or not, you can do as follow.
to retrieve the boolean value of the checkbox,
Controller
// since you haven't provide any codes in the controller what
// are you gonna do with this value,
// I will juts catch it to a variable.
$isChecked = $request->spouse == 'on'
To retrieve checkbox value from request as boolean:
$yourModel->with_spouse = (bool) $request->spouse;
If checkbox is checked then it's value (on by default) will be passed to request and casting non-empty string to boolean will give you true. If checkbox is not checked then spouse key won't be passed to request at all, so $request->spouse will return null. Casting null to boolean will result in false (in PHP true is int 1 and false is int 0, which is exactly what you want).

Laravel cicle foreach of a litteral object

I have this problem in my project in laravel.
I have a table Optional where $optional->column_name with the name of column of $bookingOptionals.
I have this code:
#foreach($optionals as $optional)
#if( $optional->column_name == 'coffee_break' ||
$optional->column_name == 'permanent_coffee' ||
$optional->column_name == 'permanent_coffeeplus'||
$optional->column_name == 'integrazione_permanentcoffee' ||
$optional->column_name == 'quick_lunch')
<div class="col-lg-3">
<fieldset>{{ $optional->nome }}</fieldset>
<input type="text" name="{{ $optional->column_name }}" value="{{ $bookingOptionals['0']->$optional->column_name }}">
</div>
#else
#endif
#endforeach
I have this error:
Trying to get property 'column_name' of non-object (View: /home/vagrant/code/prenotazioni/resources/views/dashboard/optional_booking_edit.blade.php)
use
#php
print_r($optional)
#endphp
inside your loop to see what fields it has and make sure column_name is exists,
and is $optional an object? or is it array? then you should remove raw php and probably use
$optional['column_name']
instead

How do you correctly post data to the same view in Laravel 5.2

I am having trouble getting the if statement in this Laravel view to work.
<h1>Login View</h1>
<form action="{{ route('type_of_user') }}" method="post">
<select name="type_of_user">
<option value="Volunteer">Volunteer</option>
<option value="Organization" selected>Organization</option>
<br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit">
</select>
</form>
{{ $value }}
<?php
if ($value = "Volunteer") {
echo "I am a Volunteer";
}
else {
echo "I am a Organization";
}
The "{{ $value }}" is outputting the correct type of user when It is changed and submitted from the form. but the output of the if statement is not working at all correctly. When Volunteer is selected the output is:
Volunteer I am a Volunteer
but when I select organization the output is:
Organization I am a Volunteer.
I first tired using the #if command in blade and got the same error. I switched to PHP to see if the problem could have lied within my blade command, but got the same problem I am receiving with php. The only thing I can think of is that maybe I am causing an error by submitting to the same page.
My controller for this code is:
class Usercontroller extends Controller
{
public function Type_of_user(Request $request)
{
$value = $request['type_of_user'];
return view('/login')->with('value', $value);
}
}
Any advice as to how to get the "if" statement to work correctly to what is selected is appreciated.
You are not making a comparision you are making an assignment. Use double = sign.
($value == "Volunteer")

Resources