Accesing variable value from Model and showing it in blade

Published 1 week ago by Gabotronix

Hi, for my admin panel project I created a table called globals, with fields name and value.

This is the migration of globals table:

//CREATE GLOBALS TABLE
        Schema::create('globals', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('value');
            $table->timestamps();
        });
        //FILL GLOBALS TABLE WITH DUMMY POSTS
        DB::table('globals')->insert([
            'name' => 'Nombre del restaurante',
            'value' => config('globals.web_name'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Descripcion en google del restaurante',
            'value' => config('globals.web_description'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Biografia del restaurante',
            'value' => config('globals.restaurant_bio'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Télefono de reserva',
            'value' => config('globals.booking_phone'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Email de contacto',
            'value' => config('globals.client_mail'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Dirección del restaurante',
            'value' => config('globals.full_address'),
        ]);
        DB::table('globals')->insert([
            'name' => 'Código postal y ciudad',
            'value' => config('globals.city_zip_address'),
        ]);

I want to have access to my variables in my blade templates, but I don't know how to access the value field referencing the name field, something like $globals->name->value??

This is how I retrieve globals and post them into the view:

public function inicio()
    {
        
        $pageInfo = 
        [
            'page_title'      => 'Inicio',
            'menu_active'     => 'Inicio',
            'submenu_active' => '',
        ];
        
        $globals = Globals::all();
        $sliders = Slider::all();

        
        return view('inicio.index', compact('pageInfo', $globals, $sliders));
    }

I know about the {{ }} echo thing

Please give me some advice on this

Best Answer (As Selected By Gabotronix)
Cronix
@foreach ($globals as $global)
    <div>Name: {{ $global->name }} = {{ $global->value }}</div>
@endforeach 

If you want to get a particular "name" field, like "Nombre del restaurante", then you'd use the where method on the collection.

$number = $globals->where('name', 'Nombre del restaurante')->first();
{{ $number['value'] }}

Note those are collection methods, even though it looks like a db query.

https://laravel.com/docs/5.6/collections#method-where

Cronix
Cronix
1 week ago (566,300 XP)

I don't see the point. You already have most of the data in some "global" config file (that you're reading from to insert in the db). Why not just use that to begin with instead of read from the config, store in the database, retrieve from the database? This doesn't make sense to do and wastes a query to the db when it's not necessary.

Gabotronix

It's beacuse I persorm CRUD operations on the global variables, so the value changes, so if I change the "Name of web" value the results will update on all my templates

Cronix
Cronix
1 week ago (566,300 XP)
@foreach ($globals as $global)
    <div>Name: {{ $global->name }} = {{ $global->value }}</div>
@endforeach 

If you want to get a particular "name" field, like "Nombre del restaurante", then you'd use the where method on the collection.

$number = $globals->where('name', 'Nombre del restaurante')->first();
{{ $number['value'] }}

Note those are collection methods, even though it looks like a db query.

https://laravel.com/docs/5.6/collections#method-where

Cronix
Cronix
1 week ago (566,300 XP)

Why use config at all though? If you update the config, then you'd also have to update the database. Why not just use the db for it all?

Gabotronix

I have many more variables in my globals page, not all of them are CRUDeable .

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