Laravel Showing my Data Arrays to index.blade.php - Undefined Offset 1 - php

Im trying to show all my data in database to my index.blade.php, however, there's an error that is showing Undefined Offset 1. I used arrays. There's no data that is showing in my table form. here is my code. Please guide me im new to Laravel thanks.
My VIEW
Here is where the problem occurs
<div class="form-group">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<th>Image Name</th>
<th>Action</th>
</tr>
#foreach($promotions->$promotion)
<tr>
<th>{{ $promotion->promotion_image }}</th>
<th>Update</th>
<th>Remove</th>
</tr>
#endforeach
</table>
</div>
</div>
My CONTROLLER INDEX FUNCTION
public function index()
{
$promotions = Promotion::all();
return view('admin.airlineplus.promotions.index')->with('promotions', $promotions);
}
My CONTROLLER STORE FUNCTION
Here is where I store my data using arrays
public function store(Request $request)
{
$this->validate($request, [
'promotion_image' => 'required'
]);
if ($request->has('promotion_image'))
{
//Handle File Upload
$promotion = [];
foreach ($request->file('promotion_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/promotion_images',$fileNameToStore);
array_push($promotion, $fileNameToStore);
}
$fileNameToStore = serialize($promotion);
}
else
{
$fileNameToStore='noimage.jpg';
}
foreach ($promotion as $key => $value) {
$promotionImage = new Promotion;
$promotionImage->promotion_image = $value;
$promotionImage->save();
}
return redirect('/admin/airlineplus/promotions/create')->with('success', 'Image Inserted');
}
Guide me through all of this please thanks

You have made a slight error with your foreach type changing it to this
<div class="form-group">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<th>Image Name</th>
<th>Action</th>
</tr>
#foreach($promotions as $promotion)
<tr>
<th>{{ $promotion->promotion_image }}</th>
<th>Update</th>
<th>Remove</th>
</tr>
#endforeach
</table>
</div>
foreach has the first option is the variable to loop through and the second is the variable for each of the loops
http://php.net/manual/en/control-structures.foreach.php

Related

Undifined Variable : employess

i want to show image gallery at user page
this my code from ImageUserController
function list(){
$employees['img_user'] = ImageUser::where('user_id', Auth::user()->id)->get();
return view('ukm.index', $employees);
}
and this my code from UserContoller
public function list()
{
$employees = ImageUser::all();
$ukm = Ukm::all();
return view('/ukm/index', compact( 'employees', 'ukm'));
}
this my route
Route::get('/ukm/index', 'ImageUserController#list');
and this my code at ukm/index.blade.php
table class="table table-stripped table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Image</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
#foreach ($employees as $employee)
<tr>
<th scope="row">{{ $employee->id }}</th>
<td>{{ $employee->name }}</td>
<td>{{ $employee->description }}</td>
<td><img src="{{ asset('uploads/employee/' . $employee->image) }}" width="150px;" height="100px;" alt="Image"></td>
<td>Edit</td>
<td>Delete</td>
</tr>
#endforeach
</tbody>
</table>
i'm getting this error : Undefined variable: employees
You aren't passing the array to the view actually
return view('ukm.index')->with('employees', $employees);
In your ImageUserController list() method,
function list(){
$employees = ImageUser::where('user_id', Auth::user()->id)->get();
return view('ukm.index', compact('employees'));
}

Laravel 5 Search Multi attribute Error

I`m new to the Laravel 5.4 .i needed to develop multiple attributed Search function.Here is the error um getting .
Here is my registered.index.blade.php View
<div class="panel panel-default">
<div class="panel-body">
<table class="table table-striped">
<thead>
<th>Full Name</th>
<th>Name with initials</th>
<th>National ID Number</th>
<th>Date Of Birth</th>
</thead>
<tbody>
#foreach($items as $item)
<tr>
<td>{{ $item->full_name }}</td>
<td>{{ $item->name_with_initials }}</td>
<td>{{ $item->nic_no }}</td>
<td>{{ $item->date_of_birth }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
Here is the my Controller .
public function search_code(Request $request){
$query = $request->search;
$queryType = $request->institute; // 'id' or 'name'
$items = DB::table('registerdetails');
if($queryType == 'id'){
$items = $items->where('id', 'LIKE',"%$query%");
}
if($queryType == 'name'){
$items = $items->where('name', 'LIKE',"%$query%");
}
$items->get();
return view('registeredusers.index')->with('items',$items);
}
Can Anyone suggest me the solution for this error?
Change this:
$items->get();
To this:
$items = $items->get();
When you're doing just $items->get();, the $items variable has an instance of Query Builder and not query result.

Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag (download report using dompdf )

I'm trying to download my html report into pdf file using dompdf, but I'm getting this error:
Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag::$first_name
here's my controller code so far:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
$view = view('reporting.monthly_payroll.detail')->with(compact('monthlypayrolls'));
return parent::render($view, null, null);
}
public function downloadMonthlyPayrollReport(Request $request)
{
$monthlypayrolls = $this->monthlypayrollProcess($request);
$isPdf = true;
$view = 'reporting.monthly_payroll.pdfview';
$pdf = PDF::loadView($view, compact('monthlypayrolls', 'isPdf'))->setPaper('A4', 'landscape');
return $pdf->stream();
}
and here's my pdf view blade code so far :
<div class="table-responsive text-center">
<table class="table table-hover" id="datatable" style="width: 100%">
<thead>
<tr>
<th class="text-center">Driver Name</th>
<th class="text-center">NRIC</th>
<th class="text-center">Employment Type</th>
<th class="text-center">Hired Date</th>
<th class="text-center">Basic Salary</th>
<th class="text-center">Uniform</th>
<th class="text-center">Total Working Hours</th>
<th class="text-center">Take Home Pay</th>
</tr>
</thead>
<tbody>
#foreach($monthlypayrolls as $monthlypayroll )
<tr>
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
<td>{{$monthlypayroll->nric}}</td>
<td>{!! DriverEmployedType::getString($monthlypayroll->employed_type) !!}</td>
<td>{{$monthlypayroll->join_date}}</td>
<td>{{$monthlypayroll->basic_salary }}</td>
<td>{{$monthlypayroll->uniform_size}} {{$monthlypayroll->uniform_quantity}}</td>
<td>{{$monthlypayroll->total_working_hours}}</td>
<td>{{$monthlypayroll->take_home_pay}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
any idea ?
The monthlypayrollProcess() method returns a Response view, then you are assigning it to another variable
$monthlypayrolls = $this->monthlypayrollProcess($request);
Finally in your view
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
Something wrong with your logic, the method probably should return an object not a view. The method should be something like that:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
return $monthlypayrolls;
}
I had this very issue when trying to retrieve data as a response->json($myObject) and then iterating through it with a foreach with blade in the view.
I solved it by just returning the object "as is" (i.e. return $myObject instead of return response->json($myObject) ).
Hope this helps!

Download file helper

I wanna download my file that i have upload to my web
Im just saving the name of the file to database
My controller
public function index()
{
$data['file'] = $this->home_model->file();
$this->load->view('file', $data);
}
public function download()
{
$id_file = $this->uri->segment(3);
$data = file_get_contents("/upload/kepsek/".$id_file);
force_download('$id_file', $data);
}
My model
function file()
{
$query=$this->db->query("SELECT * FROM silabus");
return $query->result();
}
My view
<table class="table table-bordered text-center" id="table-user">
<thead>
<tr class="info">
<th>Download</th>
<th>Pelajaran</th>
</tr>
</thead>
<tbody>
<?php
foreach($file as $data){
?>
<tr>
<td width="50%">Download</td>
<td width="50%"><?php echo $data->pelajaran;?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
When i click download nothing happend
Its there any way to make it work?
try this
$data = file_get_contents("upload/kepsek/".$id_file);
and also check the "upload/kepsek/".$id_file file exist or not
<table class="table table-bordered text-center" id="table-user">
<thead>
<tr class="info">
<th>Download</th>
<th>Pelajaran</th>
</tr>
</thead>
<tbody>
<?php
foreach($file as $data){
?>
<tr>
<td width="50%">Download</td>
<td width="50%"><?php echo $data->pelajaran;?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
Then in Download Controller
public function index()
{
$data['file'] = $this->home_model->get_file();
$this->load->view('file', $data);
}
public function download($id)
{
$this->load->helper('file');
$this->load->helper('download');
$data = file_get_contents("./upload/kepsek/".$id); //check file exist
$name = my_file.php//your file type
force_download($data, $name);
}
In model
function get_file()
{
$query= $this->db->query("SELECT * FROM silabus");
$result = $query->result_array();
return $result;
}

laravel-excel not working to export xls file using blade with image

Excuse me,
I want to use laravel-excel to export my report.
This is the controller:
public function getExcelfile(){
$users = UserService::findAllPublic();
$total = UserService::count();
$total_with_photo = UserService::countWithPhoto();
Excel::create('excelfile', function($excel) use ($users, $total, $total_with_photo) {
$excel->sheet('Excel', function($sheet) use ($users, $total, $total_with_photo) {
$sheet->loadView('report.excel')->with("users", $users)->with("total", $total)->with("total_with_photo", $total_with_photo);
});
})->export('xls');
}
This is the excel.blade.php located at report folder:
<html> <body> Total Registered User : {{$total}} Total Registered User With Photo : {{$total_with_photo}} <h1>Data User</h1> #if(!empty($users)) <table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Photo</th>
<th>Nama</th>
<th>Struck</th>
<th>Email</th>
<th>Phone</th>
<th>FB ID</th>
<th>Timestamp</th>
<th>Publish</th>
</tr>
</thead>
#foreach ($users as $row)
<tr>
<td>{{$row->id}}</td>
<td><img src="{{URL::asset('assets/images/upload/' . $row->photo)}}" width="200px"/></td>
<td>{{$row->nama}}</td>
<td>{{$row->struck}}</td>
<td>{{$row->email}}</td>
<td>{{$row->phone}}</td>
<td>{{$row->fbid}}</td>
<td>{{$row->timestamp}}</td>
<td>{{$row->publish}}</td>
</tr>
#endforeach </table> #endif </body> </html>
Then this error appears:
PHPExcel_Exception
File http://myserver.com/projectname/public/assets/images/upload/DSCN1164.jpg not found!
That file is exist when I try to access it at browser.
Instead of:
<img src="{{URL::asset('assets/images/upload/' . $row->photo)}}"
you should use:
<img src="assets/images/upload/{{{ $row->photo }}}"
Of course assuming it's correct path

Categories