Eloquent returning JSON as variable

Asked

Viewed 44 times

0

I have mounted the following query inside my controller:

public function __construct() {

    $informations = Informations::get()->first();
    View::share( 'informations', $informations );
}

The idea is that this query is available in all my views, without having to repeat them.

This works perfectly with all pages, but with a specific view I get the following error:

Undefined variable: {"id":1,"address":"Av. Marginal Tancredo Neves","number":"300","district":"Parque Industrial Tancredo Neves","zipcode":"15076-630","city":"S\u00e3o Jos\u00e9 do Rio Preto","state":"SP","whatsapp":"(17) 99728-3179","instagram":"https:\/\/www.instagram.com\/siladecorr\/","facebook":"https:\/\/www.facebook.com\/siladecorr\/","linkedin":null,"twitter":null,"pinterest":null,"phone1":"(17) 99728-3179","email":"[email protected]","phone2":"(17) 99728-3179","texto_1":"Cras faucibus faucibus urna vel fermentum. Nullam eu tempor urna. Morbi vel magna id augue commodo venenatis ac in neque. Ut hendrerit quam quis ligula feugiat luctus. Vivamus pulvinar elit a pretium pharetra.","texto_2":"Cras faucibus faucibus urna vel fermentum. Nullam eu tempor urna. Morbi vel magna id augue commodo venenatis ac in neque. Ut hendrerit quam quis ligula feugiat luctus. Vivamus pulvinar elit a pretium pharetra.","valor_1":"Duis at augue","valor_2":"Nulla pharetra","valor_3":"Sed dictum","valor_4":"Quisque vel","valor_5":"Aliquam iaculis","valor_texto_1":"Maecenas vitae urna ut est ultrices bibendum. Integer ac imperdiet elit, a dapibus dui. Sed nec ultrices arcu. Integer non ipsum turpis. Quisque vel est ut libero scelerisque mollis eu vel mauris.","valor_texto_2":"Maecenas

This view has the $informations variable being used correctly, just like all the others. However, only this one I have this error in specific.

below follows the view in question

@extends('layouts.default')
@section('content')


<!-- Content Scroll -->
<div id="content-scroll">


    <!-- Main -->
    <div id="main">

        <!-- Hero Section -->
        <div id="hero">
            <div id="hero-styles" class="parallax-onscroll">
                <div id="hero-caption">
                    <div class="inner">
                        <div class="hero-title">CONTATO</div>
                        <div class="hero-subtitle">Envie suas críticas, sugestões e elogios para a gente</div>
                    </div>
                </div>
            </div>
        </div>
        <!--/Hero Section -->


        <!-- Main Content -->
        <div id="main-content">
            <!-- Main Page Content -->
            <div id="main-page-content">

                <!-- Row -->
                <div class="vc_row row_padding_bottom">

                    <h1 class="has-mask" data-delay="10">Vamos conversar</h1>

                    <hr>
                    <hr>

                    <div class="one_third">

                        <div class="has-animation" data-delay="100">
                            <div class="clapat-icon">
                                <i class="fa fa-paper-plane fa-2x" aria-hidden="true"></i>
                            </div>

                            <h6><a href="mailto:[email protected]"
                                    class="link"><span>[email protected]</span></a></h6>
                            <p>E-mail</p>
                        </div>

                        <div class="has-animation" data-delay="200">
                            <div class="clapat-icon">
                                <i class="fa fa-map-marker fa-2x" aria-hidden="true"></i>
                            </div>

                            <h6>{{ $informations->address }}, {{ $informations->number }}</h6>
                            <h6>{{ $informations->district }}</h6>
                            <h6>{{ $informations->city }}</h6>
                            <p>Endereço</p>
                        </div>

                        <div class="has-animation" data-delay="300">
                            <div class="clapat-icon">
                                <i class="fa fa-phone fa-2x" aria-hidden="true"></i>
                            </div>

                            <h6><a href="tel:<?= preg_replace('/[^0-9]+/','', $$informations->phone1) ?>"
                                    class="link"><span>{{ $informations->phone1 }}</span></a></h6>
                            <h6><a href="https://wa.me/<?= preg_replace('/[^0-9]+/','', $informations->whatsapp) ?>"
                                    class="link"><span>{{ $informations->whatsapp }}</span></a> - WhatsApp</h6>
                            <p>Telefone</p>
                        </div>

                    </div>


                    <div class="two_third last">

                        <!-- Contact Formular -->
                        <div id="contact-formular">

                            <div id="message"></div>

                            <form method="post" action="http://clapat.ro/themes/hervin/contact.php" name="contactform"
                                id="contactform">
                                <div class="name-box has-animation" data-delay="100">
                                    <input name="name" type="text" id="name" size="30" value=""
                                        placeholder="Seu nome"><label class="input_label"></label>
                                </div>
                                <div class="email-box has-animation" data-delay="150">
                                    <input name="email" type="text" id="email" size="30" value=""
                                        placeholder="Seu e-mail"><label class="input_label"></label>
                                </div>
                                <div class="message-box has-animation" data-delay="100">
                                    <textarea name="comments" cols="40" rows="4" id="comments"
                                        placeholder="Sua mensagem"></textarea><label class="input_label slow"></label>
                                </div>
                                <div class="button-box has-animation" data-delay="100">
                                    <div class="clapat-button-wrap parallax-wrap hide-ball">
                                        <div class="clapat-button parallax-element">
                                            <div class="button-border outline rounded parallax-element-second"><input
                                                    type="submit" class="send_message" id="submit" value="Enviar" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>

                        </div>
                        <!--/Contact Formular -->

                    </div>

                </div>
                <!--/Row -->




            </div>
            <!--/Main Page Content -->



        </div>
        <!--/Main Content -->
    </div>
    <!--/Main -->


    <!-- Footer -->
    <footer class="hidden">
        <div id="footer-container">

            <div id="backtotop" class="button-wrap left-custom">
                <div class="icon-wrap parallax-wrap">
                    <div class="button-icon parallax-element">
                        <i class="fa fa-angle-up"></i>
                    </div>
                </div>
                <div class="button-text"><span data-hover="Voltar ao topo">Voltar ao topo</span></div>
            </div>

            <div class="footer-middle">
                <div class="copyright">2019 © <a class="link" target="_blank" href="https://www.clapat.com/">Sila
                        Decor</a>.</div>
            </div>

            <div class="socials-wrap">
                <div class="socials-icon"><i class="fa fa-share-alt" aria-hidden="true"></i></div>
                <div class="socials-text">Siga a Sila Decor</div>
                <ul class="socials">
                    <li style="{{ isset($informations->facebook)!=""?'':'display:none;' }}" }}>
                        <span class="parallax-wrap">
                            <a class="parallax-element" href="{{ $informations->facebook }}" target="_blank">
                                <i class="fa fa-facebook-official" aria-hidden="true"></i>
                            </a>
                        </span>
                    </li>

                    <li style="{{ isset($informations->instagram)!=""?'':'display:none;' }}">
                        <span class="parallax-wrap">
                            <a class="parallax-element" href="{{ $informations->instagram }}" target="_blank">
                                <i class="fa fa-instagram" aria-hidden="true"></i>
                            </a>
                        </span>
                    </li>

                    <li style="{{ isset($informations->linkedin)!=""?'':'display:none;' }}">
                        <span class="parallax-wrap">
                            <a class="parallax-element" href="{{ $informations->linkedin }}" target="_blank">
                                <i class="fa fa-linkedin" aria-hidden="true"></i>
                            </a>
                        </span>
                    </li>

                    <li style="{{ isset($informations->twitter)!=""?'':'display:none;' }}"><span class="parallax-wrap">
                            <a class="parallax-element" href="{{ $informations->twitter }}" target="_blank">
                                <i class="fa fa-twitter" aria-hidden="true"></i>
                            </a></span>
                    </li>

                    <li style="{{ isset($informations->pinterest)!=""?'':'display:none;' }}">
                        <span class="parallax-wrap">
                            <a class="parallax-element" href="{{ $informations->pinterest }}" target="_blank">
                                <i class="fa fa-pinterest" aria-hidden="true"></i>
                            </a>
                        </span>
                    </li>
                </ul>
            </div>

        </div>
    </footer>
    <!--/Footer -->


</div>

@endsection

Grateful from now on!

1 answer

2


To share any information with all Views you need to use the View::share() in the method boot() of AppServiceProvider or any ServiceProvider that you have created.

#AppServiceProvider.php

/**
 * Bootstrap any application services.
 *
 * @return void
 */
 public function boot()
 {
     $informations = Informations::first();
     View::share( 'informations', $informations );
 }
  • 1

    Thanks @Virgilionovic, as I just copied and pasted I ended up not perishing the error, but already corrected. vlws

Browser other questions tagged

You are not signed in. Login or sign up in order to post.