Reputation: 588
I got an error ,When i try to use whereDate in my view page.
1/2 BadMethodCallException in Macroable.php line 74: Method whereDate does not exist.
2/2 ErrorException in Macroable.php line 74: Method whereDate does not exist. (View: C:\wamp\www\2_Work\dashboard\resources\views\pdf\user.blade.php)
My model relationship is work, But why i can't use whereDate function.
@extends('layouts.pdf')
@section('content')
<table border="1" cellspacing="0" cellpadding="0" width="545">
<tr>
<th>ชื่อผู้ใช้งาน</th>
<th>เข้าใช้ล่าสุด</th>
<th>จำนวนการเข้าใช้งานใน 7 วัน</th>
<th>จำนวนงานที่ทำในวันที่ 13/01/2560</th>
</tr>
@foreach ($users as $user)
<tr>
<td><span class="td-text">{{$user->user_detail}}</span></td>
<td align="center">{{$user->ual->first()->created_at ?? NULL}}</td>
<td align="center">{{$user->ual->where('created_at', '>=', '(CURDATE() - INTERVAL 7 DAY)')->count()}}</td>
<td align="left"><?php var_dump( $user->dataDetail->whereDate('created_at', '2017-01-13')->toArray() ); ?></td>
</tr>
@endforeach
</table>
@endsection
Controller
$data['users'] = UserModel::all();
return view('pdf.user', $data);
UserModel
class UserModel extends Model
{
public function dataDetail()
{
return $this->hasMany('App\DataDetailModel', 'user_id', 'user_id');
}
}
Upvotes: 0
Views: 11336
Reputation: 3186
You may need to use $user->dataDetail()
with parenthesis so you can uitilise Query Builder.
Edit: As you mentioned in your comment, you will also need to add get()
to access the collection.
So $user->dataDetail()->whereDate('created_at', '2017-01-13')->get()->toArray()
Upvotes: 6
Reputation: 155
I think u had missed the 3rd params of whereDate
Pls try this one
if(isset($user->dataDetail)) {
var_dump($user->dataDetail->whereDate('created_at','=' ,'2017-01-13')->get()->toArray());
}
Upvotes: 0