0

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.

 @foreach ($compactData as $value) {{ $value->Name }} @endforeach 

this is my contoller function.

public function showallProducts()
{
    $productstock = Product::all()->stocks;
    $productoldprice = Product::all()->OldPrices;
    $productcurrentprice = Product::all()->CurrentPrice;
    $compactData=array('productstock', 'productoldprice', 'productcurrentprice');
    return view('welcome', compact($compactData));
}

this is my view

       <!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>


 </head>
 <body>
    <div class="flex-center position-ref full-height">

        <div class="content">
            <div class="title m-b-md">
                Laravel
            </div>

            <div class="title m-b-md">
                All products
            </div>


            <table>

                <tbody>
                @foreach ($compactData as $value)
                    {{ $value->Name }}

                @endforeach

                </tbody>


            </table>
        </div>
    </div>
</body>

why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html? My route is.

   Route::get('/', function () {

    $action = 'showallProducts';
    return App::make('ProductController')->$action();
 });
raja
  • 83
  • 2
  • 3
  • 16
  • Does it actually show you the `@foreach ($compactData as $value) {{ $value->Name }} @endforeach` in the browser source? – aynber Nov 15 '17 at 14:01
  • yes it prints the the @foreach loop in the browser source. when i try to run my porject – raja Nov 15 '17 at 14:03
  • Then you're not running it properly. You need to run your laravel project within a web server. If you don't already have one locally, Laravel's Homestead VM is a great solution. If you're already running it with a web server, then you'll need to check the server's configuration to find out why it's not parsing the PHP code. – aynber Nov 15 '17 at 14:05
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – aynber Nov 15 '17 at 14:05
  • The quickest and simplest way is to use Laravel Valet :) – Baldráni Nov 15 '17 at 14:05
  • no its not duplicate – raja Nov 15 '17 at 14:06
  • I have local webserver apache installed. – raja Nov 15 '17 at 14:07
  • How are you accessing that page? What's the address in the address bar? – aynber Nov 15 '17 at 14:08
  • are your sure your view is loading? – Ritesh Khatri Nov 21 '17 at 06:31

3 Answers3

1

Have you checked your $compactData variable? Please dd($compactData) to see what it contains.

Problem 1

You are accessing a relational property as a property of Eloquent collection, like this:

Product::all()->stocks

which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.

Problem 2

$compactData = array('productstock', 'productoldprice', 'productcurrentprice');

This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:

$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');

Problem 3

return view('welcome', compact($compactData));

Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:

return view('welcome', compact('compactData'));

Problem 4

Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.

Imran
  • 4,582
  • 2
  • 18
  • 37
0

I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:

filename.blade.php

and try again.

Explanation:

@foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

You most likely have a problem with your web server.

Try to use Laravel Valet as development environnement.

Edit : I found this : Valet for Windows

Baldráni
  • 5,332
  • 7
  • 51
  • 79