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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.