Displaying additional attribute in table - Laravel - php

My pivot table admin_user with additional attribute item_no. I am finding it hard to retrieve the additional attribute into my html table.
PS: i tried $admin->pivot->item_no in my table but i get an error trying to get a non-object
Admin
public function users()
{
return $this->belongsToMany('App\User')->withPivot('item_no')
->withTimestamps();
}
admin_food
admin_id
user_id
item_no
Controller
public function index()
{
$admins = Admin::where('id',1)->get();
return view('index',compact('admins'));
}
HTML
<table class="table" id="table">
<thead>
<tr>
<th>Order No#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach($admins as $admin)
<tr >
<td>{{$admin->name}}</td>
</tr>
#endforeach
</tbody>
</table>

You access the item_id by access the pivot property of the user/related model.
Your controller is returning a collection that has only one admin.
$admins = Admin::where('id',1)->get();
admins will be a collection of 1 admin that has id = 1.
if this was on purpose, then you can access the item_id for every user related to this admin by this code:
#foreach($admins as $admin)
#foreach($admin->users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->pivot->item_id}}</td>
</tr>
#endforeach
#endforeach
To simplify:
$admin = Amdin::first();
$item_id = $admin->users()->first()->pivot->item_id

Related

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.

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.

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

Laravel - Get last 5 registered users attributes

I'm trying to make a dashboard and I want to display the last 5 users that are registered on the site, So the most recent 5 registered people. How can I accomplish this? I need to have access to all their attributes, for instance, Name, Lastname etc.
This is my route: Route::get('/adminpanel', 'AdminController#index')->name('adminpanel');
And this is the controller:
public function index()
{
Carbon::setLocale('nl');
$tijd = Carbon::today();
$posts = Post::count();
$users = User::count();
$replies = Reply::count();
return view('admin.dashboard', compact('posts', 'users', 'replies', 'tijd'));
}
They need to be displayed in a table like this:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Rainier</td>
<td>Laan</td>
<td>rainier.laan#home.nl</td>
</tr>
</tbody>
</table>
Thanks in advance
You can use take() to get specific number of records.
$users = User::orderBy('id', 'desc')->take(5)->get();
And on your view-
#foreach($users as $user)
{{$user->name}}
#endforeach
Hope it helps :)
This one will take 5 latest users.
$users = User::latest()->take(5)->get();

laravel 5.1 - retrieving data from 2 table and sending it to index page using foreach

I am retrieving data from two different tables - users and phones - and sending them to a view page. My code is pulling all the columns from the phone table:
Controller.php:
public function index()
{
$alluser=User::with('phone')->get();
return view('index',compact('alluser'));
}
view.blade.php:
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Mobile</th>
</tr>
</thead>
<tbody>
#foreach($alluser as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->phone}}</td>
</tr>
#endforeach
</tbody>
</table>
result:
Using ->with("phone") will return an object attached to your user accessed via ->phone, which is why your mobile column has a json object with all the phone columns in it.
If you just want to show each user's name and their phone number, change
<td>{{$user->phone}}</td>
to
<td>{{$user->phone->phone}}</td>
You could specify an additional query to select the desired columns,
User::with(['phone' => function ($query) {
$query->select(['id', 'phone']);
}])->get();
And then,
$user->phone->phone //not $user->phone

Categories