Laravel 5 next and previous paging - php

Hello I have a list of max 5 articles in my homepage but I have many more articles and I would like to show these articles in other pages. So when I see more old posts, I would like to do a pagination like user home page / 1 when I get it as a result of posting it as url. But I do not follow the correct paths when I make directions.
I need to change the following lines of code to help you change the point, good day, good day
app/Http/Controller/HomeController:
public function deneme($page){
$url = url()->full();
$myUrl = explode('/', $url);
$uz= sizeof($myUrl);
$myUrl = $myUrl[$uz-1];
if ($myUrl == 'work.com'){
$yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();
$posts = array_slice($yazilar->getIterator()->getArrayCopy(),0,5);
return view('backend.pages.anasayfa')->with('yazilar', $posts);
}else{
$baslangic = $page*5;
$yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();
$posts = array_slice($yazilar->getIterator()->getArrayCopy(),$baslangic,5);
return view('backend.pages.anasayfa')->with('yazilar' ,$posts);
}
}
routes/web.php:
Route::get('/{page}', 'HomeController#deneme');
View :
<ul class="pager">
<li class="next">
Older Post →
</li>
</ul>

You can use this code in the blade (Laravel 5.7)
#if($news->previousPageUrl() != null)
<i class="fa fa-chevron-left"></i> NEWER
#endif
#if($news->nextPageUrl() != null)
OLDER <i class="fa fa-chevron-right"></i>
#endif

Use Pagination
Controller
$users = DB::table('users')->paginate(15);
View
{{ $users->links() }}
for More Details https://laravel.com/docs/5.2/pagination

Related

Notification icon hide number after click

I use October and I have made plugin with builder and I just add notification icon in menu.
I want to show "!" if there is a new item and hide "!" if user click that (it open bootstrap modal). And when I add new item it shows "!" again etc... I don't know exactly what is the best way to do this. Thank you if you can help me with this.
Menu:
<li>
<a data-toggle="modal" data-target="#itemModal"><i class="fa fa-bell"></i>
<span>! {{ Item }}</span></a>
</li>
Code:
function onStart()
{
$this['Item'] = Db::table('items')->count();
}
for this you need db table and you need to save last seen/click date.
so idea here is like you will add current date when user click !, you will show bootstrap modal and fire ajax request and set current date.
fire ajax request at that time and add new date for param.
for this you can use CMS default params table
<?php
use System\Models\Parameter;
....
// something like this in ajax request
Parameter::set('your_plugin_name::items.last_click_date', time() );
// this is when page reload
use System\Models\Parameter;
function onStart()
{
// now
$lastClickDate = Parameter::get('your_plugin_name::items.last_click_date');
if($lastClickDate == null) {
// first time it will just count items directly as user never click on icon
$count = Db::table('items')->count();
}
else {
// new this will call next time as user called that modal and we added click date
// so we compare if any record added after that date if it is then show `!`
$count = Db::table('items')->where('created_at', '>', $lastClickDate)->count();
}
$this['item_count'] = $count;
$this['new_items'] = $count > 0 ? true : false;
}
html
<li>
<a data-toggle="modal" data-target="#itemModal">
<i class="fa fa-bell"></i>
{% if new_items %}
<span>! {{ item_count }} </span>
{% endif %}
</a>
</li
please add comment if you face any problem.

In Codeigniter : How to get url if i clicked on the fetched data from database

I get this data through one controller in a list in my view files,and i also want to show in every page a different meta title. How it is Possible ?
View
<?php
$data = "";
foreach($items_data as $row){
$data .= '<li class="list-group-item">
<a href="'.base_url("hotels/ls/".$row->item_id).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
</li>';
}
echo $data;
?>
Suppose i got four items like
Books
Pens
Computers
All of my items are showing these urls which is pretty good.
www.xyz.com/1
www.xyz.com/2
www.xyz.com/3
But i want to show my item names in every url. along with different meta tag title name.
Now when i click on any listed items my url will show something like these
This is possible or not , Because above list are getting from database.
www.xyz.com/computers
www.xyz.com/Books
www.xyz.com/Pens
Here is my controller which is fetching data from database.
Controller
function index() {
$data['items_data'] = $this->Visa_mdl->get_items_name();
$data['title'] = "Hotels";
$this->load->view('include/header');
$this->load->view('hotels/visa',$data);
$this->load->view('include/footer');
}
This Controller is Linked to other Pages.
function ls($item_name = NULL) {
$data['title'] = "Company Lists";
$result = $this->Visa_mdl->get_items_company_list($item_name);
$result1 = $this->Visa_mdl->get_items_name_in_dash($item_name);
$data['items_data'] = $this->Visa_mdl->get_items_name();
// $data['items_company_data'] = $this->Visa_mdl->get_items_company_list($id);
$this->load->view('include/header',$data);
$this->load->view('hotels/dashboard',$data);
$this->load->view('include/footer');
}
Instead of this:
<a href="'.base_url("hotels/ls/".$row->item_id).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
try
<a href="'.base_url("hotels/ls/".$row->item_name).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
and try again.
In your code you have passed $row->item_id which shows item id in url, instead of that pass $row->item_name then it will show item name in url.

Laravel-5.3 : Updating session not working properly

I'm using database driver to store sessions. When I update the payload, the views, wich depend on the datas into the payload, don't update.
Into my view, I often have things like that:
#if( Session::get('quicktrade') == 'Client' )
<li class="#if ($coreData['navParent'] == 'quicktrade') active open #endif">
<a href="javascript:;">
<i class="ico-quicktrade-{{Session::get('left_menu')}}"></i>
<span class="title">QUICK[TRADE]</span>
<span class="arrow #if ($coreData['navParent'] == 'quicktrade') open #endif"></span>
#if ($coreData['navParent'] == 'quicktrade') <span class="selected"></span> #endif
</a>
<ul class="sub-menu">
#if( Session::get('quicktrade_evaluation') == 1 )
<li class="#if ($coreData['navChild'] == 'evaluation') active #endif">
<a href="{{ url('/quicktrade/evaluation') }}">
NOUVELLE ÉVALUATION</a>
</li>
#endif
#if ( Session::get('quicktrade_liste') == 1 )
<li class="#if ($coreData['navChild'] == 'liste') active #endif">
<a href="{{ url('/quicktrade/liste') }}">
LISTE DES VÉHICULES</a>
</li>
#endif
#if ( Session::get('quicktrade_statistiques') == 1 )
<li class="#if ($coreData['navChild'] == 'statistiques') active #endif">
<a href="{{ url('/quicktrade/statistiques') }}">
STATISTIQUES</a>
</li>
#endif
</ul>
</li>
#endif
This make the menu to show or not options depend on the session.
All the datas are added to the payload during the LogSuccesfulLogin event.
So, if the user logout then login, I got no problem when I do some changes in the users table.
The problem is when the session is active and the user don't logout. If I do some changes, the view will stay the same as before.
In my user controller there is what happen:
$user = User::with('acces')->find($user->id);
$sessions = DB::table('sessions')->where('user_id', $user->id)->get();
foreach ($sessions as $session) {
$payload = unserialize(base64_decode($session->payload));
$payload['oneaction'] = $user->acces->oneaction;
$payload['oneaction_type'] = $user->acces->oneaction_type;
if ($user->acces->oneaction == 'Client') {
$payload['oneaction_clients'] = $user->acces->oneaction_clients;
$payload['oneaction_list'] = $user->acces->oneaction_list;
$payload['oneaction_creator'] = $user->acces->oneaction_creator;
} else {
$payload['oneaction_clients'] = 0;
$payload['oneaction_list'] = 0;
$payload['oneaction_creator'] = 0;
}
//dd($payload);
$payload = base64_encode(serialize($payload));
DB::table('sessions')->where('id', $session->id)->update(['payload' => $payload]);
}
$sessions = DB::table('sessions')->where('user_id', $user->id)->get();
foreach ($sessions as $session) {
$payload = unserialize(base64_decode($session->payload));
//dd($payload);
}
As you can see in the code above, there is 2 dd(). Both show me that the changes are done properly. When I reload the page, ther is no change in the left menu.
I tried to do a php artisan view:clear right after the sessions have been updated, but the problem persist.
The only way I'm able to make this work, it's to delete all sessions of the user into the table. I don't want to go this way, so what am I missing? Why updating session don't work as I expect?
Your session gets probably lost because your response does not contain the session-cookie. In laravel this is done via the AddQueuedCookiesToResponse middlware which is kicked off in your HTTP-Kernel. But since you are dump and dying somewhere in your controller, you are preventing this middlware from running.
Please note that this is just a guess, there could be other reasons why your cookie is getting lost. Check your kernel and maybe also your config and ensure that you can see your cookie in your http response header.

How to Get the logged in User ID in Laravel 5.2

In my application I used the scaffolding method for creating authentication fast. But I just got one problem while playing with it. Actually I can get Auth::user()->name current logged in user username and other details but I can't get the user ID , I tried to get the ID with Auth::user()->id and Auth::id() but all in vain. Is there any method to get the current logged in user ID ?
What I tried :
I created a variable in AuthServiceProvider and then tried to get the ID in view like this
ServiceProvider
$id = Auth::id();
view()->share('id', $id);
View
> <li><a href="{{ url('/users/{!! $id !!}') }}"><i class="fa fa-btn
> fa-sign-out"></i>Profile</a></li>
Next Try in View:
<li><i class="fa fa-btn fa-sign-out"></i>Profile</li>
I want to get the logged in user ID so that I can show every user his/her profile.
You're doing a {!! !!} echo inside a {{ }}, but you can't nest those. It's like having <?php echo <?php echo ... ?> ?>. The nested printing is also inside quotes. Assuming that your idvariable is correctly set, here's how the call should look:
<a href="{{ url('/users/' . $id) }}">
Much easier too read, right? :)
in View try this:
{{ Auth::user()->name }}
in Controller try this:
\Auth::user()->id;

Laravel get current route

I have some navigation links and I want to get the current route in laravel 5 so that I can use the class active in my navigations. Here are the sample links:
<li class="active"> Home </li>
<li> Contact </li>
<li> About </li>
As you can see, in the Home link there is the active class which means that it is the current selected route. But since I am using laravel blade so that I would not repeat every navigation links in my blades, I can't use the active class because I only have the navigation links in my template.blade.php. How can I possibly get the current route and check if it is equal to the current link? Thank you in advance.
$uri = Request::path()
Check out the docs
You can use named routes like
Route::get('/', ['as'=>'home', 'uses'=>'PagesController#index']);
and then in the view you can do the following
<li {!! (Route::is('home') ? 'class="active"' : '') !!}>
{!! link_to_route('home', 'Home') !!}
</li>
So I got it working with some little tricks. For example the url in the address bar is http://localhost:8000/tourism/places.
If I will use the Request::path() method, I will get tourism/places. But what I wanted to get is the tourism part only so what I did is this:
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
Now the $final variable will have the value "tourism".
Now to make the variable load globally, I included the code in the AppServiceProvider.php inside the public function boot().
public function boot()
{
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
view()->share('final', $final);
}
And lastly in my blade template,
<li #if ($final== "tourism") class="active" #endif>
Places
</li>
Simple way is always better, just use the same function you use to generate links and it will give you all, including the flexibility to use wild cards
<a class="nav-link {{ Request::url() === route("products.list", [$group_key]) ? 'active' : '' }}"
href="{{ route("products.list", [$group_key]) }}"
>
LInk text
</a>
Plus, you still are going to have only one place to manage URL-s - your router files.

Categories