Reputation: 252
I am new to Laravel & working on my first project. A small issue I am having is when I build a collection (array) and insert results into Laravel Form Select, and export to Excel, my results are wrapped in ["array"] brackets and quotes.
Code to build the collection:
$p3 = DB::table('players')
->where ('id', $player3)
->selectRaw('id, CONCAT(fname," ",lname) as full_name')
->pluck('full_name', 'id');
$played['p'] .= $p3;
$p4 = DB::table('players')
->where ('id', $player4)
->selectRaw('id, CONCAT(fname," ",lname) as full_name')
->pluck('full_name', 'id');
Note, there are 16 above (grabbing names from associated table)... I then build the collection:
$played = collect([$p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9, $p10, $p11, $p12, $p13, $p14, $p15, $p16]);
and pass to view:
return view('grades.matchcard.edit', compact('data','data1', 'played', 'mid'));
As per image, the results are wrapped in brackets/quotes... Same when I export to Excel... I tried join & implode (get errors). Tried using
str_replace (kind of worked - but not great...
Any sugggestions would be greatly appreciated. thanks
Upvotes: 0
Views: 7324
Reputation: 14510
Each of your DB queries generates a collection of one element, and then you collect those collections into your final $played
variable. So iterating over it, each item is actually another collection. You want to flatten
the collections, like so:
$played = collect([$p1, $p2, ...])->flatten();
BTW, I don't see the rest of your code but the chunk you've shown is quite inefficient, and might be better written:
$played = DB::table('players')
->whereIn('id', [$player3, $player4, $player5 .... all your ids ...])
->selectRaw('id, CONCAT(fname," ",lname) as full_name')
->pluck('full_name', 'id');
Here you are doing just one DB query, instead of many, and the query returns your collection, no need for further collecting and flattening. More info in the docs.
UPDATE to clarify based on comments:
The pluck()
method above keys the resulting collection by the second parameter. So $played
should be a collection of correctly matched id => full_name
elements. To display the player with id 3's full name, you should be able to use $played[3]
.
BTW: do you have a Player
model? If not, to do things the neat Laravel way you probably should, and it would make things a lot simpler. For eg:
In your model
Define an accessor, to make getting full name easier.
public function getFullNameAttribute($value) {
return $value->fname . ' ' . $value->lname;
}
More info about accessors in the docs.
In your controller
$players = App\Player::find([$player3, $player4, $player5 .... all your ids ...]);
return view('whatever.your.view.is', ['players' => $players]);
More info about retrieving models in the docs.
In your view
<select>
@foreach ($players as $player)
<option value='{{ $player->id }}'>{{ $player->full_name }}</option>
</select>
@endforeach
If you want players to be listed in a particular order, you should either define that order in your model/database (eg a players.ranking field?), or, if the order is based on something derivable you could order it on the fly. Eg if you need them alphabetical you can add ->orderBy('lname')
to the $players
query in your controller.
Upvotes: 2
Reputation: 1225
try
$p4 = DB::table('players')
->where ('id', $player4)
->selectRaw('id, CONCAT(fname," ",lname) as full_name')
->get();
And in blade
{{$p4->id}}
{{$p4->full_name}}
EDIT
Sorry i didnt see the hole question
you can do something like this
$p4 = DB::table('players')
->where ('id', $player4)
->selectRaw('id, CONCAT(fname," ",lname) as full_name')
->get();
$played = new Collection();
$played.push($p4);
And in blade this should work
@foreach($played as $object)
{{$object->id}}
{{$object->full_name}}
@endforeach
Upvotes: 0