0

I have two functions

whatsnew()

and

perfume()

both has its own @foreach in different sub folders which i show them in one page.

problem is if one @foreach works the other show an error undefined variable

ps- this is my first time posting a question in SO .. sorry if its sloppy..

ProductController.php

//to show last 5 latest items
public function whatsnew(){
    $cat_new = products::orderBy('id', 'desc')->take(5)->get();
    return view('/home', compact('cat_new'));
}

//to show category
public function perfume(){
   $perfume = products::where('category','LIKE','perfume')->get();

    return view('/home', compact('perfume'));
}

Web.blade.php

Route::get('/', [
'uses' => 'ProductController@whatsnew',
'as' => 'db.whatsnew']);

Route::get('/', [
'uses' => 'ProductController@perfume',
'as' => 'db.perfume']);

perfume.blade.php

@foreach($perfume as $perfume_cat)

whatnew.blade.php

@foreach($cat_new as $row)
Mizy
  • 69
  • 9
  • 4
    welcome to SO. please show us your code. – dparoli Jul 26 '19 at 20:15
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – ceejayoz Jul 26 '19 at 20:15
  • @dparoli i have edited the post above. – Mizy Jul 26 '19 at 20:31

2 Answers2

1

It looks like you are passing only 1 collection back to the view each time, which is probably why one or the other works.

If you change this:

public function whatsnew(){
    $cat_new = products::orderBy('id', 'desc')->take(5)->get();
    return view('/home', compact('cat_new'));
}

public function perfume(){
   $perfume = products::where('category','LIKE','perfume')->get();

    return view('/home', compact('perfume'));
}

to this:

public function whatsNew(){
    $cat_new = products::orderBy('id', 'desc')->take(5)->get();

    return $cat_new; // return the collection 
}

public function perfume(){
   $perfume = products::where('category','LIKE','perfume')->get();

    return $perfume; // return the collection 

}

// Create a new index function and pass both collections to your view 
public function index() {
    return view('index', [
        'cat_new' => $this->whatsNew(),
        'perfume' => $this->perfume(),
    ]);
}

Your web routes file can be:

Route::get('/', 'ProductController@index')->name('product.index');

Your index.blade.php

<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <ol>
                        @foreach( $cat_new as $new )
                            <li>{{ $new->foo }}</li>
                        @endforeach
                    </ol>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <ol>
                        @foreach( $perfume as $p )
                            <li>{{ $p->bar }}</li>
                        @endforeach
                    </ol>
                </div>
            </div>
        </div>
    </div>
</div>

where foo and bar are whatever columns you are after.

FullStackOfPancakes
  • 1,351
  • 1
  • 17
  • 30
1
//merge become one
public function perfume(){
   $perfume = products::where('category','LIKE','perfume')->get();
   $cat_new = products::orderBy('id', 'desc')->take(5)->get();

    return view('/home', compact('perfume', 'cat_new'));
}