String value showing with [" "] characters

Published 4 weeks ago by Gabotronix

So I just noticed this bug, when building the subject for my mailable the string is showing like this :

Nuevo reporte de bug en ["Nombre del Restaurante"]

Any idea why is it showing the [""] characters around the concatenated variable??

This is the method controller where I build the string:

public function contactar_reportar_bug(BugReport $request)
    {
        
        $restaurant = Globals::where('name' , 'Nombre del restaurante')->pluck('value');
        
        $subject = 'Nuevo reporte de bug en '.$restaurant;
        
        
        
        Mail::to($destination_mail)->send(new BugReportMail($body, $uploadFile, $filename, $mimetype, $subject, $restaurant, $client_phone, $sender_mail));
        
        return response()->json([
        
            'success' => 'Reporte de bug enviado',
            'bug' => $bug,

        ]);
    }

Best Answer (As Selected By Gabotronix)
Cronix

$restaurant is an array of the plucked values. So you can't just access $restaurant.

So, you need to access the first element of the array.

$restaurant = Globals::where('name' , 'Nombre del restaurante')->pluck('value')->first();

Then

$subject = 'Nuevo reporte de bug en '.$restaurant;

will become

Nuevo reporte de bug en Nombre del Restaurante
Cronix
Cronix
4 weeks ago (647,500 XP)

$restaurant is an array of the plucked values. So you can't just access $restaurant.

So, you need to access the first element of the array.

$restaurant = Globals::where('name' , 'Nombre del restaurante')->pluck('value')->first();

Then

$subject = 'Nuevo reporte de bug en '.$restaurant;

will become

Nuevo reporte de bug en Nombre del Restaurante
Gabotronix

I understand, thanks!

Snapey
Snapey
4 weeks ago (959,275 XP)

Or use value() (the eloquent function, not your column name)

$restaurant = Globals::where('name' , 'Nombre del restaurante')->value('value');
Gabotronix

Very nice, thanks

webtrickshome

You need to replace pluck('value') with first() and that'll give you a single data. Here's an excerpt from the site webtrickshome from the page laravel query builder tutorial.

RETRIEVE SINGLE ROW / COLUMN FROM A TABLE The first() method will return a single StdClass object.

$user = DB::table('users')->where('name', '=', 'John')->first();
echo $user->name;

The value() method will return the value of the column directly.

$email = DB::table('users')->where('name', '=', 'John')->value('email');
Snapey
Snapey
4 weeks ago (959,275 XP)

@webtrickshome thanks for plugging your site... The problem's been resolved already

Please sign in or create an account to participate in this conversation.