Vivek Titwal Vivek Titwal - 1 year ago 40
PHP Question

how to print the database content on a desired page?

I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.

My code is here:

My model name is:gallery

My routes:

Route::get('/gallery/list' ,[

Route::post('/gallery/save' ,[

My controller:

public function viewgallerylist()
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
public function savegallery(Request $request)

$gallery=new gallery();
return redirect()->route('viewgallery');

My desired page:

<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">

@foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>

Answer Source

Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?

which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.

public function viewgallerylist()
        $galleries = Gallery::all();
        return view('gallery')->with('galleries', $galleries);