php Larvel在程序中间包含模型

时间:2018-11-22 06:12:01

标签: laravel laravel-5 model eloquent

我想从程序中间的一个表“ modellist”中添加模型。

foreach ($trans_infos as $key => $trans_info) {
                if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2){
                    $model=TransactionModules::where('id',$info->module_id)->first();
                    $model_name=$model->model; // here am getting model name form table
                    use  App\Models\.$model_name; //i used this function to include model on middle of program  
                    $model_name::find($info->transaction_id)->update(['status' => 7]);
                }
            }

2 个答案:

答案 0 :(得分:1)

我还没有测试过,但是尝试:

foreach ($trans_infos as $key => $trans_info) {
            if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2){
                $model=TransactionModules::where('id',$info->module_id)->first();  
                ($model::class)::find($info->transaction_id)->update(['status' => 7]);
            }
        }

class方法返回带有模型名称的名称空间

我希望这行得通

答案 1 :(得分:0)

您可以直接雄辩地做到这一点。

无需先声明使用

  

记住要使用名称空间作为字符串=>“ App \ Models \\”

foreach ($trans_infos as $key => $trans_info) {
      if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2)
      {
          $model=TransactionModules::where('id',$info->module_id)->first();
          "App\Models\\".$model->model::find($info->transaction_id)
                                    ->update(['status' => 7]);
      }
 }