hellow i want to show 10 persons who has deposited amount in last24 hours . i am able to show there user_id and amount but i want users name instead of user_id.
i have no name colum in $funds it is in users
i have this code:
<div class="col-md-6">
<div class="text-center"><h4 class="text-success">Last 10 Investors</h4></div>
<table class="table text-white" >
<tbody>
#foreach( \App\Fund::where('created_at', '>', \Carbon\Carbon::now()->subHours(24))->orderBy('id', 'DESC')->take(10)->get() as $fund)
<tr>
<th scope="row"></th>
<td>{{ $fund->user_id }}</td>
<td class="text-center text-warning">{{ $fund->total }}</td>
<td class="text-right"><img src="https://adsok.com/img/btc.png" alt="Bitcoin"> <i><b>bitcoin</b></i><br></td><br>
</tr>#endforeach
</tbody>
</table>
</div>
Set up a relation from your Fund Model to the User Model
In your Fund model,
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
and you could access the User that belongs to the fund as
$fund->user->{your-username-field}
OR if you don't want to set up a relationship and fetch the username, you could do so by
$user = \App\User::find($fund->user_id);
if (! empty($user)) {
$user_name = $user->user_name; // or whatever the username field is
}
Try like this :
#foreach( \App\Fund::select("*")->with('users')->where('created_at', '>', \Carbon\Carbon::now()->subHours(24))->orderBy('id', 'DESC')->take(10)->get()->toArray() as $fund)
<tr>
<th scope="row"></th>
<td>{{ $fund['user']['user_name'] }}</td>
<td class="text-center text-warning">{{ $fund->total }}</td>
<td class="text-right"><img src="https://adsok.com/img/btc.png" alt="Bitcoin"> <i><b>bitcoin</b></i><br></td><br>
</tr>#endforeach
so you have to models: "Fund" and "User".
you should connect them with Laravel's Relationships:
add this method to "User" model:
public function funds()
{
return $this->hasMany('App\Fund', 'user_id');
}
then in "Fund" model:
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
now you can access user's name from funds:
i suggest you to retrive your datas in controller, not views:
public function index()
{
$funds = Fund::with('user')->
where('created_at', '>', \Carbon\Carbon::now()->
subHours(24))->orderBy('id', 'DESC')->take(10)->get();
return view('viewName', ['funds' => $funds]);
}
then in view:
#foreach ($funds as $fund)
{{ $fund->user->name }}
#endforeach
Related
I have a table I need to complete, for now, it has 3 columns DATE, SHIFT and STATION.
I only want the data that is attached to the logged-in user.
I have a pivot table where I can call the data from for DATE and SHIFT, but Stations is a separate table,
My pivot table is to display my many to many relationships between USER and SHIFTS called shift_user. this works,
the STATIONS table relationship is a one to many relationship with USER
as a User will belong to one station, but a station will have multiple users
Stations model
public function user()
{
return $this->hasMany('App\User');
}
}
User Model
public function station()
{
return $this->belongsTo('App\Station');
}
Controller for Project
$user = Auth::user();
return view('layouts/timesheet/index', ['user' => $user]);
}
Blade view to get info into tables
<table class="table table-sm table-success">
<tr>
<th scope="col">DATE/DAY</th>
<th scope="col">SHIFT</th>
<th scope="col">STATION</th>
</tr>
<tbody class="table-light">
#foreach ($user->Station as $station)
#endforeach
#foreach ($user->shifts as $shift)
<tr>
<td>{{ $shift->created_at }}</td>
<td>{{ $shift->Name }}</td>
<td>{{ $station->Name }}</td>
</tr>
#endforeach
Here is a picture of the table I want to fill in. I want the Station name to display, but I just get this ID which I think is the user I.D
Please can you let me know what I can do to call the data from another table via the relationship?
Thank you for any help
Make sure you create a function that's same as when you call it on Blade View.
Station.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function stations()
{
return $this->hasMany('App\Station');
}
Then on blade view, try this:
#foreach($user as $u)
<tr>
<td>{{ $u->shift->created_at }}</td>
<td>{{ $u->shift->Name }}</td>
<td>{{ $u->stations->name }}</td> // I assume the column in stations table is "name"
</tr>
#endforeach
I want to implement simple teaming option to my app. I have theese tables:
users/teams /team_members/
-----/---------/------------/
id /id /id /
name /user_id /user_id /
... /... /team_id /
I have 3 models where I have defined relations:
User model:
public function teams() {
return $this->hasOne(Team::class);
}
public function teamMembers() {
return $this->hasManyThrough(Team::class, TeamMembers::class);
}
Team model:
public function user() {
return $this->belongsTo(User::class);
}
public function members() {
return $this->hasMany(TeamMember::class);
}
TeamMember model:
public function user() {
return $this->belongsTo(User::class);
}
User can only create one team, but join many teams. The problem I have is with displaying data in the frontend.
This is the query I make in the controller:
public function index(User $user) {
$teams = Team::where('user_id', auth()->user()->id )->with('members')->get();
return view('teams.index')->with('teams', $teams);
}
I have a table with the members of the current autheticated user team. I want to display the names if each member but the only thing i can do is $team->member->id and I get the ids of the users but I want to get their names too. I can do it with simple query but I dont want to make new query for every row.
This is the blade file:
#if ( $teams->count() > 0 )
#foreach ($teams as $team)
<table class="table table-striped table-sm table-responsive-lg">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Members</th>
<th scope="col">Created</th>
<th scope="col" colspan="2"></th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$team->name}}</td>
<td>{{ $team->members->count('members')}} {{ Str::plural('member', $team->id) }}</td>
<td>{{ $team->created_at->diffForHumans() }}</td>
<td>
Edit
</td>
<td>
<form action="" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-sm btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
<br>
<h5>Team members:</h5>
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Added</th>
</tr>
</thead>
<tbody>
#foreach ($team->members as $member)
<tr>
<td>{{ $member->user->name }}</td>
<td>{{ $member->created_at->diffForHumans() }}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
#else
<div class="container">
<h2>You do not own a team</h2>
<p class="lead text-muted">You can create your own team or join the team of other user.</p>
<p>
Create team
Join team
</p>
</div>
#endif
When I changed $team->member->id with $member->user->name it worked. It shows the names of the users but I get N+1 queries alert and I don't know how to fix it (i tried different queries and relations but it didnt work):
You are accessing the user relationship from each member. So lets eager load that relationship as well.
public function index(User $user)
{
$teams = Team::where('user_id', auth()->user()->id )->with('members.user')->get();
return view('teams.index')->with('teams', $teams);
}
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 am retrieving all Tickets assigned to a user. A single ticket has a foreign key to users table. These are the code below:
App\Models\User.php
protected $appends = ['full_name', 'avatar'];
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
App\Http\Controllers\TicketsController.php
/**
* Display a listing of the resource.
*/
public function index()
{
$tickets = Ticket::paginate(25);
$users = User::all();
//return view('user.tickets.index', compact('tickets'));
return View::make('user.tickets.index', compact('tickets','users'));
}
Resources/Views/User/Tickets/index.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Status</th>
<th>Assigned To</th>
</tr>
</thead>
<tbody>
#foreach($tickets as $item)
<tr>
<td>{{ $item->status }}</td>
<td>{{ $item->user_id }}</td>
</tr>
#endforeach
</tbody>
</table>
I want to convert the field {{ $item->user_id }} into {{ $item->first_name where id = $item->user_id }} The field should be converted into the First Name according to the $item->user_id
user_id is a foreign key to tickets and the equivalent of it is the is of Users table.
Please help.
Firstly it's not good practice to write db query in views. You can accomplish that by defining a relation in your Ticket model class.
public function user()
{
return $this->belongsTo(User::class);
}
Now you can access user name by $item->user->first_name.
Try this:
#foreach (DB::table('table_name')->where('id', '=', $tickets->id)->get() as $id)
{{ $id->id }}
{{ $item->column_val1}}
{{ $item->column_val2}}
{{ $item->column_val3}}
#endforeach
I have projects which hasMany users, and users belongsTo a project.
I want to count the total amount of users a project has so I need to link them.
This way I'm getting a Call to undefined method Illuminate\Database\Query\Builder::user() error.
What am I doing wrong?
Controller:
class ProjectController extends Controller
{
private $project;
public function __construct(Project $project){
$this->project = $project;
// $this->project = $project
// ->with('user');
}
public function index(Project $project)
{
$projects = $project->with('user')->get();
$currenttime = Carbon::now();
// return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));
return view('user.index', compact('projects'));
}
}
Model user:
public function project(){
return $this->belongsTo('App\Project', 'project_id','id');
}
Model project:
public function users() {
return $this->hasMany('App\User', 'id');
}
HTML/Blade:
#if(isset($projects))
<table class="table table-striped table-hover table-dynamic datatable">
<thead>
<tr>
<th>{{ trans('common.project') }}</th>
<th>{{ trans('common.workers') }}</th>
<th>{{ trans('common.completion_date') }}</th>
<th>{{ trans('common.status')}}</th>
</tr>
</thead>
<tbody>
#foreach($projects as $project)
<tr>
<td>{!! link_to_route('project.edit', $project->name, [$project->id] )!!}</td>
<td>{{ $project->id }}</td>
<td>{{ $project->completion_date }}</td>
#if (($project->completed) == 1)
<td><span class="label label-success">{{ trans('common.completed') }}</span></td>
#elseif(($project->completion_date) < $currenttime )
<td><span class="label label-danger">{{ trans('common.toolate') }}</span></td>
#elseif(($project->active) == 0)
<td><span class="label label-default">{{ trans('common.inactive') }}</span></td>
#else
<td><span class="label label-warning">{{ trans('common.inprogress') }}</span></td>
#endif
</tr>
#endforeach
</tbody>
</table>
#endif
You have to provide the method name that defines the relationship.I mean users not user
public function index(Project $project)
{
$projects = $project->with('users')->get();
$currenttime = Carbon::now();
// return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));
return view('user.index', compact('projects'));
}