-1

This is my : IndexPageController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class IndexPageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $products = Product::inRandomOrder()->take(10)->get();

        // return view('pages.index')->with('index', $products);
        $products = Product  ::inRandomOrder()->take(9)->get();
        return view ('pages.index')->with('index',$products);

    }
}
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
hrt
  • 9
  • 5
  • 3
    You're probably gonna need to show us your `pages.index` view. None of the code you've shared would cause this error. I suspect you're using `$products` in your view, but you've named the view's variable `$index` in the `->with()` call. – ceejayoz Apr 02 '19 at 13:18
  • you probably acessing the wrong variable as @ceejayoz said. try changing ```'index'``` to ```products``` – Piazzi Apr 02 '19 at 13:21
  • @ceejayoz, you were right, he did use $products in his view, Could you write an answer? – Piazzi Apr 02 '19 at 13:30
  • 2
    You should not post a question just made with image links with no description. Check the question writing rules. – namelivia Apr 02 '19 at 13:37
  • @namelivia I know what you say, but I was hurrying. – hrt Apr 02 '19 at 13:39
  • 2
    @AlexandruHort In your hurry, though, you waste everyone else's time. – ceejayoz Apr 02 '19 at 13:43
  • @ceejayoz Yes you are right. Next time will be more careful about these details. Thanks for understanding. – hrt Apr 02 '19 at 16:46

1 Answers1

2

You're using index as key on with(), I hope you've used {{ $products }} on view, so you should replace index with products on with() just like below

return view('pages.index')->with('products', $products);

OR

Instead of using with() you can pass the data as array to the view() just like below and you can use the corresponding key inside the view

return view('pages.index', ['products' => $products]);
Sethu
  • 1,299
  • 10
  • 13
  • What is diffrent between above code.i return same answer. – Manisha Apr 02 '19 at 13:37
  • You're welcome :) but someone downvoted this answer but still I'm happy this answer helps you – Sethu Apr 02 '19 at 13:37
  • @Sethu I didn't downvote you, but I suspect the person who downvoted every answer here did so because none of them explained *why* the change is needed. – ceejayoz Apr 02 '19 at 13:44
  • 2
    I downvoted, because there is no explanation and you wrote this answer before he linked the images, so my guess is that you readed our comments (the answer is there) and just copied the text to this answer. I'm glad that the OP has his problem solved, but that's not the way we do things. – Piazzi Apr 02 '19 at 13:51
  • @ceejayoz I can understand that, Thanks for your info – Sethu Apr 02 '19 at 13:57
  • Please check my posted time,No problem ,his problem solved.that is fine. – Manisha Apr 02 '19 at 13:59
  • @LucasPiazzi I'm glad you've warned me in a manner to notify when i should answer the question and I'm not an expert but still i know few things in laravel, please don't say that i copied your text and thanks for your warning. – Sethu Apr 02 '19 at 14:04
  • It was just a guess, i believe @ceejayoz deserve the credits for this because he figured out the problem, i just writed a possible solution. but if you say you did not copy, I believe in you . Just make sure to write a explanation as well so i can upvote your answer :). – Piazzi Apr 02 '19 at 14:10
  • @LucasPiazzi I can understand that, I should've added the explanation before and I missed, thanks for your advice. I'll follow this here after. I updated the explanation on my answer :) – Sethu Apr 02 '19 at 14:31
  • I upvoted your answer, and i'm sorry for suggest that you copied, but i think i had reasons for that. Anyway, thanks for adding a explanation – Piazzi Apr 02 '19 at 14:35