0

I have faced the issue for Undefined variable: images - Error message. Please check the below codes. Please support me.

'Route::get('partials/recent-gallery','ImageGalleryController@recentview');'

'public function recentview()
{
    $images = ImageGallery::all();
    return view('partials.recent-gallery', ['images' => $images]);          
}'

---Home Page '@include('partials/recent-gallery')'

---- View page-----recent-gallery.blade.php------

'@if($images->count())
    @foreach($images as $image)  
        {{ $image->galley_image }}
@endforeach  
@endif'

-------------------ERROR---------------
$images is undefined

3
  • pass the image parameters to the view with @include('partials/recent-gallery', ['images' => $images]). documentation Commented Sep 23, 2021 at 14:26
  • Hi Tithira, I have tried to this code to first parent page (home.blade.php) but same error faced and try to this code to subview page (recent-gallery.php) but again same error faced. I am the beginner please help. Commented Sep 24, 2021 at 16:01
  • please check the answer for clarification. Commented Sep 24, 2021 at 18:48

1 Answer 1

1

The steps to pass data to subview is by passing the data from the parent (In your case it is the homepage) to the subview (recent-gallery) with the help of the blade syntax.

You must first pass the data to the Homepage from your function which initialize your homepage route and pass the image data for your subview.

public function viewHomePage()
{
   $images = ImageGallery::all();  
 
   // passing your image data to the homepage not to your subview

   return view('home', ['images' => $images]); 
}

You do not have to have a separate route for your subview as it is a subview. Pass the data you passed along from your Homepage view function to the subview by blade syntax @include('partials/recent-gallery', ['images' => $images]). Notice we have not routed a subview in web.php or have a controller function to pass image data.

home.blade.php

<!-- homepage content -->

@include('partials/recent-gallery', ['images' => $images])

Then you can access the image data inside your subview with the parameter $images. You can check your passing data with dd() method and passing the parameters dd($images) so you can debug.

partials/recent-gallery.blade.php

@if($images->count())
    @foreach($images as $image)  
        {{ $image->galley_image }}
    @endforeach  
@endif
Sign up to request clarification or add additional context in comments.

1 Comment

I have added the HomeController in Route page and data passing code added to index function. Working now. Thanks for your support. --web.php--- Route::get('/','HomeController@index'); ---HomeController.php-- use App\Models\ImageGallery; class HomeController extends Controller { public function index(){ // Passing your image data to the homepage not to your subview $images = ImageGallery::where('galley_status', '=', 0)->orderBy('galley_id', 'desc')->limit(6)->get(); return view('pages.home', ['images' => $images]); } }

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.