luismldias's avatar

Blade echoing returning 1

I am having an issue with a project.

In a nutshell, in a controller i am sharing a variable like this:

 view()->share('body_class',  'home');

and in the view when echoing the value using

{{ $body_class }}

The value that is being echoed is "1"

Has anyone had this issue?

Laravel 5.8

Thanks in advance,

Luis Dias

0 likes
21 replies
rawilk's avatar

Is there a reason you're using view()->share() in a controller? If you only need it for one endpoint, I'd just send the data through when returning the view, otherwise I would use a view composer to share the variable with specific views.

https://laravel.com/docs/5.8/views#view-composers

As far as your variable goes, try using dd($body_class) to see what the value is, as I'm guessing it's a boolean value.

1 like
Snapey's avatar

I use view()->share() in the controller quite a lot. Its useful for prepping data for dropdowns etc. It means you can prepare data in other methods (eg in traits) and not have to push everything down to the end of the controller method.

My suggestion would be to look for any way you could be overwriting the value

oh, and of course make sure it does not actually say bofy

luismldias's avatar

Hi,

That was a typo :) (bofy)

I made sure about the data i was sending. In the controller, just before returning the view using dd the values check out.

rawilk's avatar

@snapey You make a good point. I usually use view composers to accomplish things like this but it's probably just a personal preference.

1 like
mstrauss's avatar

@luismldias Can you share the view? And maybe the controller method that you are sending the data with too.

luismldias's avatar

Here is the Controller function:

public function index() {

    //get wizardries
    $wizardry = Wizardry::where('lang', Lang::getLocale())
            ->where('active', 1)
            ->inRandomOrder()
            ->limit(1)
            ->first();

    //get featured projects with category and wizardries
    $projects = Project::where('lang', Lang::getLocale())
                    ->where('active', 1)
                    ->where('featured_project', 1)
                    ->inRandomOrder()
                    ->limit(5)
                    ->with('category')
                    ->with('media')
                    ->with('wizardries')->get();

    //get clients
    $clients = Client::orderBy('order')->where('active',1)->limit(5)->inRandomOrder()->get();
    
    $this->meta['description'] = trans('meta.home.description');
    $this->meta['title'] = trans('meta.home.title') . ' - ' . env('APP_NAME');
    View::share('wizardry', $wizardry);
    View::share('projects', $projects);
    View::share('clients', $clients);
    view()->share('body_class',  'home');
    View::share('meta', $this->meta);
    return view("home");
}

In the view, a partial for the head section the code usage is as follows:

body class="{{ $body_class or '' }}"

The thing is, the issue seems to be with the {{ }} since it replaced with the following php code and the values are echoing correctly:

body class=" php echo isset($body_class) ? $body_class: '';"

(i removed the open and close markup and php tags)

So it doesn't seem to be an issue with the variable assignment but with the echoing.

Notice that all the other assignments, namely the ones resulting from Eloquent queries are working fine. The $meta array also echoes the value 1.

I managed to work around it using regular php code but it bothers me not understanding why something doesn't work.

Snapey's avatar

or is deprecated. I wonder if its that?

try null coalesce operator

<body class="{{ $body_class ?? '' }}">
luismldias's avatar

Don't think that applies since the value of the variable is always set, the "Or" or the ternary operator is in place just because it is a practice we have.

mstrauss's avatar

Never mind, I see the value in the previous post

What is the value of $body_class before it is sent to the view? Is it just a string like 'home'?

luismldias's avatar

Yes, and it is assigned in the controller:

view()->share('body_class', 'home');

Snapey's avatar

just because it is a practice we have.

I assume then you are running on something like 5.5 since this is now deprecated?

Is there anything in the fact that you use two different methods to do the same thing?

    View::share('clients', $clients);
    view()->share('body_class',  'home');
luismldias's avatar

Hi Snapey,

I'm running 5.8, what is deprecated? The View facade?

The different methods was just to try different ways of doing the same thing to see if the outcome was different.

luismldias's avatar

Thanks I was unaware of that deprecation (that's what I get for not reading the docs in full).

However, deprecation doesn't mean it doesn't work, just a heads up not to use it (which I won't anymore) but would that cause the echoing of the value "1"?

Snapey's avatar
Snapey
Best Answer
Level 122

You were right about not reading the documentation

The or Operator Likelihood Of Impact: High

The Blade "or" operator has been removed in favor of PHP's built-in ?? "null coalesce" operator, which has the same purpose and functionality:

Snapey's avatar

Your or is being treated as a logical operator. Just change to ?? as suggested (much) earlier

luismldias's avatar

Hi,

Sorry, was out all day in a client, will give it a go tomorrow.

luismldias's avatar

Hi Snapey,

That did the trick. Changing to the "??" operator worked.

Thanks

luismldias's avatar

This is my first time using this tool. I can´t find the place to mark the issues as solved...

Please or to participate in this conversation.