undefined variable inside view laravel - php

so i'm trying to fetch data from my database into a datatable i got from getbootstrap
but i get the error:
Undefined variable: issue (View: C:\Users...\resources\views\dashboard.blade.php)
below ive listed the code where i use the variable: issue
dashboard.blade.php
<table id="datatable" class="table table-bordered table-striped table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Issue</th>
<th scope="col">begrootte tijd</th>
<th scope="col">beschrijving</th>
<th scope="col">Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">#</th>
<th scope="col">Issue</th>
<th scope="col">begrootte tijd</th>
<th scope="col">beschrijving</th>
<th scope="col">Action</th>
</tr>
</tfoot>
<tbody>
#foreach($issue as $issues)
<tr>
<th> {{ $issues->id }} </th>
<th> {{ $issues->iname }} </th>
<th> {{ $issues->begroting }} </th>
<th> {{ $issues->description }} </th>
<th>
START
STOP
</th>
</tr>
#endforeach
</tbody>
DashboardController.php
<?php
namespace App\Http\Controllers;
use App\Dashboard;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function store(Request $request)
{
$this->validate($request,[
'iname' => 'required',
'begroting' => 'required',
'description' => 'required',
]);
$issue = new Issue;
$issue->iname = $request->input('iname');
$issue->begroting = $request->input('begroting');
$issue->description = $request->input('description');
$issue->save();
return redirect('/issue')->with('success', 'Issue opgeslagen');
}
}
the model
dashboard.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model
{
protected $table = 'issue';
}

You should pass the variable to the view.
Lets say you have a HomeController, and want to pass some variable to a view from the controller.
HomeController.php
public function show()
{
$someVariable = "An Awesome Example";
return view('example', [
'someVariable' => $someVariable,
]);
}
example.blade.php
<b>{{ $someVariable }}</b>
You have to pass data to a view. Then inside your view you can show that data to the user. In my example I've created a array with the key someVariable and passed $someVariable to the value of that key.
Inside my view I can then use the key to show the value.

You didn't send $issues to blade page, change your DashboardController like this:
<?php
namespace App\Http\Controllers;
use App\Dashboard;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function store(Request $request)
{
$this->validate($request,[
'iname' => 'required',
'begroting' => 'required',
'description' => 'required',
]);
$issue = new Issue;
$issue->iname = $request->input('iname');
$issue->begroting = $request->input('begroting');
$issue->description = $request->input('description');
$issue->save();
$issues=Dashboard::all();
return redirect('/issue')->with('success', 'Issue opgeslagen')
->with('issues',$issues);
}
}

Related

Grab the right value when 2 tabel values are the same using Laravel

Can someone help me with this problem?
I have 2 tables in phpMyAdmin
the first one where the with the department_id of the person
The second one where you can see the department_id and then the department_name
I have a table with this code:
<div class="container">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Department Name</th>
</tr>
</thead>
<tbody>
#foreach ($employee as $name)
<tr>
<td>{{ $name->first_name }}</td>
<td>{{ $name->last_name }}</td>
<td>{{ $name->!!!IDK YET!!! }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Basically, for each employee I want to grab the department_id and check what the matching department_name
Extra code that makes the table on my page work:
EmployeeController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employee;
use Illuminate\Http\Request;
class EmployeeController extends Controller
{
public function index()
{
$employee = Employee::all();
return view('employee.index', compact('employee'));
}
}
Employee.php (Model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $employees = 'employees';
protected $EmployeeValues = ['first_name','last_name'];
protected $departments = 'departments';
protected $DepartmentValues = ['department_id','department_name'];
}
Okey so I found the answer after trying a lot of things.
My new code is:
EmployeeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class EmployeeController extends Controller
{
public function index()
{
$employee = DB::table('employees')
->leftJoin('departments','employees.department_id',"=",'departments.department_id')
->get();
return view('employee.index', compact('employee'));
}
}
Employee (Model)
Wasn't necessary anymore
index.blade.php
<div class="container">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Department Name</th>
</tr>
</thead>
<tbody>
#foreach ($employee as $name)
<tr>
<td>{{ $name->first_name }}</td>
<td>{{ $name->last_name }}</td>
<td>{{ $name->department_name }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>

Search results shows everything in database and show image in results

I have a search function in my laravel project, when I navigate to the search page it shows all the entries in the database. I would like it to show nothing until I put something in the search field. Furthermore, when the results are displayed I would like an image to be dispalyed.
Here are the relavent files:
public function search(Request $request){
$query = $request->input('query');
$result = Photo::search($query)->get();
return view('search.results')->with('result', $result);
}
Here is the model
class Photo extends Model
{
use SearchableTrait;
protected $searchable = [
'columns' => [
'photos.title' => 10,
'photos.description' => 10,
'photos.photo' => 10,
],
];
protected $fillable = array('photo','title','description','album_id');
public function album(){
return $this->belongsTo('App\Album');
}
}
The display.blade.php
<div class="row">
<div class="col-8">
<table class="table table-bordered table-stripped">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($result as $res)
<tr>
<th><img src="/storage/photos/."{{$album_id}}/{{$res->photo}}</a></th>
<th>{{$res->title}}</th>
<th>{{$res->description}}</th>
</tr>
#endforeach
</tbody>
</table>
In the above code I get an error message about not being able to find $album_id.
As always thank you for your help

laravel 5.6 datatable pagination not working

I'm using laravel 5.6 and try to add pagination to bootstrap datatable. But it's not working.I got error.Please help me to find mistake I did.
Error
Method Illuminate\Database\Eloquent\Collection::links does not exist.
designation.blade.php(View)
#if(count($designations)>=1)
<table class="table table-dark" id="designationTable">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Designation Type</th>
<th scope="col">Status</th>
<th scope="col">Create At</th>
<th scope="col">Action</th>
</tr>
</thead>
#foreach ($designations as $designation)
<tbody>
<tr>
<th scope="row">{{$designation->id}}</th>
<td>{{$designation->designation_type}}</td>
<td>{{$designation->status}}</td>
<td>{{$designation->created_at}}</td>
<td>
<button type="" class="btn btn-secondary" id="btn_update_designation">Update</button>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $designations->links() }}
#endif
DesignationController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use App\Designation;
class DesignationController extends Controller
{
public function index()
{
$designations = Designation::all();
return view('pages.designation')->with('designations',$designations);
}
}
You should use the paginate() method in your controller file.
public function index()
{
$designations = Designation::paginate(10);
return view('pages.designation')->with('designations',$designations);
}
Paginating Eloquent Results
You may also paginate Eloquent queries. In this example, we will
paginate the User model with 15 items per page. As you can see, the
syntax is nearly identical to paginating query builder results:
$designations = Designation::paginate(15);
You may call paginate after setting other constraints on the query,
such as where clauses:
$designations = Designation::where('column', 'value')->paginate(15);
You should use the paginate() method in your controller file.
public function index()
{
$designations = Designation::paginate(15);
return view('pages.designation')->compact('designations',$designations);
}

Undefined variable: users (View: C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)

SOLVED
i am confusing that i think i have already make a true code with laravel. i have seen on many references for me to know how to read database. yes i'm newbie and still learning. well here is my code
from model. admin.php is the name of folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
protected $table = "admin";
}
here is my controller. my controller name is UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', ['admin' => $users]);
}
}
and here is my view, it is located in view/admin/ named user.blade.php:
#extends('admin.header')
#section('content')
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title">Striped Table</h4>
<p class="category">Here is a subtitle for this table</p>
</div>
<div class="content table-responsive table-full-width">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Name</th>
<th>Salary</th>
<th>Country</th>
<th>City</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
#endsection
and here is my error
i call from my browser with this http://localhost:8000/users
sorry for bad English :(
#foreach($admin as $users)
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
#endforeach
if u not need for each
<tbody>
<tr>
<td>1</td>
<td>{{ $admin[0]->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
First, the variable will be called admin in the view, because that's how you passed it when you wrote 'admin' => $users.
Second, it's going to be a collection of users, so $users->email isn't going to work. You need to #foreach through each user.
Please update your view/admin/user.blade.php and UserController like:
UserController:
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', compact('users'));
}
}
view/admin/user.blade.php:
<tbody>
#foreach($users as $user)
<tr>
<td>1</td>
<td>{{ $user->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>

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!

Categories