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>
Related
I have two tables. One employees and one timesheets
I would like to have correct name at the result but different ID. If you see the result I have the same name everywhere.
How can make the code to fixed my challenge?
Thank you.
In controller I have this code
$profile = ['no' => Auth::user()->no];
$timesheets = Timesheet::where($profile)->select('*')->orderBy('created_at','DESC')->get();
foreach ($timesheets as $employee) {
$emp = ['identification' => $employee->identification];
$oneemployee = Employees::where($emp)->select('*')->orderBy('created_at','DESC')->get();
}
return view('contractor.employees.timesheets', compact('timesheets', 'oneemployee'));
and in blade I have
#foreach ($timesheets as $row1)
<tr>
<td>#</td>
#foreach ($oneemployee as $row2)
<td>{{ $row2->fname}}</td>
<td>{{ $row2->lname}}</td>
#endforeach
<td>{{ $row1->identification}}</td>
<td>{{ $row1->week}}</td>
<td>{{ $row1->year}}</td>
</tr>
#endforeach
According to your commit, I suggest the following solution
If you want dispay timesheet with employees you can use eager loading.
For example:
In your controller:
$profile = ['no' => Auth::user()->no];
$timesheets = Timesheet::where($profile)->with("employees")->select('*')->orderBy('created_at','DESC')->get();
return view('contractor.employees.timesheets', compact('timesheets'));
In your Timesheet model add hasOne relation to Employee:
public function employee(){
return $this->hasOne(Employee::class,'identification','identification')
}
And your view:
#foreach ($timesheets as $timesheet)
<tr>
<td>#</td>
<td>{{ $timesheet->employee->fname}}</td>
<td>{{ $timesheet->employee->lname}}</td>
<td>{{ $timesheet->identification}}</td>
<td>{{ $timesheet->week}}</td>
<td>{{ $timesheet->year}}</td>
</tr>
#endforeach
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>
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>
I have got this relationships:
CUSTOMER
id
name
ADDRESS
id
name
customer_id
country_id
COUNTRY
id
name
My Customer model:
class Customer extends Model
{
use SoftDeletes;
public function addresses()
{
return $this->hasMany(Address::class);
}
My address model:
class Address extends Model
{
use SoftDeletes;
public function country()
{
return $this->hasOne(Country::class);
}
And right now doing:
Customer::with('addresses')->orderBy('id', 'DESC')
I can get all customer with his addresses. But How Can I get the name of country fro customer?
I know getting in blade, if I do 2 loops I can create $address variable and get country names:
loop customers
loop adddresses
$address->name
$address->country (name of country)
But if I try in Vue:
<tr v-for="(customer, index) in customers" :key="customer.id">
<td>{{ customer.id}}</td>
<td>{{ customer.firstname}}</td>
<td>{{ customer.lastname}}</td>
<td v-if="customer.addresses">
<tr v-for="(address, index) in customer.addresses" :key="address.id">
<td>{{ address.name }}</td>
<td>{{ address.country }}</td>
</tr>
</td>
I dont get any value in addres.country. I mean, how Can I relate CUSTOMERS & COUNTRIES for usign in Vuejs?
Thans in advance
I found a solution!
I updated Address model like this:
class Address extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
protected $with = ['country'];
}
then I can solve country name like this:
<tr v-for="(customer, index) in customers" :key="customer.id">
<td>{{ customer.id}}</td>
<td>{{ customer.name}}</td>
<td v-if="customer.addresses">
<tr v-for="(address, index) in customer.addresses" :key="address.id">
<td>{{ address.name }}</td>
<td>{{ address.country.name }}</td>
</tr>
</td>
How to get column names and value of a table created on runtime with laravel.
$table = 'table_name';
$columns = DB::getSchemaBuilder()->getColumnListing($table);
$records = DB::table($table)->get();
Now i get can column name by this in view.
<tr>
#for($i = 0; $i < sizeof($columns); $i++)
<th>{{ ucfirst(str_replace('_', ' ',$columns[$i]) )}}</th>
#endfor
</tr>
Now how can i display column values below column headings
#foreach($records as $key=>$row)
{{ $row->id}}
#endforeach
I need this type of output
Table Name
id | colum_1 |colum_2 | etc
1 | test | test11
2 | asdf | asf
Change your second #foreach to be:
#foreach($records as $row)
<tr>
#foreach($row as $data)
<td>{{ $data }}</td>
#endforeach
</tr>
#endforeach