-1

I'm getting error "Trying to get property of non-object " when I pass my array to view

This is my function

 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]);
}

This is my view

<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>

I'm getting error Trying to get property of non-object (View: C:\xampp\htdocs\laravel\customer_inquiry_model\resources\views\prospect.blade.php)

But when I echo this in my function it's working

 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]);
}

when I pass to view it isn't work

Jazim Max
  • 111
  • 1
  • 10
  • Try this : return view('prospect',compact('courses','details_prospects')); – Pragna Apr 17 '19 at 07:00
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Dezza Apr 17 '19 at 07:49
  • can you show the ```dd of $details_prospects``` – ManojKiran A Apr 17 '19 at 08:12
  • array:2 [▼ 0 => "[{"id":3,"prospect_name":"Jazim","prospect_nic":"89494448884","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,8,9", ▶" 1 => "[{"id":4,"prospect_name":"Jazim","prospect_nic":"545454","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,5,9","comm ▶" ] – Jazim Max Apr 17 '19 at 08:22
  • 0 => "[{"id":3,"prospect_name":"Jazim","prospect_nic":"89494448884","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,8,9","comments":"wjkshkjasasd sajdkaskd as kjbksbkja","referred_through":"jbkjbkj dskj","created_at":"2019-04-14 15:38:48","updated_at":"2019-04-14 15:38:48"}][{"course_name":"BIT"}],[{"course_name":"AAT"}],[{"course_name":"Spoken English"}] ◀" – Jazim Max Apr 17 '19 at 08:26

2 Answers2

0

Change this line:

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

into:

return view('prospect', ['courses' => $courses,'details_prospect' => $details_prospect]);
// OR
return view('prospect', compact('courses', 'details_prospect'));

Note:- Hey you can also change the details_prospects into details_prospect remove s.

And also change the loop variable in your view:

@foreach($details_prospect as $prospect)
    <td>{{$prospect->id}}</td>
    //......
@endforeach
Inzamam Idrees
  • 1,955
  • 14
  • 28
  • You have to use both the methods mention in my answer? – Inzamam Idrees Apr 17 '19 at 07:07
  • @JazimMax check my edited answer. I hope it would helpful to you. – Inzamam Idrees Apr 17 '19 at 07:15
  • I'm having same problem @Inzamam Idrees – Jazim Max Apr 17 '19 at 07:17
  • before the loop You can add `@if(isset($details_prospect)` and then check that the error is shown or not? – Inzamam Idrees Apr 17 '19 at 07:19
  • Before you return the view in controller, please show me the output of this `dd($details_prospect);` – Inzamam Idrees Apr 17 '19 at 07:53
  • array:2 [▼ 0 => "[{"id":3,"prospect_name":"Jazim","prospect_nic":"89494448884","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,8,9", ▶" 1 => "[{"id":4,"prospect_name":"Jazim","prospect_nic":"545454","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,5,9","comm ▶" ] – Jazim Max Apr 17 '19 at 08:20
  • 0 => "[{"id":3,"prospect_name":"Jazim","prospect_nic":"89494448884","contact_number":"711266464","address":"9\/11, 15th lane, spill road,","course_intrested":"2,8,9","comments":"wjkshkjasasd sajdkaskd as kjbksbkja","referred_through":"jbkjbkj dskj","created_at":"2019-04-14 15:38:48","updated_at":"2019-04-14 15:38:48"}][{"course_name":"BIT"}],[{"course_name":"AAT"}],[{"course_name":"Spoken English"}] ◀" – Jazim Max Apr 17 '19 at 08:26
0

You're working with arrays, so you have to get your values using the following syntax : $details_prospect['id'] in your view.

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

As your array isn't a collection, you can't use the arrow.

Takachi
  • 701
  • 4
  • 14