how to get columns name by columns value in laravel - php

table(skill)
have many skills, columns type boolean(0,1),by laravel
i want to get the names of columns if skills value equal 1 and user_id equal 1,
without say the name of column becouse they 50 skills ,i tried this on sublime
<?php
$variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
#foreach($variable2 as $key1 => $value1)
#if($value1='1')
<span class="tags">{{ $key1 }}</span>
#endif
#endforeach

Well, looks like you forgot, that get returns a collection of skills. You must use two loops.
<?php
$userSkills = App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
<!-- loop through a list of user skills (get gives us a set of skills -->
#foreach($userSkills as $skill)
<!-- loop through skill properties -->
#foreach($skill->toArray() as $key => $value)
#if($value)
<span class="tags">{{ $key }}</span>
#endif
#endforeach
#endforeach

<?php $variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->where('user_id', 1)
->get();
$skills = $variable2->filter(function ($item) { return $item === 1; })->keys();
?>
#foreach($skills as $key)
<span class="tags">{{ $key }}</span>
#endforeach

Related

Prevent foreach from loop html tag multiple time in blade laravel

I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach

Join Find and Foreach retrieving wrong data in Laravel

I'm working on a join/find Laravel DB to retrieve the correct data, but it is showing the last entry of the database repeated 13 times.
I hope you can help me.
Here is the code.
In controller
$joinCot = Cotizaciones::findOrFail($id);
$joinCot = DB::table('cotizaciones')->join('prod_cotizaciones', 'prod_cotizaciones.id_cotizacion', '=', 'cotizaciones.id')
->join('users', 'users.id', '=', 'cotizaciones.id_usuario')
->join('clientes', 'clientes.id', '=', 'cotizaciones.id_cliente')
->select('cotizaciones.id', 'cotizaciones.id_usuario', 'cotizaciones.id_cliente', 'cotizaciones.equipo', 'cotizaciones.servicio_venta', 'cotizaciones.pdf', 'cotizaciones.fecha', 'cotizaciones.status', 'prod_cotizaciones.id as idPC', 'prod_cotizaciones.partida as partida', 'users.id as idU', 'clientes.id as idC', 'clientes.nombre_cliente')->get();
In blade
#foreach ($joinCot as $key => $value)
<div>{{ $joinCot->idPC }}</div>
#endforeach
If I make a return of $joinCot it shows
[{"id":40,"id_usuario":1,"id_cliente":1,"equipo":"Pipeta","servicio_venta":"Servicio","pdf":null,"fecha":"2021-05-27","status":"Pendiente","idPC":27,"partida":1,"idU":1,"idC":1,"nombre_cliente":"Instituto Nacional de Enfermedades Respiratorias Ismael Cos\u00edo Villegas"},{"id":40,"id_usuario":1,"id_cliente":1,"equipo":"Pipeta","servicio_venta":"Servicio","pdf":null,"fecha":"2021-05-27","status":"Pendiente","idPC":28,"partida":2,"idU":1,"idC":1,"nombre_cliente":"Instituto Nacional de Enfermedades Respiratorias Ismael Cos\u00edo Villegas"}]
I have 2 entries with idPC = 27 and the second one with idPC = 28.
But at the time to return the view the foreach just throws 28282828282828282828282828.
You are accessing wrong on inside loop $joinCot->idPC it should be $value->idPC.$value will return each row from array or object.
#if(isset($joinCot)&&count((array)$joinCot))
#foreach ($joinCot as $key => $value)
<div>{{$value->idPC }}</div>
#endforeach
#endif
$joinCot is your retrieved data array. foreach() return single row and key in every iteration. You have to access row->value.
That means,
#foreach ($joinCot as $key => $value)
<div>{{$value->idPC }}</div>
#endforeach

Combine multiple column for to do a foreach

I have 5 columns in my database. Here are the names of the columns:
promo1
promo2
promo3
promo4
promo5
I would like to combine these columns to be able to make a foreach in my view.
I tried this:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->where('id', $card->id)->get()->toArray();
and in my view :
#foreach($promos as $promo)
<span>{{ $promo['promo1'] }}</span>
#endforeach
But I have only one result for my foreach. Instead of 5. And I can only select one by one ($ promo ['promo1'], $ promo ['promo2']) so doing a foreach would not help
Is there a way to do that? thank you very much
When you're using get() method, you're getting a collection. Use the find() method to get an object instead of collection:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->find($card->id)->toArray();
Then you'll be able to iterate over promos:
#foreach ($promos as $promo)
<span>{{ $promo }}</span>
#endforeach
First get the building object
$building = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->findOrFail($card->id);
then loop through the promos
#foreach ($building->getAttributes() as $key => $value)
<span>{{ $key }} : {{ $value }}</span>
#endforeach
note: using the $key here is optional

Laravel 5.1 pagination count

I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.
I've found some threads on stackoverflow solving this issue for Laravel 4
Where they say, you should solve it like this
<?php $count = $players->getFrom(); ?>
#foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
#endforeach
or like this
<?php $count = $players->getFrom() + 1; ?>
#foreach ($players as $player)
...
The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.
I tried it like this
<?php $count = $pages->firstItem() + 1 ?>
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
and like this
{{ $count = $pages->firstItem() }}
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
//pages.singlebasic.blade.php
{{ $count ++ }}
but my site always displays only the number "2" instead of counting up. How do I achieve that?
Just use this to get pages count
$players->lastPage()
or this to get items count
$players->total()
If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that
<?php $count++; ?>
{{ $count }}
First of all get 100 players with paginate(100) method then you can get the index or count in the following way:
<?php $count = 1; ?>
#foreach ($players as $player)
<tr>
<td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
</tr>
<?php $count++; ?>
#endforeach
Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.
If you want to use laravel 5 loop itration
`#foreach ($players as $player)
<tr>
<td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
</tr>
#endforeach`
Hope this helps.
What i used to do in order to avoid php tags in blade templates in 4.2 was something like that
#foreach ($players as $key => $player)
<tr>
<td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
#endforeach
It's kinda strange but it will do the trick.

ElseIf statement with laravel

I have the following foreach loop that I'm getting good results with however when it gets to the elseif statement I'm attempting to see if two of the players are on the same team or not and if they are then have them listed together separated with a & symbol. Currently I am getting the following still.
Player 1 vs. Player 2 vs. Player 3 vs. Player 4
This is okay however Player 1 and Player 2 are on the same team. So its not seeing them as on the same team for some reason. Does someone see what my issue is?
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($match->players->count() - 1 != $key)
vs.
#elseif ($match->players[$key - 1]->team_id == $player->team_id)
&
#endif
#endforeach
EDIT: I changed the data a little but still should work.
http://pastebin.com/AdyzemC4
It was tricky to puzzle together your $match variable :)
#if($match->players->count() - 1 != $key)
Your $match->players->count() is always equal to the value of the number of players(say 'n'). So your 'if' ONLY checks the 'n-1' != current key ($key). [Which is true except for the last key, so you get all 'vs'].
This should work:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endforeach
In this we are checking if the current player's team is the same as the next player's team [$key+1].
Note:
You need to stop the loop for the last player, since $key+1 will go outside your array and you will get an offset error.
Therefore add another if:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($key + 1 < $match->players->count())
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endif
#endforeach

Categories