i need the code that will enable me fetch an entire table from the database and load it to my view. and also to the code that will display the item in my views
my controller:
public function index()
{
if(Auth::user()->usertype=='Admin')
{
$categories_count = Categories::count();
$news_count = News::count();
$published_news = News::where('status', 1)->count();
$unpublished_news = News::where('status', 0)->count();
$slider_news = News::where('slider_news', 'yes')->count();
$slidsder_news = News::where('slider_news', 'yes')->count();
$featured_news = News::where('featured_news', 'yes')->count();
$editor = User::where('usertype', 'Editor')->count();
}
else
{
$user_id=Auth::user()->id;
$news_count = News::where(['user_id' => $user_id])->count();
$published_news = News::where(['user_id' => $user_id, 'status' => '1'])->count();
$unpublished_news = News::where(['user_id' => $user_id, 'status' => '0'])->count();
}
return view('admin.pages.dashboard',compact('categories_count','news_count','published_news','unpublished_news','slider_news','featured_news','editor'));
}
You can simply do
Model::all();
To get all the data for that model/table. I would recommend paginating this if you have large data sets like so:
Model::all()->paginate(20);
See pagination here.
Once you have this assigned to a variable, as you already have done, you can pass it into your compact.
Within your view, it's always worth checking the collection isn't empty before attempting to loop over:
@if ($exampleItems->isNotEmpty())
and then you can continue to loop over the collection:
@foreach ($exampleItems as $exampleItem)
I recommend learning the basics using various tutorials such as Laracasts because if I've understood your request, this is fairly basic.