how to insert new variable into laraadmin view - laravel

i have for each loop in my view i want to check if the target is equal to some value and insert another value i`
#foreach ($modules as $module)
#if ($module->name === "h_car_positions") <?php $module->name = "history_car_positions" ?>
#else <?php $module_name = $module->name; ?>
#endif
<tr>
<td>{{ $module->id }}</td>
<td>{{ $module->name }}</td>
<td>{{ $module->name_db }}</td>
<td>{{ Module::itemCount( $module_name ) }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-key"></i>
<i class="fa fa-sort"></i>
<a module_name="{{ $module->name }}" module_id="{{ $module->id }}" class="btn btn-danger btn-xs delete_module" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
i get undefined variable for that variable :$module_name

You are mixing blade syntax with php, so you should do it this way:
#php
if($module->name == "h_car_positions"){
$module_name = "history_car_positions";
} else {
$module_name = $module->name;
}
#endphp

#foreach ($modules as $module)
#php
$module_name = $module->name;
switch ($module_name) {
case "H_car_positions":
$module_name = "history_car_positions";
break;
default:
$module_name = $module->name ;
$module_count =Module::itemCount( $module_name );
}
#endphp
<tr>
<td>{{ $module->id }}</td>
<td>{{ $module->name }}</td>
<td>{{ $module->name_db }}</td>
<td>{{ $module_count }}</td>

Related

Laravel how to display day and month only without year from date type?

I would wan to display month and data for payment_date only in a foreach loop in view.blade.php.
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ $data->payment_date }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach
Hereby I include the datatype:
Short way (not recommended)
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ \Carbon\Carbon::createFromFormat('Y-m-d', $data->payment_date)->format('m-d') }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach
Laravel way (Recommended)
Add cast to the eloquent model class.
protected $casts = [
'payment_date' => 'date',
];
after that you can do this
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ $data->payment_date->format('m-d') }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach

how to display nested data in table laravel

How can I get the result as drawn on the right
in Controller
$programs = Program::with(['activities', 'activites.subactivities'])->get();
in Blade View
#foreach ($programs as $programKey => $program)
#foreach ($program->activities as $activity)
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>{{ $programKey + 1 }}</td>
<td>{{ strtoupper($program->name) }}</td>
<td>{{ $activity->name }}</td>
<td>{{ $subactivity['name'] }}</td>
</tr>
#endforeach
#endforeach
#endforeach
I would say to save in a variable and if different the show
Like this:
#foreach ($programs as $programKey => $program)
#php
$newProgram = true;
#endphp
#foreach ($program->activities as $activityKey => $activity)
#php
$newActivity = true;
#endphp
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>
#if($newProgram == true)
{{ $programKey + 1 }}
#endif
</td>
<td>
#if($newProgram == true)
{{ strtoupper($program->name) }}
#php
$newProgram = false;
#endphp
#endif
</td>
<td>
#if($newActivity == true)
{{ $activity->name }}
#php
$newActivity = false;
#endphp
#endif
</td>
<td>
{{ $subactivity['name'] }}
</td>
</tr>
#endforeach
#endforeach
#endforeach

ASK I want to use 2 FOREACH in 1 table body VIEW laravel

I want to use 2 FOREACH in 1 table body VIEW laravel
like this for the EXCEL.VIEW.BLADE code
<table>
<thead>
<tr>
<th>Booking Status</th>
<th>CreatedAT</th>
<th>New row</th>
</tr>
</thead>
<tbody>
#foreach($datas as $hasil)
foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
and for the code in the controller like this
$data = TR_BookingHeader::where('LogisticId', $this->qty)->get();
$pp = TR_BookingHeader::all();
return view('Excel', [
'datas' => $data, 'pp' => $pp
]);
Try this one
<tbody>
#foreach($datas as $hasil)
#foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
Another example
[
state
college
college
state
college
college
college
...
]
#foreach ($states as $state => $colleges)
<h1 style="center">{{ $state }}</h1>
<ul class="nospace clear">
#foreach ($colleges as $college)
<li class="one_quarter first">
<h3 class="center">{{ $college->college }}</h3>
</li>
#endforeach
</ul>
#endforeach

Laravel - syntax error, unexpected ')', expecting '['

I am using Laravel 5.8 to create an App. When I wanted to display the view, it generated an error as shown below:
syntax error, unexpected ')', expecting '['
Controller
public function serviceOverview(Request $request)
{
$data['title'] = 'Subscription Overview';
$serviceoverviews = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
$render=[];
if(isset($request->package_name))
{
$serviceoverviews=$serviceoverviews->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
$render['package_name']=$request->package_name;
}
if(isset($request->service_id))
{
$serviceoverviews=$serviceoverviews->where('service_id',$request->service_id);
$render['service_id']=$request->service_id;
}
$serviceoverviews= $serviceoverviews->orderBy('created_at','DESC');
$serviceoverviews= $serviceoverviews->paginate(15);
$serviceoverviews= $serviceoverviews->appends($render);
$data['serviceoverviews'] = $serviceoverviews;
return view('report.serviceOverview',$data);
}
In the Controller, I tried to do some filtering. Also, I did raw query.
View
<div class="box box-primary">
<div class="box-header with-border">
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
#if(count($detailsubscriptions))
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->links() }}
</td>
</tr>
</tbody>
</table>
#else
<div class="row text-center">
<h2>No Service Overview to show</h2>
</div>
#endif
</div>
</div>
I tried to check the code, but found nothing. How do I resolve this issue
You have missed a $ from the $serviceoverview variable in your blade #foreach. Change:
#foreach($serviceoverviews as $key => serviceoverview)
To
#foreach($serviceoverviews as $key => $serviceoverview)
Here is the full code updated with changes. Please refer to this
Controller.php
public function serviceOverview(Request $request)
{
$builder = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
if($request->filled('package_name'))) {
$builder = $builder->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
}
if(isset($request->service_id))
{
$builder=$builder->where('service_id',$request->service_id);
}
$serviceoverviews = $builder->orderBy('created_at','DESC')->paginate(15);
$data = [
'title' => 'Subscription Overview',
'serviceoverviews' => $serviceoverviews
];
return view('report.serviceOverview', $data);
}
Now change your view code like this.
view.blade.php
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => $serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->appends(\Request::all())->links() }}
</td>
</tr>
</tbody>
</table>
You are using serviceoverview in place of $serviceoverview in view file. That would be an issue.
You have made some typo and one logical error.
Change
#foreach($serviceoverviews as $key => serviceoverview) to #foreach($serviceoverviews as $key => $serviceoverview)
<td>{{ serviceoverview->service_name }}</td> to <td>{{ $serviceoverview->service_name }}</td>
You have put {{ $serviceoverview->links() }} out side of the foreach loop

How to hide variable in laravel

I have a code like below
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach
and the result is like this
enter image description here
How to hide that "nama" variable
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])->nama
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach

Resources