我需要一个数据库查询构建器转换成雄辩模型。我使用该插件https://github.com/Kyslik/column-sortable,它需要一个雄辩的模型。
<?php
$payments = DB::table('users')
->select('orders.id', 'users.id', 'users.name', 'orders.amount', 'orders.created_at', 'orders.updated_at')
->join('patients', 'users.id', '=', 'patients.user_id')
->join('orders', 'patients.id', '=', 'orders.patient_id')
->where('users.name', 'LIKE', '%' . $q . '%')
->orWhere('users.email', 'LIKE', '%' . $q . '%')
->get();
打电话给你的模型控制器方法内。
$companyObj = new modelname();
$userDetail = $companyObj->model_method_name($request);
尝试声明在各自的模型雄辩的关系下面给出:
class Users extends Model {
public function patients(){
return this->hasOne('App\Patients', 'user_id', 'id')->with('orders');
}
}
class Patients extends Model {
public function orders() {
return this->hasMany('App\Orders', 'patient_id', 'id')->select('id', 'amount', 'created_at', 'updated_at');
}
}
现在叫雄辩查询这些关系中的用户资料库/控制器:
$payments = $this->getModel()->select('id', 'name')->with('patients')->where('name','LIKE','%'.$q.'%')->orWhere('email','LIKE','%'.$q.'%')->get();