Save value of a variable in Laravel

Asked

Viewed 631 times

1

I’m starting a project in Laravel, and at the sidebar I have a dropdow with a list on which I access the database and bring information from 3 companies for example, and when I click on one of these companies, I would like to save the id value so that I can use this value to insert into the bank along with an Insert or update in another register, as I could do this?

Controller Method:

public static function emitentes(){
    $users = User::where('id', 1)->with('emitentes')->get()->first();
    $emp_user = $users->emitentes;
    return $emp_user;
}

PHP block requesting information:

             @php

                use App\Http\Controllers\EmitenteController;
                use App\Models\Cliente;

                $empresas = EmitenteController::emitentes();

            @endphp

Dropdow code:

<li class="dropdown notifications-menu">

                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    <i class="fa fa-university"></i>
                    <span class="label label-warning">{{count($empresas)}}</span>
                </a>

                <ul class="dropdown-menu">

                    <li class="header">Você pode emitir recibo para {{count($empresas)}} empresas</li>

                    <li>

                        <ul class="menu">

                            @foreach($empresas as $e)
                                <li><!-- start notification -->
                                    <a href="#">
                                    <i class="fa fa-industry text-aqua"></i> {{$e->name}}
                                </a>
                            </li><!-- end notification -->
                            @endforeach

                        </ul>

                    </li>

                </ul>

            </li>

Sample image:

inserir a descrição da imagem aqui

  • Can you give more details?! I could not understand very well. What you have already done?

  • See if it helped? i would like to store this value of the company id when I click on one of the list items... for me to use later, if possible put an icon with different color in which I select..

  • Only PHP even, no Javascript?!

  • But you say "use later" means even after browsing other parts of the site/system?

  • I would like it to be in php but I think javascript would also give, I think the important thing is that I can use id!

  • Yes, would it be possible? I can’t use session variable... I don’t know how the Laravel behaves

  • You can save the id to cookies. You have tried this?

  • I didn’t try! how could I do?

Show 3 more comments

1 answer

1


With PHP, an option would be to save the id us cookies using the function setcookie. Example:

setcookie("empresa_id", '15');

To redeem the value of cookie who is saved, just use $_COOKIE. Behold:

echo $_COOKIE["empresa_id"];

Laravel itself has some resources also for this same property. See here in the documentation about Requests & Input.

With Javavascript, it is also possible to do with cookies, but if you prefer you can use localStorage. Take an example:

// Atribui um valor a empresa_id em relação a empresa com valor 15
window.localStorage.setItem('empresa_id', '15');

// Resgata o empresa_id 
var usuario = window.localStorage.getItem('empresa_id');

// Remove o empresa_id 
window.localStorage.removeItem('empresa_id');
  • In Javascript it worked!! thanks..

  • @Ulissesgimenes with cookies you tried?! or discarded?!

  • Beauty! Need, we are here! o/

  • With Cokies I could not, worked only with javascript

  • I’m not even getting to be a part of what I wanted, if you can help me.... https://answall.com/questions/229736/trecho-de-javascrip-n%C3%A3o-funciona

Browser other questions tagged

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