Two functions not running on same view laravel - php

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.

Related

My table headers are being displayed but no data is being fetched from the database

This here is my join controller. There is no error but the problem is that the data is not being shown in the table. The headers from blade.php file are being shown but the data is not being fetched or shown in the table.
My Join Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class JoinController extends Controller
{
//
public function report()
{
$data = DB::table('pmedicalreadings')
->join('medrecords','medrecords.id','=','pmedicalreadings.id')
->join('payments','payments.id','=','pmedicalreadings.id')
->select('pmedicalreadings.bp','pmedicalreadings.temp','pmedicalreadings.ecg','medrecords.prerecord','payments.ptype','payments.tpayment','payments.rpayment')
->get();
return view('report',compact('data'));
}
}
My Routes from web.php:
Route::post('/report','JoinController#report');
Route::get('/report', function () {
return view('/report');
});
My report.blade.php file:
<center><h1>Patient Report</h1></center>
<form >
<table class="table table-bordered table-striped">
<tr class="bg-danger text-light">
<th>Patient Blood Pressure Reading</th>
<th>Patient Temperature Reading</th>
<th>Patient ECG Reading</th>
<th>Patient Medical Record</th>
<th>Payment Type</th>
<th>Total Payment</th>
<th>Remaining Payment</th>
</tr>
#if(isset($data))
#foreach($data as $row)
<tr class="bg-secondary text-light">
<td>{{$row->bp}}</td>
<td>{{$row->temp}}</td>
<td>{{$row->ecg}}</td>
<td>{{$row->prerecord}}</td>
<td>{{$row->ptype}}</td>
<td>{{$row->tpayment}}</td>
<td>{{$row->rpayment}}</td>
</tr>
#endforeach
#endif
</table>
</form>
Here is the output I'm getting,
The problem is in your web routes. Your get request doesnt return any data. It just returns the blade. You return the data in the post request. When you access the url your are making a get request

How do I show specific projects of each user for the admin.?

The admin of the application has access to list each projects of a user. Write now I am able to list the projects but they all are listed together no matter the unique id of the URL.
// Admin form case controller
public function adminforms (Project $project){
$users = User::get();
return view('smiledesign.adminforms', compact('users', 'project'));
}
///Records controller
public function records(user $users, project $project){
$project = project::get();
$users = user::get();
return view ('/smiledesign/records' , compact( 'users','project'));
}
<table class="table">
<thead>
<tr>
<th>Dr Name</th>
<th>User Id</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{$user->name}}</td>
<td> {{$user->id}}</td>
<td> {{$user->email}}</td>
</tr>
</tbody>
#endforeach
</table>
// Records view
<table class="table table-striped table-bordered" id="table_id">
<thead>
<tr>
<th>Case Number</th>
<th>Case Form</th>
<th>Patient Name</th>
<th>Date Created</th>
<th>Status</th>
</tr>
</thead>
#foreach ($project as $project)
<tbody>
<tr>
<td> {{$project->case_number}}</td>
#if ($project->services0)
<td> {{$project->services0}}</td>
#elseif ($project->services1)
<td> {{$project->services1}}</td>
#elseif ($project->services2)
{{-- <td> {{$project->services2 . ' ' . $project->mockup0}}</td> --}}
<td> {{$project->services2 . ' ' . $project->mockup0 . ' ' . $project->mockup1}}</td>
#endif
<td> {{$project->first_name . ' ' . $project->last_name}}</td>
<td> {{$project->created_at}}</td>
<td> {{$project->concerns}}</td>
</tr>
</tbody>
#endforeach
</table>
What I wanna see is each project of a specific user when I click The link from the adminforms user. Instead, every link I go I see all projects of all users
The relationship between the projects and users should be defined in the database and in the models themselves.
Database:
In my interpretation, a user can work on multiple projects and a project can have multiple users, this would be a many to many relationship. With this conclusion, I would go for an abstracted table setup like so:
table users has an id field.
table projects has an id field.
table project_users has an id field(always required for laravel), a user_id field(foreign key to users.id) and a project_id field(foreign key to projects.id). Do not forget to add a unique key which combines user_id and project id, there is no need for duplicate definitions.
Model relationships:
Project
function users()
{
$this->belongsToMany(User::class, 'project_users', 'project_id', 'user_id');
}
User
function projects()
{
$this->belongsToMany(Project::class, 'project_users', 'user_id', 'project_id');
}
If you read this piece of documentation, it will all make sense.
Query
$users = User::with('projects')->get();
or
$projects = Project:with('users')->get();
Now every user instance has a projects collection. If they do not, then you need to add a record to the database coupling a project to that user.
Showing results
foreach($users as $user)
{
foreach($user->projects as $project)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
or
foreach($projects as $project)
{
foreach($project->users as $user)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
Sidenote
My instructions are optimized for performance. You could also just call ->projects on the $user without using the with('projects') in the query, but that will mean that you will trigger a query on the spot. As this kind of code is prone to being used in foreach statements, you could be doing queries in foreach statements which is referred to a N+1 query problem.

Can't get the right view in laravel 5.6

"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.

Laravel: How to display the value of other table in a loop?

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

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