show data in blade from pivot table - laravel

I show a pivot table called community_competition using a blade file like this:
But I want to show like this, how ?
no
name
push up
sit up
back up
squat jump
1.
Dr Keith Crist
20
10
12
23
2.
Jaren Wunsch
34
13
24
53
And here is my blade to show the table:
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">name</th>
<th scope="col">push up</th>
<th scope="col">sit up</th>
<th scope="col">back up</th>
<th scope="col">squat jump</th>
</tr>
</thead>
<tbody>
#php
$no = 1;
#endphp
#foreach ($data as $v)
<tr>
<th scope="row">{{ $no++ }}</th>
<td>{{ $v->community->name}}</td>
<td>{{ $v->point}}</td>
</tr>
#endforeach
</tbody>
CommunityCompetition model:
public function community()
{
return $this->belongsTo(Community::class);
}
public function competition()
{
return $this->belongsTo(Competition::class);
}
CommunityCompetition Controller:
public function index()
{
$data = CommunityCompetition::with(['community', 'competition'])->get();
return view('cms.rank.index', compact('data'));
}
community table:
$table->id();
$table->string('name', 100);
id
name
1
Dr.Keith Crist
2
Jaren Wunsch
competition table:
$table->id();
$table->string('name', 100);
id
name
1
push up
2
sit up
3
back up
4
squat jump
community_competition table:
$table->id();
$table->bigInteger('community_id')->unsigned();
$table->bigInteger('competition_id')->unsigned();
$table->integer('point')->nullable();
$table->timestamps();
$table->foreign('community_id')->references('id')->on('communities');
$table->foreign('competition_id')->references('id')->on('competitions');
id
community_id
competition_id
point
1
1
1
20
2
1
2
10
3
1
3
12
4
1
4
23
5
2
1
34
6
2
2
13
7
2
3
24
8
2
4
53

Well, your table has 6 columns and you are populating only 3 with this code:
<tr>
<th scope="row">{{ $no++ }}</th>
<td>{{ $v->community->name}}</td>
<td>{{ $v->point}}</td>
</tr>
So, you need to add 3 more tds to complete the other ones. I have no idea what the names are as your community_competition does not store this 3 missing columns.
You should end up with something like this (bare in mind that you have to use the correct model and attribute):
<tr>
<th scope="row">{{ $no++ }}</th>
<td>{{ $v->community->name}}</td>
<td>{{ $v->point}}</td>
<td>{{ $v->sit_up }}</td>
<td>{{ $v->back_up }}</td>
<td>{{ $v->squat_jump }}</td>
</tr>

Related

I want to count total yes against an id in a particular row not a column

I want to sum total yes in a row like following
id
user_id
eligible
asset
requirement
1
1
yes
no
yes
2
1
yes
yes
yes
3
2
no
yes
no
Result should be following:
user_id
yes_count
1
5
2
1
I consider that table as Items.
User model
public function items()
{
return $this->hasMany(Item::class);
}
Controller
$users = User::withCount([
'items as items_eligible_yes_count' => fn($q) => $q->where('eligible', 'yes'),
'items as items_asset_yes_count' => fn($q) => $q->where('asset', 'yes'),
'items as items_requirement_yes_count' => fn($q) => $q->where('requirement', 'yes'),
])
->get();
View
<table>
<thead>
<tr>
<th>user_id</th>
<th>yes_count</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->items_eligible_yes_count + $user->items_asset_yes_count + $user->items_requirement_yes_count }}</td>
</tr>
#endforeach
</tbody>
</table>

not able to show array in Table #foreach

I am new to Laravel and coding. Trying to show Month name (Array) in table but not able to do the same. I tried few things like "json_encode" but got error each time.
My controller code is
$months = array();
for ($i = 0; $i < 12; $i++) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('M-Y', $timestamp);
}
Output is
array:12 [▼
3 => "Mar-2022"
2 => "Feb-2022"
1 => "Jan-2022"
12 => "Dec-2021"
11 => "Nov-2021"
10 => "Oct-2021"
9 => "Sep-2021"
8 => "Aug-2021"
7 => "Jul-2021"
6 => "Jun-2021"
5 => "May-2021"
4 => "Apr-2021"
]
View file
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th align="center">Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($months as $item )
<tr>
<td>{{ $item->$months }}</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
During your #foreach() loop, $item is not an Object, it is a String 'Mar-2022' through 'Apr-2021'. You need to adjust your code:
#foreach($months as $key => $label)
<tr>
<td>{{ $label }}</td>
<tr>
#endforeach
If you need the 1 through 12 values, they are available in each iteration as $key.

Laravel : "Object of class stdClass could not be converted to string" while pass variable into closure

I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard

How to select relational data on different table by id in laravel

i Have 3 Table , the name is : user , machine , maintance . this schema like this
*User
id | name | email | password | created_at | updated_at
1 | john | A#mail | ******* , etc
*Machine
id | name_machine | created_at | updated_at
1 | shock | .....etc
*Maintance
id | Machine_id | user_id | question | status | created_at |
updated_at
1 | .........1........ |.......1......| blablabla etc ..
now i want to show data on table "Maintance" with show data "name" (user table) and name_machine( machine table) by id .
this is my controller and my view
public function show($id)
{
$maintance = Maintance::all();
return view('users.view',['maintance' => $maintance]);
}
my view
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
#foreach ($maintance as $i)
<tr>
<td>{{ $i->machine_id}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user_id}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
#endforeach
</tbody></table>
this view data is not having error ,but i dont know how to showing this data from different table . can someone help ?
Assuming your Maintance model belows,
public function user(){
return $this->belongsTo(User::class);
}
public function machine(){
return $this->belongsTo(Machine::class);
}
assuming that your Model have these above relations.
and do the below to get your data,
$maintance = Maintance::with(['user','machine'])->get();
and in your view
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
#foreach ($maintance as $i)
<tr>
<td>{{ $i->machine->machine_name}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user->name}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
#endforeach
</tbody></table>

Laravel5.1, Relation, EloquentORM, Many to Many, Sort

<table border="true">
<tr>
<th style="width:100px">Table</th>
<th colspan="3" style="width:300px">Columns</th>
</tr>
<tr>
<td style="width:100px">Articles</td>
<td style="width:100px">id</td>
<td colspan="2" style="width:200px; color: #aaaaaa">other dependent variables</td>
</tr>
<tr>
<td style="width:100px">Article_Tag</td>
<td style="width:100px">id</td>
<td style="width:100px">article_id</td>
<td style="width:100px">tag_id</td>
</tr>
<tr>
<td style="width:100px">Tags</td>
<td style="width:100px">id</td>
<td style="width:100px">name</td>
<td style="width:100px"></td>
</tr>
</table>
Then, I want to sort Articles table by [name] of Tags table.
I tried eager-loading with closure below, but it did'nt work.
Model Article and Tag are connected with each other just by belongsToMany,
and I succeed in output their data, but they weren't sorted.Offcourse, I did'nt give getTag any argument.(When I gave argument, they weren't narrowed by [name] either.)
use App\Article;
use App\Tag;
...
public function __construct(Article $article)
{
$this->article = $article;
}
...
public function getTag($tag = NULL)
{
$flag = isset($tag);
$articles = $this->article->orderBy('created_at', 'desc')->with([
'tags' => function($query) use ($flag, $tag){
($flag)
? $query->where('name', $tag)
: $query->orderBy('name');
}
])->paginate(15);
Try this variant:
$articles = $this->article->with([
'tags' => function($query) use ($flag, $tag){
return $flag
? $query->where("name", $tag);
: $query->orderBy("name")
}
])->paginate(15);

Resources