Vivek Titwal Vivek Titwal - 4 months ago 9
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' ,[
'uses'=>'gallerycontroller@viewgallerylist',
'as'=>'viewgallery'
]);

Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller@savegallery',
'as'=>'savegallery'
]);


My controller:

public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];


$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
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">
</form>

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

Answer

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