我将数组传递给视图时出现错误“试图获取非对象的属性”

时间:2019-04-17 06:57:39

标签: laravel laravel-5

当我将数组传递给视图时,出现错误“试图获取非对象的属性”

这是我的职责

 public function get_details() {

    $courses = Course::get();
    $prospects = Course_prospect::get();


    $details_prospect = [];

    foreach ($prospects as $prospect) {

        $users = $prospect['course_id'];
        $course_pros = explode(',', $users);
        $details_course = [];


        foreach ($course_pros as $course_pro) {
            $course = Course::where('course_id', $course_pro)->select('course_name')->get();

            $details_course[] = $course;
        }

        $user = $prospect['prospect_id'];
        $selected_prospect = Prospect::where('id', $user)->get();

        $details_courses = implode(',', $details_course);
        $details_prospect[] = $selected_prospect . $details_courses . '<br>';
    }


    return view('prospect', ['courses' => $courses], ['details_prospects' => $details_prospect]);
}

这是我的观点

<div class="container">
                    <table class="table table-bordered">
                        <thead>
                            <tr>                            
                                <th>ID</th>
                                <th>Prospect Name</th>
                                <th>NIC Number</th>
                                <th>Contact Number</th>
                                <th>Contact Address</th>
                                <th>Course(s) Intrested</th>
                                <th>Comments</th>
                                <th>Referred Through</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($details_prospects as $details_prospect)
                            <tr>
                                <td>{{$details_prospect->id}}</td>
                                <td>{{$details_prospect->prospect_name}}</td>
                                <td>{{$details_prospect->prospect_nic}}</td>
                                <td>{{$details_prospect->contact_number}}</td>
                                <td>{{$details_prospect->address}}</td>
                                <td>{{$details_prospect->course_intrested}}</td>
                                <td>{{$details_prospect->comments}}</td>
                                <td>{{$details_prospect->referred_through}}</td>                                   
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

我遇到错误 试图获取非对象的属性(视图:C:\ xampp \ htdocs \ laravel \ customer_inquiry_model \ resources \ views \ prospect.blade.php)

但是当我在函数中回显它时,它就起作用了

 public function get_details() {
    $prospectss = Prospect::get();
    $courses = Course::get();
    $prospects = Course_prospect::get();


    $details_prospect = [];    

    foreach ($prospects as $prospect) {
        $users = $prospect['course_id'];
        $course_pros = explode(',', $users);
        $details_course = [];
        foreach ($course_pros as $course_pro) {
            $course = Course::where('course_id', $course_pro)->select('course_name')->get();

            $details_course[] = $course;
        }

        $user = $prospect['prospect_id'];
        $selected_prospect = Prospect::where('id', $user)->get();

        $details_courses = implode(',', $details_course);
        $details_prospect[] = $selected_prospect .  $details_courses .'<br>';
    }
    foreach ($details_prospect as $details_prospec) {
        echo $details_prospec;
    }
    die;

    return view('prospect', ['courses' => $courses],['details_prospects' => $details_prospect]);
}

当我通过查看时不起作用

2 个答案:

答案 0 :(得分:0)

更改此行:

Player

进入:

SELECT pi.user_id, 
       Sum(total_amount) AS total_amount, 
       emp_id, 

       max_pi_pending_amount, 
       first_pending_pi_warning_percentage, 
       second_pending_pi_warning_percentage, 
       emp_id, 
       Coalesce(pending_pi_amount, 0) AS pending_pi_amount 
FROM   proforma_invoice pi 
       JOIN users usr 
         ON pi.user_id = usr.user_id 
       JOIN employee emp 
         ON usr.employee_id = emp.emp_id 
       JOIN alarm_parameter ap 
         ON pi.user_id = ap.user_id 
WHERE  pi.user_id = 18 
       AND (status = 'Created' 
        OR status = 'Approved')
GROUP  BY pi.user_id, 
          max_pi_pending_amount, 
          first_pending_pi_warning_percentage, 
          second_pending_pi_warning_percentage, 
          emp_id, 
          pending_pi_amount

注意:-:您还可以将return view('prospect', ['courses' => $courses],['details_prospects' => $details_prospect]); 更改为return view('prospect', ['courses' => $courses,'details_prospect' => $details_prospect]); // OR return view('prospect', compact('courses', 'details_prospect')); ,删除 s

并在您的视图中更改循环变量:

details_prospects

答案 1 :(得分:0)

您正在使用数组,因此必须使用以下语法获取值:视图中的$details_prospect['id']

<td>{{ $details_prospect['id'] }}</td>
<td>{{ $details_prospect['prospect_name'] }}</td>
<td>{{ $details_prospect['prospect_nic'] }}</td>
...

由于数组不是集合,因此无法使用箭头。