Accesing variable value from Model and showing it in blade

Posted 9 months 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

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

Reply to

Use Markdown with GitHub-flavored code blocks.