在Laravel中从MySQL显示数据以查看时出错

时间:2019-03-14 05:03:12

标签: php mysql laravel

我使用Laravel通过多查询将数据从Mysql显示到View。

这是我的路线:

Route::get('/procinstdetail','ProcDetailController@index');

这是我的控制器:

use App\act_hi_taskinst;
use View;

class ProcDetailController extends Controller
{
    private $act_hi_taskinst;

    public function __construct(act_hi_taskinst $act_hi_taskinst) {
        $this -> act_hi_taskinst = $act_hi_taskinst;
    }

    public function index(act_hi_taskinst $act_hi_taskinst, Request $request) 
    {
        $procinstid = $request->get('id');

        return View::make('datatrackingdetail')->with('data', $this->act_hi_taskinst->getData($procinstid));
    }
}

我的模特:

class act_hi_taskinst extends Model
{

    public function getData($procinstid) {
        $data = array();

        $data['taskData'] = DB::select("SELECT NAME_, PRIORITY_, ASSIGNEE_, DUE_DATE_, START_TIME_, END_TIME_, PROC_DEF_ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ = $procinstid");

        $data['varData'] = DB::select("SELECT NAME_, TEXT_ FROM act_hi_varinst WHERE PROC_INST_ID_ = $procinstid");

        $data['subprocessData'] = DB::select("SELECT * FROM act_hi_procinst WHERE SUPER_PROCESS_INSTANCE_ID_ = $procinstid");

        return $data;
    }
}

最终正在获取要查看的数据:

<body>      
        <h2>Task</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Priority</th>
                <th>Assignee</th>
                <th>Due Date</th>
                <th>Created</th>
                <th>Completed</th>
            </tr>

            @if(isset($data))
            @foreach($data as $processTaskDataValue)
            <tr>
                <td>{{ $processTaskDataValue -> NAME_ }}</td>
                <td>{{ $processTaskDataValue -> PRIORITY_ }}</td>
                <td>{{ $processTaskDataValue -> ASSIGNEE_ }}</td>
                <td>{{ $processTaskDataValue -> DUE_DATE_ }}</td>
                <td>{{ $processTaskDataValue -> START_TIME_ }}</td>
                <td>{{ $processTaskDataValue -> END_TIME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Variable</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>

            @if(isset($data))
            @foreach($data as $processVarDataValue)
            <tr>
                <td>{{ $processVarDataValue -> NAME_ }}</td>
                <td>{{ $processVarDataValue -> TEXT_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Sub Process</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>ID_</th>
                <th>PROC_INST_ID_</th>
                <th>BUSINESS_KEY_</th>
                <th>PROC_DEF_ID_</th>
                <th>START_TIME_</th>
                <th>END_TIME_</th>
                <th>DURATION_</th>
                <th>START_USER_ID_</th>
                <th>START_ACT_ID_</th>
                <th>END_ACT_ID_</th>
                <th>SUPER_PROCESS_INSTANCE_ID_</th>
                <th>DELETE_REASON_</th>
                <th>TENANT_ID_</th>
                <th>NAME_</th>
            </tr>

            @if(isset($data))
            @foreach($data as $subprocessDataValue)
            <tr>
                <td><a href= "{{ url('/procinstdetail') }}?id={{ $subprocessDataValue -> ID_ }}&endtime={{ $subprocessDataValue -> END_TIME_ }}"> {{$subprocessDataValue -> ID_ }}</a></td>
                <td>{{ $subprocessDataValue -> PROC_INST_ID_ }}</td>
                <td>{{ $subprocessDataValue -> BUSINESS_KEY_ }}</td>
                <td>{{ $subprocessDataValue -> PROC_DEF_ID_ }}</td>
                <td>{{ $subprocessDataValue -> START_TIME_ }}</td>
                <td>{{ $subprocessDataValue -> END_TIME_ }}</td>
                <td>{{ $subprocessDataValue -> DURATION_ }}</td>
                <td>{{ $subprocessDataValue -> START_USER_ID_ }}</td>
                <td>{{ $subprocessDataValue -> START_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> END_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> SUPER_PROCESS_INSTANCE_ID_ }}</td>
                <td>{{ $subprocessDataValue -> DELETE_REASON_ }}</td>
                <td>{{ $subprocessDataValue -> TENANT_ID_ }}</td>
                <td>{{ $subprocessDataValue -> NAME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>
    </body>

但是我收到错误消息:

  

试图获取非对象的属性“ NAME _”。

我的代码有什么问题? 我是Laravel的新手,所以我真的不知道该如何解决。

2 个答案:

答案 0 :(得分:2)

在模型中,您正在使用键taskData,varData和subprocessData创建数组,但是忘记了在获取数据时将此键用于$ data数组

    <body>      
        <h2>Task</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Priority</th>
                <th>Assignee</th>
                <th>Due Date</th>
                <th>Created</th>
                <th>Completed</th>
            </tr>

            @if(isset($data))
            @foreach($data['taskData'] as $processTaskDataValue)
            <tr>
                <td>{{ $processTaskDataValue->NAME_ }}</td>
                <td>{{ $processTaskDataValue->PRIORITY_ }}</td>
                <td>{{ $processTaskDataValue->ASSIGNEE_ }}</td>
                <td>{{ $processTaskDataValue->DUE_DATE_ }}</td>
                <td>{{ $processTaskDataValue->START_TIME_ }}</td>
                <td>{{ $processTaskDataValue->END_TIME_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Variable</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>

            @if(isset($data))
            @foreach($data['varData'] as $processVarDataValue)
            <tr>
                <td>{{ $processVarDataValue->NAME_ }}</td>
                <td>{{ $processVarDataValue->TEXT_ }}</td>
            </tr>
            @endforeach
            @endif
        </table>

        <h2>Sub Process</h2>
        <table border="1" cellspacing = "0" cellpadding = "3">
            <tr>
                <th>ID_</th>
                <th>PROC_INST_ID_</th>
                <th>BUSINESS_KEY_</th>
                <th>PROC_DEF_ID_</th>
                <th>START_TIME_</th>
                <th>END_TIME_</th>
                <th>DURATION_</th>
                <th>START_USER_ID_</th>
                <th>START_ACT_ID_</th>
                <th>END_ACT_ID_</th>
                <th>SUPER_PROCESS_INSTANCE_ID_</th>
                <th>DELETE_REASON_</th>
                <th>TENANT_ID_</th>
                <th>NAME_</th>
            </tr>

            @if(isset($data))
            @foreach($data['subprocessData'] as $subprocessDataValue)
            <tr>
                <td><a href= "{{ url('/procinstdetail') }}?id={{ 
                $subprocessDataValue -> ID_ }}&endtime={{ $subprocessDataValue -> 
                 END_TIME_ }}"> {{$subprocessDataValue -> ID_ }}</a></td>
                <td>{{ $subprocessDataValue->PROC_INST_ID_ }}</td>
                <td>{{ $subprocessDataValue->BUSINESS_KEY_ }}</td>
                <td>{{ $subprocessDataValue->PROC_DEF_ID_ }}</td>
                <td>{{ $subprocessDataValue->START_TIME_ }}</td>
                <td>{{ $subprocessDataValue->END_TIME_ }}</td>
                <td>{{ $subprocessDataValue->DURATION_ }}</td>
                <td>{{ $subprocessDataValue->START_USER_ID_ }}</td>
                <td>{{ $subprocessDataValue->START_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue->END_ACT_ID_ }}</td>
                <td>{{ $subprocessDataValue->SUPER_PROCESS_INSTANCE_ID_ }}</td>
                <td>{{ $subprocessDataValue->DELETE_REASON_ }}</td>
                <td>{{ $subprocessDataValue->TENANT_ID_ }}</td>
                <td>{{ $subprocessDataValue->NAME_ }}</td>
            </tr>
            @endforeach
            @endif
          </table>
        </body>

答案 1 :(得分:1)

DB::select()将返回一个数组。因此,将其称为属性。

小型POC

DB::select()

更新:根据其他答案,您也不会遍历$data的元素,而不是遍历从DB::select获得的实际数据。