get users ip address in Laravel without installing packages [duplicate] - php

This question already has answers here:
How to get client IP address in Laravel 5+
(20 answers)
Closed 2 years ago.
My site is trilingual ,I have a problem now, Normally, the site is in Persian language and users first see the Persian site and can change the language of the site from the language change option. How can the language be changed according to the IP of the visitor?
For example :Persian language users, by opening the site, view the site in Persian , Arabic-speaking users, by opening the site, see the site in Arabic .this is the method that used for change language.
controller:
public function index($locale) //en | fa
{
App::setLocale($locale);
Session()->put('local', $locale);
return redirect()->back();
}
app/config:
'timezone' => 'Asia/Tehran',
'locale' => 'fa',
'fallback_locale' => 'fa',
dropdown menu:
<div class="dropdown show nav-link">
#php $local =Session()->get('local'); #endphp
<a class="btn dropdown-toggle " href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<img src="{{asset('images/translate/translate.png')}}" width="40px" height="40px">
#switch($local)
#case('fa')
فارسی
#break
#case('en')
English
#break
#case('ar')
العربی
#break
#endswitch
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item text-right nav-link " href="{{route('language','fa')}}"><img
src="{{asset('images/translate/iran.png')}}"
width="30" height="30"
>
فارسی</a>
<a class="dropdown-item text-right nav-link" href="{{route('language','en')}}"> <img
src="{{asset('images/translate/english.png')}}" width="30" height="30"
>English</a>
<a class="dropdown-item text-right nav-link" href="{{route('language','ar')}}"> <img
src="{{asset('images/translate/emarat.png')}}" width="30" height="30"
>العربی</a>
</div>
</div>
Rout
Route::get('lang/{locale}', 'LocalizationController#index')->name('language');

salaam.
You can get the user IP from request() method that is available in every request (every route). For example in your index method:
public function index($locale) //en | fa
{
$userIp = request()->ip();
App::setLocale($locale);
Session()->put('local', $locale);
return redirect()->back();
}

Related

Is there a way displaying only specific data on the table? like if the data being receive in the database contains 'SuperAdmin' dont display it?

I already get all the data from the database and displayed it on my vue component. But, how can I display specific data on the table?
Like if the results from the database contains a user type of 'SuperAdmin'
I wanted to filter it like don't display if you find data related 'SuperAdmin'
https://imgur.com/kwTziOG - Here is my output.
I have searched this kind of problem and nothings pops-up.
Say for example.
['Paul','paul#new.com',['Admin']
['Paul', 'paul#new.com', ['SuperAdmin']
I just want to display the first array. Since it doesn't have a user type of SuperAdmin.
<tr v-for="user in users.data" :key="user.id">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.email}}</td>
<td>{{user.type}}</td>
<td>{{user.created_at |myDate}}</td>
<td>
<a href="#" #click="editModal(user)">
<i class="fa fa-edit"></i>
</a>
/
<a href="#" #click="deleteUser(user.id)">
<i class="fa fa-trash red"></i>
</a>
</td>
</tr>
I just displayed all the data being fetch in the database. Just wanted it to filter it somehow.
I have not used Vue in alittle while but if I remember correctly you can just use 'v-if' and it will conditionally render the data. Below is an example of how this could be done.
Possible Solution
<tr v-for="user in users.data" :key="user.id">
<div v-if="user.type == 'SuperAdmin'">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.email}}</td>
<td>{{user.type}}</td>
<td>{{user.created_at |myDate}}</td>
<td>
<a href="#" #click="editModal(user)">
<i class="fa fa-edit"></i>
</a>
<a href="#" #click="deleteUser(user.id)">
<i class="fa fa-trash red"></i>
</a>
</td>
</div>
</tr>
Hope this answered your question, good luck!

Laravel 5.6. How to switch languages with html anchor or other tag?

I searched for a solution in Google but I didn't found any specific. I'm new in Laravel so ... I'll explain my idea and will paste my code after that.
In the Header of my pages I have a few links for to switching the languages. Actually to be specific I have four countries flags as every flag respond for the specific language. I would like to change the language when user click on the German flag as an example. Then page need to be reloaded to update translations on it. This functionality works only in index Actions.
My Source Code: This is the html in my header located in app.blade.php
<div class="languages">
#if (Config::get('app.locale') == 'bg')
<a class="lang bg active" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
#else
<a class="lang bg" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'en')
<a class="lang en active" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
#else
<a class="lang en" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'de')
<a class="lang de active" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
#else
<a class="lang de" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'ru')
<a class="lang ru active" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
#else
<a class="lang ru" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
#endif
/Middleware/Local.php
<code>
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
use App;
class Locale
{
public function handle($request, Closure $next)
{
$locale = Session::get('locale');
App::setLocale($locale);
return $next($request);
}
}
</code>
web.php
Route::get('/home', 'HomeController#index')->name('home');
Route::prefix('/home')->middleware('locale')->group(function() {
Route::get('/', function () {
return view('home');
});
});
Route::get('/switchLang/{lang}', 'SwitchLanguageController#switchLang')->name('switchLanguage');
app.js
$(function(){
var currentLanguage = document.documentElement.lang;
// Switch languages
$('.lang').on('click', function($event) {
$event.preventDefault();
var $selectedLang = $(this).data('lang');
$.ajax({
url: '/switchLang/' + $selectedLang,
type: 'GET',
success: function(response)
{
location.href = window.location.href;
}
});
});
});
SwitchLanguageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App;
use Log;
class SwitchLanguageController extends Controller
{
public function switchLang($lang)
{
Session::put('locale', $lang);
$response = ['status' => 'success', 'code' => '200', 'message' => 'Language was switched.', 'metod' => 'GET'];
return $response;
}
}
I found the solution of my issue. The problem was in my routes in web.php.
This code didn't work for me on Laravel 5.6;
Route::get('/users/profile', 'UsersController#profile')->name('profile');
Route::prefix('/users/profile')->middleware('locale')->group(function() {
Route::get('/users/profile', function () {
return view('profile');
});
});
I replaced the above code with this one!
This work for me on Laravel 5.6:
Route::get('/users/profile', 'UsersController#profile')->name('profile')->middleware('locale');
Now Localization works for every route described as above. :)
Thank you very much for the help of #Devon.
The documentation of Laravel 5.6 really help me to fix the issue.
https://laravel.com/docs/5.6/middleware

How to check which guard we are currently within in laravel [duplicate]

This question already has answers here:
Checking which `guard` is loggedin
(8 answers)
Closed 3 years ago.
I have here a portion of my header.blade.php file of a project.
#guest
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
#if(condition here)
$profile='/myprofile';
$logout='/logout';
#else
$profile='/admin/myprofile';
$logout='/admin/logout';
#endif
<ul class="dropdown-menu">
<li>My Profile</li>
<li> Log Out
</li>
</ul>
#endif
</li>
#endguest
I have 2 guards namely web and admin. I want the $profile and $logout to be '/profile' and '/logout' && '/admin/profile' and '/admin/logout' respectively for each of the 2 guards.
Now I know that I can check for each guard as Auth::guard('name')->check();
But, when both the guards are logged in at the same time, it creates a sort of problem this way.i.e.
if(Auth::guard('web')->check())
{$profile="/myprofile";}
if(Auth::guard('admin')->check())
{$profile="/admin/myprofile";}
Since both will be active, $profile will have the latter value in every case.
So, is there a way such that this problem can be addressed?
Something like
if(Auth::guard()=='admin'){
////////////////
}
which of course doesn't work.
I guess youll have to have separate headers for each of the interfaces taken care by each of your guards. Clearlyif(Auth::guard()=='admin'){
////////////////
} is not valid without mentioning the guard name

Why the result looks strange when I send different parameters to the same function? (laravel 5.3)

My view is like this :
...
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create.year', [$year]) !!}">
Add New
</a>
...
...
#foreach($testArray as $key)
...
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$key['display'], $year]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>
...
#endforeach
...
My routes is like this :
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
My controller is like this :
public function create($year, $display = null)
{
echo $display.' : display <br>';
echo $param_thang. ' : year';die();
...
}
When I call url like this : http://localhost/mysystem/public/users/create/2016, it works.
There result is like this :
: display
2016 : year
When I call url like this : http://localhost/mysystem/public/users/create/14144499452111901/2016
The result is like this :
2016 : display
14144499452111901 : year
It looks strange, the result is reversed
Why did it happen?
It's because your route is:
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
So the first parameter is always going to be the year, and the 2nd parameter is always going to be the display variable.
You need to change your <a> as #farkie suggested:
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$year,$key['display']]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>

Laravel 5.3 Route in template showing Route Not Defined

i am updating my application from laravel 5.2 to 5.3. Most of the things seems to work fine.
But i dont know what is happening but when i am trying to define route in anchor tag, its not working. I have done something similar to this:
<a href="{{route('backend.pages.index')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Its showing error Route [backend.pages.index] not defined.. Here is how the created the route.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController');
});
I have a template called 'mainmenu.blade.php' in which i have use this route. This mainmenu is called in main structure through #include('layouts.backend.backendstructure.mainmenu').
Is routing method is changed in laravel 5.3? Or is there any mistake from my side?
Thank you!(Advance)
The problem here is
{{route('backend.pages.index')}}
instead use
<a href="{{route('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
The route is defined as backend/pages. To return view add a method in PagesController and return the view there.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController#dummymethod');
});
Dummy method
public function dummymethod
{
return view('backend.pages.index');
}
Edit
I think you're looking for something like this
Route::resource('backend/pages','Backend\PagesController', ['names' => ['index' => 'backend.pages.index']]);
Check the docs here
You should write your code like this:
<a href="{{ route('backend/pages')}} " class="nav-link ">
<span class="title">All Pages</span>
</a>
or like this:
<a href="{{ url('backend/pages') }}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Try:
<a href="/backend/pages" class="nav-link ">
<span class="title">All Pages</span>
</a>
https://laravel.com/docs/5.3/routing
You can try link with URL to like, I use in following manner
<a href="{{URL::to('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>

Categories