ds78662302 2015-02-16 18:44
浏览 37

Laravel雄辩地分为两列

I'm pretty new to eloquent and laravel so I am having trouble pulling some information from the database. For starters i have an items table that looks like this

Id type quantity

1 fancy 1 2 prof 5 3 fun 6

I am trying to write something like this:

if fancy equals 0 do not display the button

I am having trouble figuring out how to access the two columns simultaneously so the code knows which type to look at it's quantity.

  • 写回答

2条回答 默认 最新

  • douyu4535 2015-02-16 18:54
    关注

    I'm assuming every type exists only once (otherwise your question doesn't make much sense to me). I'm also assuming you have a model called Type.

    You can retrieve the model instance like this:

    $fancy = Type::where('type', 'fancy')->first();
    

    And then get the quantity like this:

    if($fancy->quantity > 0){
        // display button
    }
    

    Or a bit shorter, use pluck to get a specific value from the first row:

    $fancyQuantity = Type::where('type', 'fancy')->pluck('quantity');
    

    If you need to do this check for multiple of your types I recommend you use lists to create an array that holds all types and all their quantities:

    $quantities = Type::lists('quantity', 'type');
    
    
    if(array_get($quantities, 'fancy', 0) > 0){
        // display fancy button
    }
    if(array_get($quantities, 'fun', 0) > 0){
        // display fun button
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line