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 ]);
}
Related
Im using Laravel 8 with PHP 8 as well as barryvdh/laravel-domPDF. I have a crud that displays employee information and you can print out their payslips. When i dont pass through the $id it works correctly and prints all the employees information however when i do it returns the error:
Attempt to read property "name" on bool
This is my route:
Route::get('/print/{id}', ['App\Http\Livewire\EmployeesTable', 'print'])->name('livewire.employees-table.print');
This is my model function
public function print($id){
$employees = Employees::find($id);
$teams= Team::all();
$pdf = PDF::loadView('employees.payslip', compact('teams', 'employees'));
return $pdf->download('invoice.pdf');
}
This is my user relationship:
public function employees(){
return $this->hasMany(Employees::class);
}
This is my employees relationship:
public function user(){
return $this->belongsTo(User::class);
}
.
<thead>
#foreach ($employees as $employee)
<tr>
<td>
{{$employee->name}} {{$employee->surname}}
{{$employee->phone}}
{{$employee->role}}
</td>
</tr>
#endforeach
</thead>
<thead>
#foreach ($teams as $team)
<tr>
<td>
{{$team->companyName}}
{{$team->street}}
{{$team->compound}}
{$team->suburb}}
{$team->phone}}
</td>
</tr>
#endforeach
</thead>
</table>
I think $employees return empty result so better check before looping
#if(isset($employees)&&count((array)$employees))
#foreach ($employees as $employee)
<tr>
<td>
{{$employee->name}} {{$employee->surname}}
{{$employee->phone}}
{{$employee->role}}
</td>
</tr>
#endforeach
#endif
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 : ''
So, I have this code which is printing the various topics/threads started by the user.
It is looping over the Posts table that contains data related to posts.
In User column, it will display user_id. But I want to access User table and display the user_name column matching that user_id. How to do this?
<table border="2">
<tr>
<th>Topic</th>
<th>User</th>
<th>Replies</th>
<th>Views</th>
<th>Last Update</th>
</tr>
#foreach($topics as $topic)
<tr>
<td>{{$topic->topic_title}}</td>
<td>{{$topic->id}}</td>
<td>0</td>
<td>0</td>
<td>{{$topic->updated_at}}</td>
</tr>
#endforeach
</table>
Controller's Code:
public function show($id)
{
$topics = Topic::where('board_id', $id)->get();
return view('boards.show')->with('topics', $topics);
}
In controller eager load user model:
$topics = Topic::where('board_id', $id)->with('user')->get();
In view:
<td>{{ $topic->user->user_name }}</td>
I assume you already have the user relationship defined in Topic model:
public function user()
{
return $this->belongsTo(User::class);
}
you can try something like this:
#foreach($topics as $topic)
#php $user = DB::table('User')->where('id', $topic->id)->first(); #endphp;
<tr>
<td>{{$topic->topic_title}}</td>
<td>{{ $user }}</td>
<td>0</td>
<td>0</td>
<td>{{$topic->updated_at}}</td>
</tr>
#endforeach
In your Post model
public function user(){
$this->belongsTo(User::class);
}
In your blade {{$topic->user->name}}
<table border="2">
<tr>
<th>Topic</th>
<th>User</th>
<th>Replies</th>
<th>Views</th>
<th>Last Update</th>
</tr>
#foreach($topics as $topic)
<tr>
<td>{{$topic->topic_title}}</td>
<td>{{$topic->user->name}}</td>
<td>0</td>
<td>0</td>
<td>{{$topic->updated_at}}</td>
</tr>
#endforeach
You need to define relation in models first like this:
class User extends Model
{
public function post()
{
return $this->hasMany("App\Post");
}
}
class Post extends Model
{
public function user()
{
return $this->belongsTo("App\User");
}
}
then in the loop on the view side you can access user's data like this:
#foreach($topics as $topic)
<tr>
<td>{{$topic->user->name}}</td>
</tr>
#endforeach
You do not need to use "with" function in this case. you only need to fetch topics, pass them on to view, iterate through and you get the user's data
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!
I am trying display all product name,
I am getting the error :
ErrorException in 4938c589ea3db6bab0c4c788473314a4 line 46:
Undefined variable: allproduct (View: C:\Server\WebDocs\CRUD\resources\views\product.blade.php)
I had try return View::make('product',$allproduct); and
return View::make('product')->with('product',allproduct); also cannot, i don't know why, this is my first Laravel program.
in my ProductController
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product',$allproduct);
}
in my product.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>No</td>
<td>Product</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
#foreach($allproduct as $key => $value)
<tr>
<td>$key</td>
<td>{{ $value->product }}</td>
<!-- we will also add show, edit, and delete buttons -->
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
You could try this,
return View::make('product', compact('allproduct'));
Trying using this code in Controller:
return View::make('product')->with(['allproduct' => $allproduct]);
try passing variable like
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product')->with('allproduct', $allproduct);
}
and in view
what about just doing #foreach($allproduct as $product) and then in <td>, <td>{{$product->product}}</td>. and make sure your have product column in db
controller:
return View::make('product')->with('allproduct',$allproduct);
blade:
#foreach($allproduct as $key => $value)
<div>
{{ $key . ' => ' . $value }}
</div>
#endforeach