Home > Enterprise >  implode not working in laravel o blade view
implode not working in laravel o blade view

Time:06-23

my blade

 <td>   @foreach($track->singers_id as $singers_id)
                    @php
                    $id=$singers_id;
    $navLinks =DB::table('singers')->select('singers_name')->where('id', '=', $id)->get();
@endphp
{{ implode(' & ', array_column($navLinks->toArray(), 'singers_name')) }}
    
                    @endforeach
                    </td>

i want to show every data after & symbol but here(one two) show but i want to (one & two)

attached my databbse pic

https://i.stack.imgur.com/DcabN.png

CodePudding user response:

if your $track->singers_id is an array of ids, you dont need to loop them to get the result you want.

<td>
    @php
        $names = DB::table('singers')->whereIn('id', $track->singers_id)->pluck('singers_name')->toArray();
    @endphp
    {{implode(' & ', $names)}}
</td>

But it would be better to eager load the singers relation beforehand in your controller using relations like @ac.0101010111 suggested

//Controller
$tracks = Track::query()
    //your other conditions
    ->with('singers')
    ->get(); // or ->paginate() just use what you already used.

//blade
<td>{{implode(' & ', $track->singers->pluck('singers_name')->toArray())}}</td>

CodePudding user response:

It's not acceptable to perform database queries inside blade as Laravel follows the MVC (Model-View-Controller) concept. All database actions must be performed in the controller. What do you mean by "implode not working"? What's "not working"? What output do you get from dd($navLinks)? Share some more information...

  • Related