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

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!

Related

laravel Shopping Cart Call to a member function has() on null (0)

this problem occurs when I used Darryldecode\Cart
the edit() method
public function edit($id)
{
$product=Product::find($id);
// Cart::add(455, 'Sample Item', 100, 2, array());
Cart::add($id,$product->name,1,$product->price,['type'=>'PDF']);
return back();
and the cart/index.blade.php
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>qty</th>
<th>Type</th>
</tr>
</thead>
<tbody>
#foreach($cartItems as $cartItem)
<tr>
<td>{{$cartItem->name}}</td>
<td>{{$cartItem->price}}</td>
<td>{{$cartItem->qty}}</td>
<td>{{$cartItem->options->has('type')?$cartItem->options->type:''}}</td>
</tr>
#endforeach
</tbody>
</table>
so I have this problem and want to solve it
ErrorException (E_ERROR) Call to a member function has() on null
and it wants validation required
what should I do?
You can use optional helper:
optional($cartItem->options)->has('type') ? $cartItem->options->type : ''

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

Redirect to the same page with retrieved values from database in laravel?

View Code:
<li class="active"><a href="#tab_1" data-toggle="tab" id="versiontab"
onclick="window.location='{{ url("versiondb") }}'" >Version
Database</a></li>
This is my view code.This is also a tab .Onclicking the tab the url redirection works.
Route Code:
Route::get('versiondb','versiondbController#select');
Controllercode:
public function select()
{
$users=debModel::all();
return redirect()->back()->with($users);
}
Here it should fetches the values from db and redirecting back to the same page and display the retreived values in table .But i am getting an error here as $users undefined
Inside the Same view (specified above):
<table class="table" id="table">
#foreach($users as $users)
<thead>
<tr class="header">
<th valign="middle" width="3%">Versions</th>
<th valign="middle" width="2%">Supported versions</th>
<th valign="middle" width="10%">Release Date</th>
<th valign="middle" width="3%">Type</th>
<th valign="middle" width="10%">Description</th>
<th valign="middle" width="3%">Actions</th>
<th valign="middle" width="3%">Downloader</th>
<!--<th valign="middle" width="3%">Beta code</th>-->
</tr>
</thead>
<tbody>
<tr>
<td>{{$users->versions}}</td>
<td></td>
</tr>
#endforeach
</tbody>
</table>
I think this is the place where error occurs.
When you use "->with" in Laravel, Laravel go back to that page with a session message named with.
Usiing with in your scenario would be
public function select()
{
$users=debModel::all();
return redirect()->back()->with("message","I generated all users");
}
which would be accessed in your blade as
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
The solution to your question is write it this way
public function select()
{
$data['users']=debModel::all();
return view("viewdeb",$data);
}
then your blade you will need a foreach loop
#foreach ($users as $user)
{{$user->firstname}}
#endforeach
mind you, more data can be sent back to the view by just including them in the data array
public function select()
{
$data['users']=debModel::all();
$data['business']=businessModel::all();
return view("viewdeb",$data);
}
The issue is here:
#foreach($users as $users)
here your array variable name and the variable in which you are holding the data on each iteration is the same, so change it to:
#foreach($users as $user)
<td>{{$user->versions}}</td>
Use ($users as $user) instead ($users as $users) here
#foreach($users as $user)
And in your template
<td>{{$user->versions}}</td>
Also change in your controller
return view('haghwayUpdate')->with('users',$users);

Trying to get property of non-object Laravel 5.3, profile.blade.php.

I am trying to show the users profile information who are logged in,I have two tables one is Message and other one is User, when user login it see his\her profile, but there is an error.
profile.blade:
<table class="table table-bordered">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
</tr>
</thead>
<tbody>
#foreach($users as $usr)
<tr>
<td>
{{$usr->name}}
</td>
<td>
{{$usr->email}}
</td>
</tr>
#endforeach
</tbody>
Controller:
public function GetUserInfo($id){
$user = DB::table('users')
->join('messages', 'messages.user_id', '=', 'users.id')
->where('users.id', '=', $id)
->get();
if($user->count()){
$user = $user->first();
return view('profile')->with('users',$user);
}
}
Model:
class Messages extends Model
{
protected $table = 'messages';
protected $fillable = [
'message','user_id'
];
public function user()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
}
When passing data to views this way, users should be an array.
Change your controller like this:
public function GetUserInfo($id){
$user = DB::table('users')
->join('messages', 'messages.user_id', '=', 'users.id')
->where('users.id', '=', $id)
->get()->toArray();
if($user->count()){
$user = $user->first();
return view('profile')->with('users',$user);
}
}
And your profile blade view:
<table class="table table-bordered">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
</tr>
</thead>
<tbody>
#foreach($users as $usr)
<tr>
<td>
{{$usr['name']}}
</td>
<td>
{{$usr['email']}}
</td>
</tr>
#endforeach
</tbody>
In
if($user->count()){
$user = $user->first();
return view('profile')->with('users',$user);
}
first() is called on queries, but $user already exists as a Laravel collection by the time it enters the if statement. It should exist as an object or an array, and you can access it as such. Something like
if($user->count()){
$user = $user[0]; /*OR*/
$user = $user->0;
return view('profile')->with('users',$user);
}
I don't recall which is the right way off the top of my head. print_r($user) before to see what it looks like.

Laravel 5.2 Getting Exception Undefined Variable

I'm working on Laravel and got stuck in this mess of undefined variable $category, I don't know why and where is the exact problem.
I have done this much.
AdminAjaxController
public function category()
{
$category=DB::select('select category_name,category_id from categories');
return view('admin.category_table',compact('category'));
}
category_table View
<table id="category" class="table">
<thead>
<tr>
<th>ID</th>
<th>Category Name</th>
<th>Delete</th>
<th>Update</th>
</tr>
</thead>
<tbody>
#foreach($category as $value)
<tr>
<td>{{ $value->category_id}}</td>
<td>{{ $value->category_name}}</td>
<th>Delete</td>
<td>Update</td>
</tr>
#endforeach
</tbody>
</table>
You should try this solution:
public function category()
{
$category=DB::select('select category_name,category_id from categories');
return view('admin.category_table')->with(['category' => $category]);
}
In addition to this you are getting this error, because you do not send your category variable.
Try this :
public function category()
{
$category=DB::table('categories')->select('category_name', 'category_id')->get();
return view('admin.category_table',compact('category'));
}
Try this, it will work.
public function category()
{
$category=
DB::select('select category_name,category_id from categories');
return view('admin.category_table',[ 'category'=>$category ]);
}

Categories