dpw30157 2018-03-03 18:06
浏览 42

Laravel检查用户的记录

i have this controller created. This is where i get all the ads (avisos) created from the users.

class AdController extends Controller
  {
  public function index(Request $request)
  {

    $page = $request->get('page',1);
    $perPage = 15;
    $offset = ($page * $perPage) - $perPage;


    $avisos = Ad::select(['id','user_id','title','content','created_at','moderated_at'])
            ->WithNotModerate()
            ->orderBy('moderated_at', 'asc')
            ->paginate(15);



    if($request->has('user_id'))
    $avisos = Ad::select(['id','user_id','title','content','created_at','moderated_at'])
                ->WithNotModerate()
                ->where('user_id','like','%'.$request->user_id.'%')
                ->orderBy('moderated_at', 'asc')
                ->paginate(15);

    if ($request->ajax()) {
        return view('admin.avisos.data', ['avisos' => $avisos])->render();  
    }

    return view('admin.avisos.index', compact('avisos'));
}

And here is where they are all rendered in my blade view, all ads from all users.

@foreach ($avisos as $aviso)
    <tr>
    <td>{{$aviso->id}}</td>
    <td>{{$aviso->user_id}}</td>
    <td>{{str_limit($aviso->title, 40)}}</div></td>
    <td>{{str_limit($aviso->content, 40)}}</div></td>
    <td>{{$aviso->created_at->diffForHumans()}}</td>
    <td>{{$aviso->moderated_at}}</td>
    </tr>
@endforeach

I want to check if the user has more than one ad 'created_at' in the past 15 days so i can add a class to the table row

I tried something like this

  @foreach ($avisos as $aviso)
    <?php
    $date = \Carbon\Carbon::today()->subDays(15);
    $already = DB::table('ads')
          ->where('user_id','=','$aviso->user_id')
          ->where('created_at' ,'=', date($date))
          ->get();
    var_dump($already); 
    ?>
    <tr>
    <td>{{$aviso->id}}</td>
    <td>{{$aviso->user_id}}</td>
    <td>{{str_limit($aviso->title, 40)}}</div></td>
    <td>{{str_limit($aviso->content, 40)}}</div></td>
    <td>{{$aviso->created_at->diffForHumans()}}</td>
    <td>{{$aviso->moderated_at}}</td>
    </tr>
  @endforeach

Returns

object(Illuminate\Database\Eloquent\Collection)#3073 (1) { ["items":protected]=> array(0) { } } for each ad...
  • 写回答

1条回答 默认 最新

  • duanditang2916 2018-03-03 18:20
    关注

    You are using the date function incorrectly. First argument of date excepts a valid format in form of a string, what your giving is a Carbon instance.

    But because you already have an instance of Carbon, you don't need to use date. Format the Carbon instance instead to match what you have in the database.

    You are also checking user_id against the string $aviso->user_id instead of the actual variable value.

    There is a difference between '$aviso->user_id' and $aviso->user_id. Remove the single quotes to get the expected value.

    $date = \Carbon\Carbon::today()->subDays(15);
    $already = DB::table('ads')
          ->where('user_id','=',$aviso->user_id)
          ->where('created_at' ,'=', $date->format('Y-m-d H:i:s'))
          ->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)