I have 2 tables in my database - Bots and Results. The bots table is to have information on each specific bot alone. Each bot should have several results attached to it. I will simplify what I have in my columns:
Bots Table
Device | Info1 | Info2 | Info3
Results Table
Device | Data1 | Data2 | Data3
As you can see, each table has the column named Device. Now, I have 2 Models of course for each table - the regarding info is as follows:
Bot Model
public function results()
{
return $this->hasMany('App\Result', 'device', 'device');
}
Result Model
public function bots()
{
return $this->belongsTo('App\Bot', 'device', 'device');
}
I have a view with a Datatable in it which I am trying to list every Device and Every Result corresponding to that Device. What I have here is an example of what was made before eloquent relations was brought in. I use to only have the Results table, so therefore it was farely simple to do a foreach and loop throught every result I had to display on the Datatabes
Datatables View
#foreach ($results as $result)
<tr>
<td>
{{ $result->device }}
</td>
<td>
{{ $result->info1 }}
</td>
<td>
{{ $result->info2 }}
</td>
<td>
{{ $result->info3 }}
</td>
<td>
{{ $result->data1 }}
</td>
<td>
{{ $result->data2 }}
</td>
<td>
{{ $result->data3 }}
</td>
</tr>
#endforeach
How can I get this to work now that the device name and information is actually stored in a separate table?
You can fetch your device information with its associated results using eager load like this
$bots = Bot::with('results')->get();
Now in view you can do it like this
#foreach ($bots as $bot)
<tr>
<td>
{{ $bot->device }}
</td>
<td>
{{ $bot->info1 }}
</td>
<td>
{{ $bot->info2 }}
</td>
<td>
{{ $bot->info3 }}
</td>
</tr>
#foreach ($bot->results as $result)
<tr>
<td>
{{ $result->data1 }}
</td>
<td>
{{ $result->data2 }}
</td>
<td colspan="2">
{{ $result->data3 }}
</td>
</tr>
#endforeach
#endforeach
Related
I have a many-to-many relationship between my User and Wallet models.
Wallet Model
public function users()
{
return $this->belongsToMany(User::class, 'user_wallet', 'user_id', 'wallet_id');
}
User Model
public function wallets()
{
return $this->belongsToMany(Wallet::class, 'user_wallet', 'user_id', 'wallet_id')->withPivot('balance');;
}
Then in my Blade file, I tried the following.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse
It works fine, but the problem is, it shows two links in the table for one user. However, it should be showing one.
And if I do:
#forelse($user->wallets as $wallet)
{{ count(array($user)) }}
#empty
<td colspan="5" class="text-center">
No wallet exist
</td>
#endforelse
The result would be 1.
The problem is coming from 'userId' => $user->id of the link. Because if I remove it, only 1 link would appear, which is correct. But still, I do need the userId and don't know how to pass that into the route name.
So what's going wrong here? I need one link for each user, and I don't know why it prints two of them.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse`
Doesn't $user->wallets returns collection of all wallets associated with each user?
maybe you need to check if any user has more than one wallet or if you want to show only one you can user $user->wallets[0] to show the first wallet model since its a collection
I have two tables in my program. The first one is tbfininst and the other is tbsubmission.
The tbfininst have a primary key of fld_code while the tbsubmission have a foreign key of CODCODE. It's a one to many relationship.
Now, I am displaying the rows of tbfininst in my blade.php. It looks like this.
Then, in the Submission (Periodicity) column, I want to insert how many rows from tbsumission does tbfininst have. By the way, the database is already created before I started. So I don't think I can use models or can i?
Anyways, in my controller, I tried using this code:
public function index()
{
$fininst = DB::connection('portaldb')->table('tbfininst')->groupBy('fld_code')->get();
$periodicity = DB::connection('portaldb')->table('tbsubmission')->select('CODCODE')->get();
return view('pages/compliance', ['fininst' => $fininst, 'periodicity' => $periodicity]);
}
and in my blade.php:
#foreach($fininst as $fin)
<tr>
<td>
<center>
{{ $fin->fld_code }}
</center>
</td>
<td>{{ $fin->fld_name }}</td>
<td>
#if ($fin->fld_status == 4)
<p>Compliant</p>
#else
<p>Non-Compliant</p>
#endif
</td>
<td>
#if ($fin->fld_ip || $fin->fld_twofactor)
<p>Compliant</p>
#else
<p>Non-Compliant</p>
#endif
</td>
<td>
#foreach($periodicity as $p)
#if ($fin->fld_code == $p->CODCODE)
{{$fin->fld_code}}
#else
#endif
#endforeach
</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
#endforeach
When I inserted the #foreach of the periodicity, my blade.php output kinda not works.
I don't know why my template is not working. I just started using laravel two days ago and I'm still learning. I accept criticism, it will help me improved. Thank you.
You are just showing the matching values where you should be counting the matching values and showing them like this
#foreach($fininst as $fin)
#php $count = 0; #endphp
#foreach($periodicity as $p)
#if ($fin->fld_code == $p->CODCODE)
#php ++$count; #endphp
#else
#endif
#endforeach
<tr>
<td>
<center>
{{ $fin->fld_code }}
</center>
</td>
<td>{{ $fin->fld_name }}</td>
<td>
#if ($fin->fld_status == 4)
<p>Compliant</p>
#else
<p>Non-Compliant</p>
#endif
</td>
<td>
#if ($fin->fld_ip || $fin->fld_twofactor)
<p>Compliant</p>
#else
<p>Non-Compliant</p>
#endif
</td>
<td>
{{$count}}
</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
#endforeach
I have a user who speaks native language lets say English and wants to learn French.
How can I find a list of match where users native is French and wants to learn English.
Its a language exchange program so I would like to list users with the match.
I have Users, Language_User and Language table. in Language_User table I have an extra field called type(learn or native).
Users
id
name
...
Language_User
id
user_id
language_id
type('learn','native')
Language
id
language
Queries
First I am getting all the users that does not have a role as 'Admin' or is not him/herself.
$other_users = User::with('languages')->with('departments')->with('hobbies')->with('universities')->with('years')->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->get();
Then, I am getting loggedin users $learn language and $native language to compare with other users
$learn = Auth::user()->languages()->wherePivot('type', 'learn')->select('language_id')->first();
$native = Auth::user()->languages()->wherePivot('type', 'native')->select('language_id')->first();
In View
gives loggedin user language to learn
{{$learn->language_id}}
gives loggedin user native language
{{$native->language_id}}
View Code
#foreach($other_users as $user)
#foreach($user->languages as $lang)
#if($lang['pivot']['type'] ==='native' AND $lang['pivot']['language_id'] ===$learn['language_id'])
<tr>
<td>{{$user->firstname}} </td>
<td>{{$user->lastname}} </td>
<td>{{$user->email}} </td>
<td>{{$user->photo}} </td>
<td>{{$user->bio}} </td>
<td>{{$user['universities']->university}} </td>
<td>{{$user['departments']->department}} </td>
<td>{{$user['years']->year}} </td>
<td>
#foreach($user->languages as $lang)
#if($lang['pivot']['type'] === 'native')
{{$lang["language"]}}
#endif
#endforeach
</td>
<td>
#foreach($user->languages as $lang)
#if($lang['pivot']['type'] === 'learn')
{{$lang["language"]}}
#endif
#endforeach
</td>
<td>
#foreach($user->hobbies as $hobby)
{{$hobby->hobby}} <br>
#endforeach
</td>
</tr>
#endif
#endforeach
#endforeach
I am unable to put two if conditions one for checking native language and one for checking learn language. if I do like this:
#if($lang['pivot']['type'] ==='native' AND $lang['pivot']['language_id'] ===$learn['language_id'])
#if($lang['pivot']['type'] ==='learn' AND $lang['pivot']['language_id'] ===$native['language_id'])
I get no rows, whereas user do exist with a match.
I think because I have two pivot type learn and native in an array. how to compare both learn and native? below shown languages format. its many to many relation
"languages":[
{
"id":3,
"language":"Spanish",
"pivot":{
"user_id":2,
"language_id":3,
"type":"native"
}
},
{
"id":4,
"language":"Greek",
"pivot":{
"user_id":2,
"language_id":4,
"type":"learn"
}
}
]
Instead of loop through all the users in the database you could just constrain your query:
$native = Auth::user()->languages()->wherePivot('type', 'native')->first();
$learn = Auth::user()->languages()->wherePivot('type', 'learn')->first();
$other_users = User::with('departments', 'hobbies', 'universities', 'years', 'languages')
->where('id', '<>', Auth::user()->id)
->where('role', '<>', 2)
->whereHas('languages', function ($query) use ($learn) {
$query->where('type', 'native')->where('id', $learn->id);
})
->whereHas('languages', function ($query) use ($native) {
$query->where('type', 'learn')->where('id', $native->id);
})
->get();
Also, just an FYI, $lang['pivot']['type'] can also be written as:
$lang->pivot->type;
Your blade section could then look something like:
#foreach($other_users as $user)
<tr>
<td>{{ $user->firstname }} </td>
<td>{{ $user->lastname }} </td>
<td>{{ $user->email }} </td>
<td>{{ $user->photo }} </td>
<td>{{ $user->bio }} </td>
<td>{{ $user->universities->university }} </td>
<td>{{ $user->departments->department }} </td>
<td>{{ $user->years->year }} </td>
<td>
{{ $user->languages->where('pivot.type', 'learn')->first()->language }}
</td>
<td>
{{ $user->languages->where('pivot.type', 'native')->first()->language }}
</td>
<td>
{!! $user->hobbies->pluck('hobby')->implode('<br>') !!}
</td>
</tr>
#endforeach
Hope this helps!
I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
I had Users table and group table, when i load a group table, it loads the user which have same group_id as the group table id. it works, but my problem is the foreach was quite a mess. the output looks like this..
however, i want to make the output look like this.
here is my code..
<tbody>
<?php $i = 0; ?>
#foreach ($auser as $res)
<?php $i += 1; ?>
<tr #if($i%2==0) class="bgcolor" #endif>
<td>{{ $res->id }}.</td>
<td id="showdet">{{ $res->nama }}</td>
<td>{{ $res->description }}</td>
<?php $user = DB::table('users')->where('devgroup',$res->id)->get();?>
#foreach($user as $mem)
<td>{{ $mem->name }}</td>
#endforeach
<td>
<a class="default-btn" href="/pms/admin/group/edit/{{ $res->id }}">Edit</a>
<a type="submit" class="default-btn del" data-id="devgroup" href="#"{{ $res->id }}">Delete</a>
</td>
</tr>
#endforeach
</tbody>
what did i do wrong? thank you for your help.
You create new TD for each member. The nested foreach has to be:
<td>
#foreach($user as $mem)
{{ $mem->name }}<br>
#endforeach
</td>
The result will be:
<td>
Name 1<br>
Name 2<br>
Name 3<br>
</td>
I don't know the template engine you used, add inside a loop condition and don't put <br> after the last one member.