1

I stuck at getting value from app\Helpers\Enums\DriverType.php.

Here's my code:

<?php

namespace App\Helpers\Enums;

final class Gender {
const MALE = 0;
const FEMALE = 1;
public static function getList() {
    return [
        Gender::MALE,
        Gender::FEMALE,
    ];
}

public static function getArray() {
    $result = [];
    foreach (self::getList() as $arr) {
        $result[$arr] = self::getString($arr);
    }
    return $result;
}

public static function getString($val) {
    switch ($val) {
        case 0:
            return "Male";
        case 1:
            return "Female";
    }
}

}

?>

my question is how can I get the value from the array in my driver\create.blade. I have tried.

here's my blade code:

{!! Form::model($obj, 
[ 'url' => $route,
'method' => $method,
'id' => 'driverCreate',
]) !!} 
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Gender:</strong>
{!! Form::select('gender', array('Male', 'Female'), null, array('class' =>     'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary" >Submit</button>
{!! Form::close() !!}
</div>

any code needed to figure out how this works?

Andrew Vanusi
  • 499
  • 4
  • 11
  • 27

1 Answers1

0

There are multiple options to share variables to view files.

First One:
From Controller you can pass variable

$genders = App\Helpers\Enums\Gender::getArray();
return view('view_name', compact($genders));

In you blade file just use $genders

{!! Form::select('gender', $genders, null, array('class' => 'form-control')) !!}

Second Call the class directly like

{!! Form::select('gender',App\Helpers\Enums\Gender::getArray(); , null, array('class' => 'form-control')) !!}

Third and probably most useful
You can share gender with all the views as you may require them later. In you providers file( AppServiceProvider is recommended) add these lines

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
    {
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $genders = App\Helpers\Enums\Gender::getArray();
        View::share('genders', $genders);
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

$gender variable will be available in all your views.

Fourth View Composer Laravel recommends this procedure, i have not used it, you can read about it Here

In the end you will get select value as 0 or 1 which are your options, you can get enum value by

 $gender = Input::get('gender');
 $genderEnum = App\Helpers\Enums\Gender::getArray()[$gender];
 // or write another method for.
Community
  • 1
  • 1
anwerj
  • 2,386
  • 2
  • 17
  • 36
  • thank you so much anwer, but why i cant use $genders , it said undi variable. but it works using call class directly. – Andrew Vanusi Sep 23 '16 at 08:47
  • These are function based inclusion of files, they are designed this way. You have to declare variables to make them accessible in views. – anwerj Sep 23 '16 at 08:49
  • @why when i press submit, show nothing. i have tried dd($request); inside the function save, it show all the data. but cant submit it to database. – Andrew Vanusi Sep 23 '16 at 09:23
  • To save data to database you need Models, have you done that? – anwerj Sep 23 '16 at 09:34
  • Add your controller code, Laravel uses Eloquent ORM, you can read about all database related actions in documentation https://laravel.com/docs/5.3/eloquent – anwerj Sep 23 '16 at 09:40
  • thanks, i can save to database now, but my datatable keep processing and dont load my data, try to console it. it say Cannot read property 'style' of undefined. alot of eror hahahah sorry to bother u – Andrew Vanusi Sep 23 '16 at 10:11
  • Break down your problem and search for each error one by one. You may need to start from some basic tutorials. All developers go though same phase when starting something new. – anwerj Sep 23 '16 at 10:23
  • @anwerjunaid second option was good one. But the code can be lengthy because of namespace, you can reduce it by putting the gender class in aliases at config/app.php. By the way the you can call all the static functions of the class from any of your blade templates – Dinoop Oct 07 '16 at 10:36