how to view different data from same row in laravel 8 - php

Here you can see I can store two different team names in my table
.
This is my database
I want to view them in my index.blade.php so I did this in my index.blade.php but it`s showing team 1 name everytime.
<table id="example1" class="table table-bordered table-striped table-sm">
<thead>
<tr>
<th>SL</th>
<th>Match Name</th>
<th>Match Slug</th>
<th>Team 1 Name</th>
<th>Team 2 Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->match_name }}</td>
<td>{{ $row->match_slug }}</td>
<td>{{ $row->team->team_name }}</td>
<td>{{ $row->team->team_name }}</td>
</tr>
#endforeach
</tbody>
</table>
This is my index method
public function index()
{
$data=Matchh::all();
$team=Team::all();
return view('admin.manage.matchh.index', compact('data','team'));
}

Try this apply this code
$Matchh = DB::table('Matchh')
->join('Team', 'Matchh.team_id', '=', 'Team.team_id')
->join('Team', 'Matchh.team2_id', '=', 'Team.team2_id')
->select('Matchh.matchh_name','Matchh.matchh_slug','Team.team1_name','Team.team2_name_')
->get();

you need a self join per reference table.
Try this:
$result = DB::select('SELECT
m.match_name,
m.match_slug,
t1.team_name as team1_name,
t2.team_name as team2_name
FROM matchh as m
LEFT JOIN team as t1 ON m.team_id = t1.id
LEFT JOIN team as t2 ON m.team2_id = t2.id');
return $result;

Related

How to group rows that have same Group IS as a foreign key?

I just trying to make a group of students who have the same group ID and calculate the CGPA of only those students who have belong to the same Group.
Refrence Example
Here's is the code for Controller
public function View_GroupStudent()
{
$allot_app = AllotmentApps::select(
"AllotmentApps.grpID",
"AllotmentApps.sname",
"AllotmentApps.cgpa"
)
->orderBy('grpID')
->get();
return view('deny', compact('allot_app'));
}
Views:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
<tr>
#foreach ($allot_app as $allot_app)
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ $allot_app->sum('cgpa') }}</td>
</tr>
#endforeach
</table>
Also help me in creating a proper view for that.
I was confused by your image because the red numbers weren't the averages at all. You should be able to achieve what you're trying to do with the following:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
#foreach ($allot_app->groupBy('grpID') as $grouped)
#foreach ($grouped as $allot_app)
<tr>
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ number_format($grouped->avg('cgpa'), 2) }}</td>
</tr>
#endforeach
#endforeach
</table>

Extracting a list of products bu category using doctrine

I want to extract a list of products by given category, I dont know how to do that! I have found findAll which return all products in database should I use dql?
somthing like:
my controller
public function ProductByCategory(Request $request, Category $cat)
{
$product =new Product() ;
$form = $this->createForm('AppBundle\Form\ProductType',$product);
$query = $em->createQueryBuilder('p')
->where('p->getCategory()->getId() = :categoryid') /*I mean where id
product =id category */
->setParameter(' categoryid ',$cat->getId())
->getQuery();
$products = $query->getResult();
return $this->render('AppBundle:Category:ListProductByCategory.html.twig', array('products' =>$products));
}
my twig will looks like:
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product name</th>
<th>Product price</th>
<th>quantity</th>
<th>color</th>
</tr>
</thead>
<tbody>
{% for p in products %}
<tr>
<td>{{p.name }}<td>
<td>{{ p.price }}</td>
<td>{{ p. quantity }}</td>
<td>{{ p. color }}</td>
<td>
Update
delete
</td>
</tr>
{% endfor %}
</tbody>
</table>
Upadte
I am updating my post with the right code I hope that will help sombody
$products= $this->getDoctrine()->getManager()->getRepository('AppBundle:Product')->findByCategory($cat->getId());
try this to get your products based on category Id
$products = $this->manager->getRepository('AppBundle:Product')
->findOneByCategory($cat->getId());

How to us timestamp in query builder in laravel 5.3 to tack users registered in last 5 days

I want to list down the users who are registered in last 5 day.. To list down all the users 1 use following query..
$users = User::select('*')->orderby('created_at', 'desc')->get();
and fetch it in view like this.
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td>{{ \App\Points::getUserPoints($u->id) }}</td>
</tr>
<?php }?>
</tbody></table>
but what will be the query to fetch users who have registered in last 5 days..?? any one please tell me a good query..
You can use Carbon (docs) to do this:
$now = Carbon::now();
$users = User::where('created_at', '>=', $now->subDays(5))->get();
Make sure to "use Carbon\Carbon;" at the top of the file.

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Select List From Database With Parameter

Hi there i have some problem getting list from database using laravel 4.1
I have session that stored some value about companyid. Then i want to print the list to table based on the companyid itself. Let say i can access the session using {{ Session::get('name')}}
This is my controller index()
public function index($cid) {
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}
then i want to print the data to table in view
#if($pengguna->count())
<table class="table table-striped table-bordered" id="table_pengguna_list">
<thead>
<tr>
<th>No</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
<th>Last Login</th>
<th class="td-actions">Action</th>
</tr>
</thead>
<tbody>
#foreach ($pengguna as $pgn)
<tr>
<td>{{ $index++ }}</td>
<td>{{ $pgn->firstname }}</td>
<td>{{ $pgn->lastname }}</td>
<td>{{ $pgn->username }}</td>
<td>{{ $pgn->email }}</td>
<td>{{ $pgn->level }}</td>
<td>{{ $pgn->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
....
how to pass the parameter from session to the controller then? so basically pass the {{ Session::get('name')}} to $cid in index controller.
thank you.
hi there i have found the solution, just place refactor the index controller to;
public function index() {
$cid = Session::get('name');
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}

Categories