repeat laravel query result 4 times - laravel-5

i have a question on how to accomplish this task..
how can i repeat the result of a query 4 times and then add a name from other database.. please check this attach image.
i have a database for flight number. and a database for employee name.
this is my code so far
#foreach ( $daily_flight as $d_f)
{{ $d_f->flight_num}}
#endforeach
<table>
#foreach ($csa as $emp)
<tr>
<td>{{ $emp->name }}</td>
</tr>
#endforeach
</table>

Let's imagine that you have 2 tables: flights and employees. Let us also imagine that the flights Table has columns like id, flight_num, etc. Simultaneously; we would also assume that the employees Table contain fields like: id, flight_id, name, etc. Now, working under this arbitrary condition, one can simply write a Query that joins the 2 tables provided there is any kind of JoinCondition that can be established between the 2 - for example: ON employees.flight_id=flights.id.
This would be how to do this from the Controller:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use App\Flights; //<== JUST SOME ARBITRARY IMPORTS... MODIFY TO SUIT YOU
use App\Employees; //<== JUST SOME ARBITRARY IMPORTS... MODIFY TO SUIT YOU
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
class FlightsController extends Controller {
public function flightSchedule(){ //<== ANOTHER ARBITRARY ACTION METHOD
// ASSUME FOR AN INSTANT THAT YOU HAVE 2 TABLES:
// 1.) flights
// 2.) employees
// YOU COULD USE THE join CLAUSE TO JOIN THE 2 TABLES TOGETHER
$flightData = DB::table('flights')
->join('employees', 'employees.flight_id', '=', 'flights.id')
->select('flights.*', 'employee.*')
->groupBy('employees.name')
->get();
// THEN ONCE YOU HAVE YOUR RESULT, IT WOULD BE ONLY
// A MATTER OF LOOPING THROUGH THE RESULT-SET IN THE VIEW SCRIPT
// AND STRUCTURING YOUR MARK-UP AS YOU SEE FIT LIKE THIS:
return view("flights.schedule", array("flightData"=>$flightData));
}
}
Now, in your View Script in our Arbitrary Example; flights/schedule.blade.php
<!-- FICTITIOUS FILE-NAME=> flights/schedule.blade.php -->
<table>
<tr>
<th>FLIGHT NUMBER</th>
<th>ASSIGNED EMPLOYEE</th>
</tr>
<tbody>
#foreach($flightData as $data)
<tr>
<td>{{ $data->flight_num }}</td>
<td>{{ $data->name }}</td>
</tr>
#endforeach;
</tbody>
</table>

Related

How to get the counts of Student for each Teachers in a single collection by Eloquent

A Teacher has many Students. When I am showing the Teachers list I also want to show the counts of Student for each Teachers. How can I do this by using Eloquent?
I can find the Teachers from this,
$teacher= Teacher::where('teacher_status','active')->get();
I can find the Student count from this
$student_count = Student::where('teacher_id','teachers.id')->count();
How can I conbine this two query and return the response in a single array/collection?
In your teacher model, create the relationship students:
class Teacher extends Model
{
public function students()
{
return $this->hasMany(Student::class, 'teacher_id');
}
}
In your controller you can do the following:
public function example(){
$teachers = Teacher::where('teacher_status','active')->withCount('students')->get();
return view('teacherViewExample', compact('teachers'));
}
In your view (teacherViewExample):
<table>
<thead>
<tr>
<th>Teacher Name</th>
<th>Students Count</th>
</tr>
</thead>
<tbody>
#foreach($teachers as $teacher)
<tr>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->students_count }}</td>
</tr>
#endforeach
</tbody>
</table>
Full documentation on how to use withCount() here: https://laravel.com/docs/9.x/eloquent-relationships#counting-related-models
Sometimes you may want to count the number of related models for a
given relationship without actually loading the models. To accomplish
this, you may use the withCount method. The withCount method will
place a {relation}_count attribute on the resulting models:
If you want to count the number of students related to teacher without actually loading them you may use the withCount method and this add new propery named by a {relation}_count column on your resulting models. For example:
Teacher::where('teacher_status','active')->withCount('students')->get();
Also you need and to your Teacher model hasMany relation method to Students
In case frontend and backend are separated, i.e Laravel for backend, Angular for frontend, below are examples for Laravel:
In api.php,
Route::get('teacher-with-students-count', 'TeacherController#getTeacherWithStudentsCount');
In Teacher.php (model)
class Teacher extends Model
{
public function students()
{
return $this->hasMany(Student::class, 'teacher_id', 'id');
}
}
In TeacherController.php
public function getTeacherWithStudentsCount()
{
$teachers = Teacher::where('teacher_status','active')->withCount('students')->get();
return $this->giveSuccessResponse($teachers);
}

Sort the data linked by a relationship by date

I'm having a problem sorting projects based on the date of the last task associated with it. I explain better.
I have a relationship between project and tasks One to Many (as a project can have one or more tasks). In the project table, for each project, I have a column where I print the date of the last task done, as you can see in the blade view shown below. So far everything ok.
Now the projects are sorted by default in ascending order of creation of each project (so the last project shown in the table is the one that is added last).
What I want is to sort the projects by the date obtained from the last task associated with that project through the column corresponding to the date of the last task I get as shown below.
Model Project:
class Project extends Model
{
use HasFactory;
protected $fillable = [
'title',
'name',
];
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Controller:
public function index()
{
$projects = Project::withCount('tasks')->get();
return view('project.index', compact('projects'));
}
View:
<table id="tabledata">
<thead>
<tr>
<th></th>
<th>Title</th>
<th>Name</th>
<th>Date last task</th>
<th>N. Tasks</th>
</tr>
</thead>
<tbody>
#foreach ($projects as $project)
<tr>
<td></td>
<td class="p-4">{{ $project->title}}</a></td>
<td class="p-4">{{ $project->name }}</a></td>
<td class="p-4">{{ $project->tasks()->latest()->first()->created_at->format('d/m/Y') }}</td>
<td class="text-center">{{ $project->tasks_count}}</td>
</tr>
#endforeach
</tbody>
</table>
To sort your query by the data of relationship column, you can change your query to something like this:
$projects = Project::with('tasks')->orderByDesc(Task::select('created_at')->whereColumn('tasks.project_id', 'project.id')->latest()->take(1))->withCount('tasks')->get();
orderBy() and orderByDesc() query builder methods support passing a query, instead of just a column name.
What we did here is:
We took the tasks of the project
We passed in a query as a subquery to the orderByDesc query builder method.
Inside the subquery:
We selected the created_at column from the tasks table.
We limited the results by comparing the project_id column of tasks table to the id column of the projects table.
Then we call the latest() method, which sorts the tasks to the recent record first.
And lastly, we limit the results to only one row, since a subquery can only return a single row and column
You can read more about how it works with different types of relationships here

Property [name] does not exist on this collection instance. Eloquent Laravel

I have a potentially basic problem for Eloquent. I want to show only the name corresponding with Role of that user. I used the Eloquent Relationship ManytoMany. But I can't get that to work.
I tried point to the name using the $users parameter but this didn't work.
My project has three files such as Role model, StudentController, find_username view.
Thank you.
Role Model:
class Role extends Model
{
protected $fillable = ['id','role_name','role_level','role_note'];
public function users()
{
return $this->belongsToMany('App\User', 'role_users', 'user_id', 'role_id');
}
}
My StudentController:
class StudentController extends Controller
{
public function find_username()
{
$roles = Role::all();
foreach($roles as $role)
{
echo $role->users . '<br>';
}
return view('find_username',['roles'=> $roles]);
}
}
My find_username view:
#extends('master')
#section('content')
<div class="row">
<table class="table">
<thead>
<tr>
<th>ID Role</th>
<th>Role name</th>
<th>Role level</th>
<th>Role note</th>
<th>User name</th>
</tr>
</thead>
<tbody>
#foreach ($roles as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['role_name']}}</td>
<td>{{$item['role_level']}}</td>
<td>{{$item['role_note']}}</td>
<td>{{$item->users}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
You have many roles. Each of these roles have many users. Your table will be fine as it is to show the name of the role, roll level, and role note, because these are on the looped $item variable individually.
The problem is that while the $item holds those singular fields, the $item->users is another collection of user objects. Which means you have added another dimension onto your table. You might have 1 or 100 users to fit inside that <tr>.
To demonstrate, try this code, specifically adding in all the users names (adding the parameter to each user object in a new loop):
#foreach ($roles as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['role_name']}}</td>
<td>{{$item['role_level']}}</td>
<td>{{$item['role_note']}}</td>
#foreach($item->users as $user)
<td>
{{$user->name}}
{{$loop->last?"":" | "}} //<-- just to separate names for this test
</td>
#endforeach
</tr>
#endforeach
If you have your models and relations set up correctly, this will show you your users for each role. However, as you can see, this may not be the best way to display this data. You might have dozens of users per role. You may wish to consider making a totally separate table for each role's users. Maybe click on something for the role on this table and bring up a new list or table of those users who have the role.
HTH

How i get the name of user in this foreach

This is my controller
$user_rank = Sprintbacklog::select('assign_user_id',DB::raw('count(*) as
jumlah_data'))->where('id_sprint',$id)->where('finish',1)-
>groupBy('assign_user_id')->get();
This is foreach in blade
#foreach($user_rank as $name_and_finish)
<tr>
<td>{{ $name_and_finish->assign_user_id}}</td>
<td><span class="badge">{{ $name_and_finish->jumlah_data }}</span></td>
</tr>
#endforeach
maybe you must join the models, to access the user like
$user_rank = Sprintbacklog::join('users',sprintbacklogs.id_user,'=','users.id')
->where('id_sprint',$id)->where('finish',1)
->select('assign_user_id',DB::raw('count(*) as jumlah_data'))
->groupBy('assign_user_id')
->get();
to access the name in user table, but you need an user id at the table that you want to join with.

Trying to get property of non-object [laravel 5.2]

I'm getting trouble in getting the data from foreign key .
already make relation between 2 tables, but still error gave me this "Trying to get property of non-object"
this is my models
public function tourism()
{
return $this->belongsTo('App\Models\Tourism','tourism_id');
}
this is my controller
$ratings = Ratings::orderBy('rating','desc')->get();
$ratings = $ratings->take(6);
and this is my blade
<tbody>
#if($ratings)
#foreach($ratings as $data)
<tr>
<td class="center">{{$data->id}}</td>
<td class="center">{{$data->tourism_id->nama}}</td>
<td class="center">{{$data->rating}}</td>
</tr>
#endforeach
#endif
</tbody>
thanks
Try
<td class="center">{{$data->tourism->nama}}</td>
EDIT
This situation occurs when you trying get nama property.
But tourism_id it's just an integer, am I right?
First of all,for accessing related model you should use you relationship method(tourism) instead of FK field(tourism_id).
Then you should check are there any related model in tourims() or not. For this purpose i recommend you use ternary operator, so your line should be something like:
<td class="center">{{$data->tourism->first() ? $data->tourism->first()->nama : 'No tourism'}}</td>
Assuming you already have this controller and the model relationship as above:
function ratingsView() {
$ratings = Ratings::orderBy('rating','desc')->get();
$ratings = $ratings->take(6);
return View('rating')->with('ratings',$ratings);
}
In your views,
<tbody>
#if($ratings)
#foreach($ratings as $data)
<tr>
<td class="center">{{$data->id}}</td>
<td class="center">{{$data->tourism()->first()->nama}}</td>
<td class="center">{{$data->rating}}</td>
</tr>
#endforeach
#endif
</tbody>
Make sure your rating table have id, rating as well as tourism_id attributes and your tourism table have name attribute as well as of course there is data in both tables.
You definitely need to check the the availability of the data and handle it accordingly. But that is different issue
in your controller use it like this:
$ratings = Ratings::orderBy('rating','desc')->take(6)->get();

Resources