Can't get the right view in laravel 5.6 - php

"Undefined variable: Pro (View: E:\xampp\htdocs\mypro\resources\views\folder\product.blade.php)" I am using laravel 5.6
trying to get data from existing database.
Product Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table='products';
protected $primaryKey = 'p_id';
protected $fillable = ['p_title', 'p_element', 'p_description', 'p_duration', 'p_amount', 'p_startDate', 'p_endDate', 'p_old_price', 'p_new_price', 'p_keyWords', 'p_category', 'p_status', 'prefertime'];
}
Product Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$pro=Product::all();
return view('Folder.product'), compact('pro'));
}
}
Product.blade.php
#extends('layouts.app')
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
#foreach($Pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
</tr>
</table>
#endsection

As identified pro is not capitalised so you should correct as follows. You should also check that the array is not empty before you execute the page otherwise you can find yourself with more errors;
Also note that I've moved your 's inside the foreach as this is needed to make the table display properly.
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
#if(! empty($pro))
#foreach($pro as $row)
<tr>
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="2">No records</td>
</tr>
#endif
</table>
#endsection
Also, in your controller the view should read
return view('Folder.product', compact('pro'));
not
return view('Folder.product'), compact('pro'));

The capital, you need to use $pro not $Pro.
#foreach($pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
I hope it works.

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>

How to display all the records with their properties in another db?

I want to display all the phones and their users's info's in a table but I keep getting this error:
Undefined property: stdClass::$user
When I check it on tinker it retrives the user without problem but I can't perform the same thing in blade file.
I have table of
users
id | name | surname | email | password | department_id
and ip_phones
user_id | ip_adress | mac_adress | phone_number
This is my UserController
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable, HasRoles;
public function phone()
{
return $this->hasOne(IpPhone::class);
}
public function department()
{
return $this->belongsTo(Department::class);
}
This is my IpPhonesController:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class IpPhone extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
This is HomeController
public function list()
{
$phones = DB::table('ip_phones')->get();
return view('list', compact('phones'));
}
And this is list.blade.php file
<table id="table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>IP</th>
<th>MAC</th>
<th>UserName</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($phones as $phone)
<tr>
<td>{{$phone->ip_adress}}</td>
<td>{{$phone->mac_adress}}</td>
<td>{{$phone->user->name}}</td>
<td>Edit</td>
</tr>
#endforeach
</tbody>
</table>
I suggest you to use eloquent.
Change this
DB::table('ip_phones')->get();
to;
IpPhone::with('user')->get();
then it should work.
try this:
public function list()
{
$data['phone'] = DB::table('users')
->leftJoin('ip_phone','users.id', '=', 'ip_phone.user_id ')
->get();
return view('list', $data);
}
//blade
<table id="table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>IP</th>
<th>MAC</th>
<th>UserName</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($phones as $phone)
<tr>
<td>{{$phone->ip_adress}}</td>
<td>{{$phone->mac_adress}}</td>
<td>{{$phone->name}}</td>
<td>Edit</td>
</tr>
#endforeach
</tbody>
</table>

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

Two functions not running on same view laravel

I am trying to get the data from two different tables and display it on one page, i means at one view. But only one function working on same time while the other not and vice versa, Both the functions work seperately but not combinely. Please help. my code is give.
PdfContoller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use PDF;
use Auth;
class PdfController extends Controller
{
public function personalpdf()
{
if(\Auth::check()){
$user = \Auth::user();
return view('pdf/personalpdf',compact('user'));
}
else
{
return view('pdf/personalpdf');
}
}
public function personalphd()
{
$user_id = Auth::user()->id;
$result['result'] = DB::table('education')->where('user_id' ,'=', $user_id)->get();
if(count ($result)>0){
return view('pdf/personalpdf',$result);
}
else
{
return view('pdf/personalpdf');
}
}
}
My controller contains the above two functions for fetching data from two different tables.
My view file is personalpdf.blade.php is given here.
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#if(isset($user))
<tr>
<th>Name</th>
<td>{{ $user ->tname}}</td>
</tr>
<tr>
<th>Father Name</th>
<td>{{ $user ->fname}}</td>
</tr>
<tr>
<th>Contact Number</th>
<td>{{ $user ->phone}}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ $user ->email}}</td>
</tr>
<tr>
<th>Official Email</th>
<td>{{ $user ->off_email}}</td>
</tr>
#endif
</table>
<div class="text text-success text-center">
PHD Research
</div>
<table class="table">
<tr>
<th>
PHD Research Area
</th>
<th>University</th>
<th>Country</th>
</tr>
#foreach($result as $value)
<tr>
<td>{{$value->research_area}}</td>
<td>{{$value->univ}}</td>
<td>{{$value->country}}</td>
</tr>
#endforeach
</table>
#endsection
My routes is given here.
Route::get('pdf/personalpdf','PdfController#personalpdf');
Route::get('pdf/personalpdf','PdfController#personalphd');
Route::get('/personal','PdfController#personal');
Actually I want to make a view file where I can see all the personal details of current login user. And below it I want to display his PHD research details getting from education table. But I am getting only one result on a same time. I means if I got personal detail of login user then phd detail not showing, if I get phd details it gives me all the phd details, not of current login user.
Please help.
class PdfController extends Controller
{
public function personalpdf()
{
if(\Auth::check()){
$user = \Auth::user();
$user_id = $user->id;
$result = DB::table('education')->where('user_id' ,'=', $user_id)->get();
return view('pdf/personalpdf',compact('user', 'result'));
}else {
return view('pdf/personalpdf');
}
}
}
Routes file
Route::get('pdf/personalpdf','PdfController#personalpdf');
Route::get('/personal','PdfController#personal');
Try this. You have to pass both the user and results from the same controller action to the view. I think you lack some basics of laravel. Please go through the docs before you proceed.

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>

Categories