Laravel cicle foreach of a litteral object - laravel

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

Related

Foreign key is now null and when mapping through the data it say "Attempt to read property "$category_name" on null"

I deleted a category which is an f-key of the items table. Now the view has an error saying 'Attempt to read property "category_name" on null'. How do I render a null foreign key?
I tried if-else:
#if({{$item->category->category_name}} === 'null')
<h2>no category</h2>
#else
{{ $item->category->category_name}}
#endif
Try this:
#if ($item->category)
{{ $item->category->category_name }}
#else
<h2>no category</h2>
#endif
Or simply
<h2>{{ $item->category?->category_name ?? 'no category' }}</h2>
In PHP8 or Larvel 8
simple use this one
<h2>{{ $item->category->category_name ? 'no category' }}</h2>

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">

laravel 7 route/url param in Blade

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

Laravel Blade - check if data array has specific key

I need to check if the data array has a specific key, I tried it like this:
#if ( ! empty($data['currentOffset']) )
<p>Current Offset: {{ $currentOffset }} </p>
#else
<p>The key `currentOffset` is not in the data array</p>
#endif
But I always get <p>The keycurrentOffsetis not in the data array</p>.
You can use #isset:
#isset($data['currentOffset'])
{{-- currentOffset exists --}}
#endisset
I think you need something like this:
#if ( isset($data[$currentOffset]) )
...
Use following:
#if (array_key_exists('currentOffset', $data))
<p>Current Offset: {{ $data['currentOffset'] }} </p>
#else
<p>The key `currentOffset` is not in the data array</p>
#endif
ternary
#php ($currentOffset = isset($data['currentOffset']) ? $data['currentOffset'] : '')

Ternary in Laravel Blade to apply row class names

I have this report that Im trying to do, but I want to make the rows alternate colors. this is what I tried, but it does not work. What is the correct way to achieve this?
<div class="row">
{{$rowOrder = "even"}}
#foreach($data as $row)
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->adm_referraldate}}</span>
<span>{{$row->adm_number}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
#endforeach
</div>
Replace
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
with
<?php $rowOrder = ($rowOrder == "odd") ? 'even' : 'odd'; ?>
or if you are using a Laravel 5.2 or up
#php($rowOrder = ($rowOrder == "odd") ? 'even' : 'odd')
Do the same for the line {{$rowOrder = "even"}}
If you used the {{$rowOrder = "even"}} it will echo out the result.
You can use modulo arithmetic to decide whether and index is odd or even:
$isEven = index % 2
If you combine this with a PHP ternary operator then you'd get this
{{ $loop->index % 2 ? 'odd': 'even' }}
see
https://davidwalsh.name/php-shorthand-if-else-ternary-operators
and
https://en.wikipedia.org/wiki/Modular_arithmetic
Here's a very easy solution:
#php $count = 0; #endphp
#foreach($data as $row)
<div class="{{ ++$count % 2 ? 'odd': 'even' }}">
{{ $row->name }}
</div>
#endforeach
Use variable $loop documentation ( $loop->even laravel 5.8, or ($loop->iteration % 2)laravel< 5.8 )
#foreach ($users as $user)
#if ($loop->even)
This is even.
#else
#endif
#endforeach
or
#foreach ($listObject as $Object)
<tr class="{{ ($loop->iteration % 2) ? 'odd' : 'even' }}">
#endforeach
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
possibly should be
{{ $rowLine = ($rowOrder == "odd" ? 'even' : 'odd') }}
Here's a working example for me: I left the dump output in it so you can see the actual number counting up. Hope it helps anyone who comes across this problem :). EDIT: Don't forget to add colors in your css file for .odd and .even!
#if(!empty($names))
{{-- SET VARIABLE + HIDE IT --}}
<div class="hide">{!! $number = 0 !!}</div>
#foreach($names as $n)
{{ dump($number) }}
<div class="{!! $number % 2 == 0 ? 'odd' : 'even' !!}">
{{-- UP VARIABLE + HIDE IT --}}
<div class="hide">{!! $number++ !!}}</div>
{{-- DISPLAY CONTENT —}}
{{ $n }}
</div>
#endforeach
#endif
Try getting the key from the foreach loop and run ($key % 2)
Basically Odd Number mod 2 always have a remainder
#foreach ($rows as $key => $row)
<div class="#if ($key > 0 && $key % 2) odd #else even #endif">
</div>
#endforeach

Resources