Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 1 year ago.
0

INspired by http://stackoverflow.com/questions/2350052/how-can-i-get-enum-... I have added the following to my Users model

public static function getPossibleRoles()
{
  $type = DB::select( DB::raw("SHOW COLUMNS FROM v2users WHERE Field = 'role'") )[0]->Type;
  preg_match('/^enum\((.*)\)$/', $type, $matches);
  $enum = array();
  foreach( explode(',', $matches[1]) as $value )
  {
    $v = trim( $value, "'" );
    $enum = array_add($enum, $v, $v);
  }
  return $enum;
}

the thing is, I have quite a few places where I need the same logic,

I am thiking I should create a function like this

public static function getEnumValues($table, $column)
{
  $type = DB::select( DB::raw("SHOW COLUMNS FROM $table WHERE Field = '$column'") )[0]->Type;
  preg_match('/^enum\((.*)\)$/', $type, $matches);
  $enum = array();
  foreach( explode(',', $matches[1]) as $value )
  {
    $v = trim( $value, "'" );
    $enum = array_add($enum, $v, $v);
  }
  return $enum;
}

but I am a bit puzzledabout where I should place this... If I would like to make this a global function... where should it go? I would prefer to get make it could extend a Eloquint model (in that way I don't need the tableName as a parameter)

Last updated 1 year ago.
0

Lets say I have the following method from the controller :

public function index()
{
    $campaign= DB::table('campaigns')->get();
    return view('management.campaign.index', compact('campaign'));
}

and in the view make use of if/else statements since its going to solve the matter for you.

                                                                                                                                                  @foreach($campaign as $campaigns)
            <div class="row">
                <tr>

                    <td >{{ $campaigns->name }}</td>
                    <td >{{ $campaigns->duration }}</td>
                    @if($campaigns->status[0] === 'p')
                        <td >Pending</td>

                    @elseif($campaigns->status[0] === 'o')
                        <td>Ongoing</td>
                    @else
                        <td>Done</td>
                    @endif
                    <td><a class="btn btn-small btn-info " href="{{ URL::to('management/whatshot/crud/' . $campaigns->id . '/edit') }}">Edit</a></td>
                    {{--<td><a class="btn btn-small btn-success" href="{{ URL::to('management/redemption/crud/' . $redeems->id) }}">Show this record</a></td>--}}

                </tr>
            </div>
        @endforeach
0

If you need that logic in other places your best bet is to use Traits.

Make an Enumerable trait, slap the logic there and apply it to any model you need it!

buildcomplete said:

INspired by http://stackoverflow.com/questions/2350052/how-can-i-get-enum-... I have added the following to my Users model

public static function getPossibleRoles()
{
 $type = DB::select( DB::raw("SHOW COLUMNS FROM v2users WHERE Field = 'role'") )[0]->Type;
 preg_match('/^enum\((.*)\)$/', $type, $matches);
 $enum = array();
 foreach( explode(',', $matches[1]) as $value )
 {
   $v = trim( $value, "'" );
   $enum = array_add($enum, $v, $v);
 }
 return $enum;
}

the thing is, I have quite a few places where I need the same logic,

I am thiking I should create a function like this

public static function getEnumValues($table, $column)
{
 $type = DB::select( DB::raw("SHOW COLUMNS FROM $table WHERE Field = '$column'") )[0]->Type;
 preg_match('/^enum\((.*)\)$/', $type, $matches);
 $enum = array();
 foreach( explode(',', $matches[1]) as $value )
 {
   $v = trim( $value, "'" );
   $enum = array_add($enum, $v, $v);
 }
 return $enum;
}

but I am a bit puzzledabout where I should place this... If I would like to make this a global function... where should it go? I would prefer to get make it could extend a Eloquint model (in that way I don't need the tableName as a parameter)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.