Homepage won't show newly added records but other pages do - laravel

I'm using a Laravel 5.7 in an old web project last year 2019 but now I encountered a very weird bug.
This is a website that when you add a new game, it will show to the menu list and if it is featured it will show to the homepage content.
this is the table ['game_id', 'name', 'key', 'status', 'order', 'is_featured']
in the header view I put this code:
<ul class="nav-menu">
<li class="{{ ($page == "games" ? 'menu-active' : '') }} submenu dropdown"><a href="/games" >Games</a>
<ul class="dropdown-menu">
#foreach(GetPublishedGames() as $game)
<li class="nav-item">{{$game->name}}</li>
#endforeach
</ul>
</li>
...
calling this helper code GetPublishedGames()
function GetPublishedGames(){
$data = DB::table("game")->where("status", 1)->orderBy("order")->get();
return $data;
}
in the homepage controller I had this line
"feat_games" => Game::where("status", 1)->where("is_featured", 1)->orderBy("order")->limit(5)->get(),
that calls in the homepage view:
#if(count($feat_games))
#foreach($feat_games as $feat_game)
<div class="feat-game-item wow fadeInUp">
... some content ...
</div>
#endforeach
#endif
I have no idea why this happening, it works before in the local and development stage. I did tried to clear cache.
any idea?

Related

How to check which controller data object being executed?

I have faced a typical problem. The problem is which controller data object being executed in view page. Here the data object is $items. I have looked most of controller but can not find data object $items . Please help me to find it. Thank in advance. The portion of view page :
#foreach($items as $item)
<div class="partner-logo">
<div class="inner-div red-box">
<p>
<img src="{{ ($item->image) ? asset('file/images/' . $item->image) : "" }}" alt="" />
</p>
<h3>
<a
href="{{ isset($item->url) ?route('page.content.show', $item->url) : "#" }}"> {{ $item->title }}
</a>
</h3>
</div>
</div>
#endforeach
In Laravel, controllers usually pass their data to views in this format:
return view('nameOfView', [
'firstParam' => $firstParam,
'secondParam' => $secondParam,
'etc' => $etc
]);
So you can search in your controllers for the view name and then for the parameter name (in this case $items). If there are multiple controller functions with the $items param and the same view, add a test param to each and see which one your view renders. You can also check in routes/web.php for the current URL you are on, and find the correct controller function that way.

Laravel: How to create link buttons on a view dynamically?

I'm making a College Administration website where a professor can log in.
I have a dashboard, where my dynamically generated button should be placed: (right now it just has dummy buttons!)
Generated by this view file, which I will have to modify soon:
<div class="container d-flex flex-column align-items-center justify-content-center">
<h1>IA DASHBOARD</h1>
<br>
<div class="grid2">
SUBCODE 1</button>
SUBCODE 2</button>
SUBCODE 3</button>
</div>
Tables in the Database:
the table iamarks contains the data (student info, and marks) that is to be displayed after /subcode/{subcode} narrows it down to records of just the students that are in the class assigned to current logged-in professor.
classroom_mappers is a table used to map a professor to a classroom with a subject. It makes sure that one classroom only has one professor for a particular subject.
the routes currently in my web.php:
route::get('/ia', 'IAController#show')->middleware('auth');
Route::get('/subcode/{subcode}', 'IAController#showTable')->middleware('auth');
...and these are the methods inside my controller:
//shows buttons to the user:
public function show(){
$subcodes = DB::table('classroom_mappers')
->select('subcode')
->where([['PID','=', auth()->user()->PID]])
->get();
return view('ia',compact('subcodes'));
}
//when user clicks a button, subcode is to be generated and a table is to be shown:
//it works, I tried it by manually typing in subcode value in URL.
public function showTable($subcode){
$sem = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['subcode','=',$subcode]])
->pluck('semester');
$division = DB::table('classroom_mappers')
->where([['PID','=', auth()->user()->PID],
['semester','=',$sem],
['subcode','=',$subcode]])
->pluck('division');
$data = DB::table('iamarks')
->where([['semester','=',$sem],
['division','=',$division],
['subcode','=',$subcode]])
->get();
return view('subcode',compact('data'));
}
My Problem:
To be able to generate the {subcode} in the URL dynamically, I want to create buttons in the dashboard using the data $subcodes. The controller hands over the $subcodes (an array of subject codes which belong to logged in professor) which are to be made into buttons from the show() method.
The buttons should have the name {subcode} and when clicked, should append the same subject code in the URL as {subcode}.
How do I make use of $subcodes and make the buttons dynamically?
How do I make sure the buttons made for one user are not visible to another user?
I managed to find the solution, thanks to Air Petr.
Apparently, you can't nest blade syntax like {{some_stuff {{ more_stuff }} }} and it generates a wrong php code. I modified the solution by Air Petr to:
<div class="grid2">
#foreach ($subcodes as $subcode)
<a href="<?php echo e(url('/subcode/'.$subcode->subcode));?>">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">
<?php
echo e($subcode->subcode);
?>
</button>
</a>
#endforeach
</div>
It generates the buttons perfectly. The buttons for one user are not visible to another, since I'm using PID constraint in a query (['PID','=', auth()->user()->PID]).
Pass the passcodes array to view:
$subcodes = []; // Array retrieved from DB
return view('subcode', compact('subcodes'));
And in subcode.blade.php, loop through each subcode:
<div class="grid2">
#foreach($subcodes as $subcode)
<a href="{{ url('/subcode/' . $subcode->subcode) }}">
<button class="btn btn-outline-primary btn-custom-outline-primary btn-custom">SUBCODE {{ $subcode->subcode }}</button>
</a>
#endforeach
</div>
You can loop your codes to create buttons. Something like this (it's for "blade" template engine):
<div class="grid2">
#foreach ($subcodes as $subcode)
{{ $subcode->subcode }}</button>
#endforeach
</div>
Since you're using PID constrain in a query (['PID','=', auth()->user()->PID]), you'll get buttons for that specific PID. So there's no problem.

Laravel show all links in paginator

Since Laravel 5.7, the paginator links() method shows only a reduced number of pages when called. The number of links can be customized using the onEachSide($count) method.
‹‹ ‹ 1 2 ... 47 48 49 50 51 52 53 ... 102 103 › ››
I want to display every page in order to make a page selector dropdown, using a html <select>.
Calling onEachSide(9999) does work, but it looks flimsy, and may break on high page counts that the app i'm writing could reach.
What would be the clean way to get all the page links ?
EDIT: I am using a custom view, which looks like the following :
#if ($paginator->hasPages())
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled">‹‹</li>
<li class="disabled">‹</li>
#else
<li>‹‹</li>
<li>‹</li>
#endif
{{--Pagination Elements --}}
#foreach ($elements as $element)
#if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>›</li>
<li>››</li>
#else
<li class="disabled">›</li>
<li class="disabled"><span>››</li>
#endif
</ul>
#endif
The $elements variable seems to already have the extra pages removed, however.
The paginator uses the bootstrap-4 blade view to render the page. This view always implements the tripple dots.
You could create a custom view that renders all pagination pages without any tripple dot items and pass this view as the first parameter to the links function.
Update: The view you are using is exactly the same as the one build in to Laravel. You shouldn't use the $element variable as this gets windowed with tripple dot elements.
You should use the pagination lastpage variable and loop over all pages yourself. For example, like this:
{{-- Pagination Elements --}}
#for ($page = 1; $page <= $paginator->lastPage(); $page++)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach

Pagination is not showing

I'm trying to get pagination into my view. I don't know where is the problem, the code doesn't show any errors.
Here is my controller function
public function apakskategorijas($id)
{
$apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(2);
return view ('kategorijas.apakskategorijas',compact('apakskat'));
}
View
#section('content')
#foreach($apakskat as $apk)
#foreach($apk->prece as $prec)
<div class="col-md-4 kat">
<a href="{{ url('kategorija/apakskategorija/preces/'.$prec->id) }}">
<div>
<img src="{{ URL::to($prec->path) }}">
</div>
<div class="nos">
<p>{{$prec->nosaukums}}</p>
</div></a>
<div class="price-box-new discount">
<div class="label">Cena</div>
<div class="price">{{ $prec->cena }} €</div>
</div>
<div><span>Ielikt grozā</span></div>
</div>
#endforeach
#endforeach
<center>{{$apakskat->links()}}</center> <--pagination
#endsection
dd($apakskat)
UPDATE
when i changed code in my controller to $apakskat = apakskategorijas::paginate(1); then it showed me pagination, but this doesn't work for me since i need to display items in each subcategory, with this code it just displays every item i have,it doesn't filter which subcategory is selected.
This is why i need this $apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(1); with is a function that i call which creates a relation between tables, so that it would display every item with its related subcategory.
That's the behavior of paginator in current Laravel version. When you have just one page, pagination links are not displayed.
To test this, just change the code to something like this to get more pages:
$apakskat = apakskategorijas::paginate(1);
If you want to show the first page if there is only one page, you need to customize pagination views. Just publish pagination views and remove this #if/#endif pair from the default.blade.php but keep the rest of the code as is:
#if ($paginator->hasPages())
....
#endif

Laravel breadcrumbs bundle cannot find custom template

I am using https://github.com/davejamesmiller/laravel-breadcrumbs package.
Installed as it says in instructions, but i want to create a custom template for my breadcrumbs.
I have created app/breadcrumbs.php:
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', route('home'));
});
Breadcrumbs::register('about', function($breadcrumbs) {
$breadcrumbs->parent('home');
$breadcrumbs->push('About company', route('about'));
});
Then in config file of that package:
return array(
'view' => 'laravel-breadcrumbs::_partials.breadcrumbs',
);
Then created that view in app/views/_partials/breadcrumbs.blade.php:
#if ($breadcrumbs)
<ul class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if (!$breadcrumb->last)
<li>{{{ $breadcrumb->title }}}</li>
#else
<li class="active">{{{ $breadcrumb->title }}}</li>
#endif
#endforeach
</ul>
#endif
Then i am printing that breadcrumb in my view:
{{ Breadcrumbs::render('about') }}
However, i am getting error:
View [_partials.breadcrumbs] not found.
How it cannot be found if i created it?!? spent 4 hours to figure out that thing. Please. help
James helped me. I had to do:
return array(
'view' => '_partials.breadcrumbs',
);

Resources