duanmao1975 2017-08-12 10:21
浏览 63
已采纳

Laravel碳无法解析时间串

I need to select users with specific age from model User, but have this error:

DateTime::__construct(): Failed to parse time string (birth) at position 0 (b): The timezone could not be found in the database 

part of controller:

$users = User::where(Carbon::parse('birth')->diff(Carbon::now())->format('%y'), '>=', 18)->get();

when i use it in view all working fine:

 @foreach ($userss as $user)

 <?php  $age_year = Carbon::parse($user->birth)->diff(Carbon::now())->format('%y'); ?>


@endforeach

Thanks for answers!

  • 写回答

3条回答 默认 最新

  • doubi9999 2017-08-12 11:52
    关注

    In your controller example, you are trying to parse the string 'birth' instead of the database field 'birth'.

    Since you are returning a collection, just run through the returned collections with the filter() method before sending it down to your view. You can also use the age property instead of doing a diff

    $users = User::get()->filter(function($user, $key) {
        return (Carbon::parse($user->birth)->age >= 18);
    });
    

    I did not test this, so you may get some typos, but this is how you will want to accomplish this task.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥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