samedi 3 mars 2018

Laravel checking records from user

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></td>
    <td></td>
    <td></div></td>
    <td></div></td>
    <td></td>
    <td></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></td>
    <td></td>
    <td></div></td>
    <td></div></td>
    <td></td>
    <td></td>
    </tr>
  @endforeach

Returns

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire