Reputation: 449
I have to convert these queries to Laravel way.
$tqry = "select app_process,end_time,((end_time-".time().")/86400)as remain_day from BYAPPS_apps_data where mem_id='$valu[mem_id]' limit 1";
$tresult = $_DB->query($tqry);
$tmp=$tresult->fetchRow();
if($tmp[app_process]){
$status=$app_process[$tmp[app_process]];
if($tmp[app_process]==7&&($tmp[end_time]&&$tmp[remain_day]<=0)) $type="Expired";
}else {
$tqry = "select app_process from BYAPPS_apps_order_data where mem_id='$valu[mem_id]' order by idx desc limit 1";
$tresult = $_DB->query($tqry);
$tmp=$tresult->fetchRow();
if($tmp[app_process]) $status=$ord_process[$tmp[app_process]];
else $status="Not Customer";
}
I set the relation through the models like this.
UserInfo model
public function order()
{
return $this->hasMany('App\AppsOrderData', 'mem_id');
}
public function apps()
{
return $this->hasMany('App\AppsData', 'mem_id');
}
AppsOrderData model (=> Using 'BYAPPS_apps_order_data' table)
public function userinfo()
{
return $this->belongsTo('App\UserInfo', 'mem_id');
}
AppsData model (=> Using 'BYAPPS_apps_data' table)
public function payments()
{
return $this->hasMany('App\AppsPaymentData','mem_id');
}
Then, I tried to convert the original query in UserInfoController.php
like below.
public function getUserInfoListData()
{
$userInfoListData = UserInfo::select('idx',
'mem_id',
'mem_nick',
'mem_name',
'cellno',
'ip',
'reg_date')
->with('order')
->with('apps');
$orderProcess = array('Cancel','Order', 'Confirm Order',
'Developing', 'App Uploaded', 'Service end',
'Service expired', '', '', 'Waiting');
$appsOrderData = AppsOrderData::select('app_process', 'mem_id', 'end_time');
return Datatables::of($userInfoListData)
->setRowId(function($userInfoListData) {
return $userInfoListData->idx;
})
->editColumn('type', function($eloquent) use ($appsOrderData, $orderProcess) {
if ($appsOrderData->app_process == 7 && $appsOrderData->end_time <= 0) {
return "Expired";
} else {
return "No Customer";
}
})
->editColumn('term', function($eloquent) {
return " (".$eloquent->launch_date." ~ ".now().")";
})
->orderColumn('reg_date', 'reg_date $1')
->make(true);
}
However, it doesn't return as I expected, all type
data return just 'No Customer'.
How can I convert this query as laravel way?
Did I wrong make the relation?
Upvotes: 1
Views: 75
Reputation: 76
I think there are some mistakes that you have done in your code,
AppData model doesn't have a user Relationship.
If you get()
method to get data , You can do it in this way,
$userInfoListData= UserInfo ::
select('idx','mem_id','mem_nick','mem_name','cellno','ip','reg_date')
->with('order')
->with('apps')
->get();
For further reference you can use this: Get Specific Columns Using “With()” Function in Laravel Eloquent
I hope this will help you to solve your issue- thanks!
Upvotes: 1