欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel - Model const in a blade?

Is it bad practice to use Model:CONST in a blade view or what is other approach?

For example in the model, I have like this:

class ServiceType extends Eloquent
{

    protected $table = 'service_type';

    const TYPE_LANDLINE = 1;
    const TYPE_SIP = 4;
}

and in the controller:

 if ($packageDb->service_type_id == ServiceType::TYPE_SIP) {
   $summary[service_type] = $packageDb->service_type_id;
 }

 if ($packageDb->service_type_id == ServiceType::TYPE_LANDLINE) {
   $summary[service_type] = $packageDb->service_type_id;
 }

 return View::make("order.order-billing")->with('summary', $summary);

In blade I could do something like this (not tested):

  @if ($summary['service_type'] == ServiceType::TYPE_SIP) 
    ..
  @endif

    tl;dr

    It is up to you.

    Alternate solution

    You could create a variable for your constant and pass it to the view:

    $roleNames = User::ROLE_NAMES;
    return View::make("membership.edit", compact('roleNames'));

    Then in the view:

     <td>@lang("app.{$roleNames[$member->pivot->role_id]}")</td>

    Advantages

    • Shorter: You don't have to write the fully qualified name of the model. This is pretty handy if you have a deep model structure or long model names.

    • You can "rename" the constant: Sometimes constant names are general. By passing a variable you can give a more descriptive name for the variable that can tell how exactly you use those constants in that given context.

    • It is clearer what the view works with: The controller's job is to provide the needed resources (for the view) to generate a response to the request. If you pass the constants to the view in the controller, you can see what resources the view works with.

    Disadvantages

    Of course there can be down sides too, when using this method is cumbersome. If you have many constants (for example for each user role) then probably you don't want to pass all of them to the view, because you will end up with something like this:

    $noRole = User::NO_ROLE;
    $memberRole = User::MEMBER_ROLE;
    $adminRole = User::ADMIN_ROLE;
    $moderatorRole = User::MODERATOR_ROLE;
    $reviewerRole = User::REVIEWER_ROLE;
    $publisherRole = User::PUBLISHER_ROLE;
    return View::make("membership.edit", compact(
        'noRole',
        'memberRole',
        'adminRole',
        'moderatorRole',
        'reviewerRole',
        'publisherRole'
    ));

    The main problems with this:

    • Lot of unnecessary code for a trivial functionality.

    • Hard to maintain, especially if your view uses only a few of them.

    • Violates DRY, especially if you need to do this in almost all function that returns a view.

    Of course you could refactor this, create helper functions, but why would you deal with all of this hassle when (in this case) using the constants directly in the view is straightforward and easy to understand:

    @if ($user->role === App\User::ADMIN_ROLE)

    The rule of thumb is to use the solution that is easier to read and understand. Except if you have a style guide, then you should follow that.

    In your blade file you can inject the model

    @inject('ServiceTypeModel', 'App\Models\ServiceType')

    and then use constants like this

    {{ ServiceTypeModel::SIP }}

    or

    @if ($x < ServiceTypeModel::SIP)...

    来自 https://stackoverflow.com/questions/30778727/laravel-model-const-in-a-blade


    laravel access to model constant in blade

    Need access model constant in blade not with full path

    class PaymentMethod extends Model {

    const PAYPAL_ACCOUNT      = 'paypal_account';
    const CREDIT_CARD         = 'credit_card';
    

    and in blade

            {{ App\Classes\Models\PaymentMethod::CREDIT_CARD }}
    

    work

    but

            {{ PaymentMethod::CREDIT_CARD }}
    

    throws Class 'PaymentMethod' not found


    You may use aliases:

    in your config\app.php under aliases section :

    aliases => [
         ....
        'PaymentMethod' => App\Classes\Models\PaymentMethod::class
    ]
    

    then use it in your balde file

    {{ PaymentMethod::CREDIT_CARD }}
    

      来自  https://stackoverflow.com/questions/47270905/laravel-access-to-model-constant-in-blade

      普通分类: