anchor tag not working on laravel 5.2 - laravel

I wont to make direct page with anchor tag in blade template engine, but its not work.
_sidebar.blade.php
<li class="active treeview">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
routes.php
Route::get('/', 'PagesController#getIndex');
Route::get('data_jamaah', 'PagesController#getJamaah');
PagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Inbox;
class PagesController extends Controller
{
public function getIndex() {
return view('pages.index');
}
public function getJamaah() {
return view('pages.data_jamaah');
}
}
The problem is When i click a nothing to happen, thanks for your help
Edit:
This is main.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
#include('partials._head')
#yield('stylesheets')
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
#include('partials._nav')
#include('partials._sidebar')
#yield('content')
<footer class="main-footer">
#include('partials._footer')
</footer>
</div>
#include('partials._javascript')
#yield('javascripts')
</body>
</html>

Your issue is coming from of how you are using adminLTE Sidebar.
When you specify treeview in your class, you will have a dropdown list. And the anchor will be deactivated.
So try something like that :
<li class="active">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li>
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
</li>

I think you can try this hope this work for you:
Route::get('/', 'PagesController#getIndex')->name('home');
Route::get('data_jamaah', 'PagesController#getJamaah')->name('dataJamaah');
<li class="active treeview">
<a href="{{route('home')}}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{route('dataJamaah')}}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>

Laravel has it's own helper function to create the routes, which you can see here:
https://laravel.com/docs/5.4/helpers#method-route
Edit:
You also need to have a name for your routes to work from the helper function.
On your routes.php:
Route::get('/', ['uses' => 'PagesController#getIndex', 'as' => 'homepage']);
Route::get('data_jamaah', ['uses' => 'PagesController#getJamaah', 'as' => 'data.jamaah']);
You need to change the href from url() to route().
Change
From:
<a href="{{ url('/') }}">
To:
<a href="{{ route('homepage') }}">
and for other route
From:
<a href="{{ url('data_jamaah') }}">
To:
<a href="{{ route('data.jamaah') }}">
And it will work perfectly fine.
If we specify the name the laravel routes automatically gets the route and renders for you.

Related

How to change the language of layouts using the Laravel localization tool?

I'm using Laravel's localization tool to change the language of my application (English and French). The change of language is done thanks to a select in the navigation bar.
With the official doc I use the routes, the controllers and the blades to change the language of the content of the pages.
However, I can't change the language of the layouts (footer and navbar links). Normally I create routes for my pages and point to the controller that contains the changeLang method. But I can't create route and controller for layouts because layouts concern all pages and therefore all routes. I'm new to Laravel so.. be indulgent :) thank you very much ^^
I've already searched a lot on StackOverflow and I haven't found anything that looks like my problem. Thank you to all of you!
BASIC METHOD (which works well FOR PAGES)
routes/web.php
Route::group([ 'middleware' => 'Language'], function () {
Route::get('/',"\App\Http\Controllers\HomeController#index");
Route::get('/change-language/{lang}',"\App\Http\Controllers\HomeController#changeLang");
});
app/Http/Middlewares/Language
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class Language
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* #return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(session()->has("lang_code")){
App::setLocale(session()->get("lang_code"));
}
return $next($request);
}
}
app/Http/Controllers/HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class HomeController extends Controller
{
public function index(){
return view("home");
}
public function changeLang($langcode){
App::setLocale($langcode);
session()->put("lang_code",$langcode);
return redirect()->back();
}
}
resources/views/home.blade.php (data come from resources/lang/en/home (or /fr/home))
#include('layouts/navbar')
<div class="container-fluid ">
<div class=""><img class="presentation-img " src="{{ asset('images/presentation.jpg') }}" alt="myself"></div>
<div class="presentation-paragraph zoomIn">
<h1>{{__("messages.title")}} </h1> <br>
<p class="paragraph">{!! trans('messages.presentation')!!}
</p>
</div>
</div>
#include ('layouts/footer')
<script>
function changeLanguage(lang){
window.location='{{url("change-language")}}/'+lang;
}
</script>
WHAT I HAVE TRIED TO DO FOR LAYOUTS (which doesn't work) (data come from resources/lang/en/navbar (or /fr/navbar))
resources/views/layouts/navbar.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="description" content="Portfolio" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mon Portfolio</title>
<link rel="shortcut icon" sizes="114x114" href="{{ asset('images/favicon.ico') }}">
<!-- Styles -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body class="antialiased">
<nav id="mainNav" class="navbar navbar-expand-lg py-lg-4 navbar-dark" style="padding: 23px 0px;">
<div class="absolute-select">
<select class="absolute-select-select" onchange="changeLanguage(this.value)" >
<option {{session()->has('lang_code')?(session()->get('lang_code')=='en'?'selected':''):''}} value="en">
<img src="{{ asset('images/flags/english.png') }}" />
English
</option>
<option {{session()->has('lang_code')?(session()->get('lang_code')=='fr'?'selected':''):''}} value="fr">
<img src="{{ asset('images/flags/french.png') }}" />
French
</option>
</select>
</div>
<div class="container">
<img class="logo-img " src="{{ asset('images/logo.png') }}" alt="logo ">
<button class="navbar-toggler navbar-toggler-right" data-bs-toggle="collapse" data-bs-target="#navbarResponsive"><span class="navbar-toggler-icon"></span></button>
<div id="navbarResponsive" class="collapse navbar-collapse">
<ul class="navbar-nav flex-grow-1 justify-content-evenly">
<li class="nav-item">
<a class="nav-link" href="/">{{__("navbar.one")}}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/projets">{{__("navbar.two")}}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/competences">{{__("navbar.three")}}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/apropos">{{__("navbar.four")}}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact">{{__("navbar.five")}}</a>
</li>
</ul>
</div>
</div>
</nav>
<script>
function changeLanguage(lang){
window.location='{{url("change-language")}}/'+lang;
}
</script>
On my page the content is not displayed. I get these navbar links :
Should I create a route/ a controller for layouts? I thought only the script in the blade was enough because the navbar and the footer are included in all pages... But I'm missing something to make it work!
Sorry if my question is a bit long and for my intermediate level of English.
Due to your comment, I think you should log your locale in the blade and send your result here.
like this :
{{ Config::get('app.locale') }}
I think you forgot to define lang
/lang
/en
navbar.php
/fr
navbar.php

Laravel HREF techniques for multiple pages

<!-- BEGIN HEADER MENU -->
<div class="nav-collapse collapse navbar-collapse navbar-responsive-collapse">
<ul class="nav navbar-nav">
<li class="dropdown dropdown-fw dropdown-fw-disabled active open selected">
<i class="icon-home"></i> Home
<ul class="dropdown-menu dropdown-menu-fw">
<li>
<i class="icon-bar-chart"></i> View Photo
</li>
<li class="active">
<i class="icon-bulb"></i> About
</li>
<li>
<i class="icon-graph"></i> Contact
</li>
</ul>
</li>
<li class="dropdown dropdown-fw dropdown-fw-disabled">
<i class="icon-home"></i> Upload
<ul class="dropdown-menu dropdown-menu-fw">
<li>
<i class="icon-bar-chart"></i> Upload Photo
</li>
<li>
<i class="icon-bulb"></i> View Photo List
</li>
</ul>
</li>
</ul>
</div>
I am still new to this but basically, I have 5 pages (View Photo, About, Contact, Upload Photo, View Photo List). Currently, I want to show this HTML in all 5 pages so that it will be standardized. If I change the "picture/about" in one page to "photograph/about" for example, the 5 pages will need to reflect this change. This gets more complex as the number of pages increases.
Is there any way or technique in Laravel to solve this issue? Thanks.
Create a template file like Laravel provides app.blade.php in resources/template/ directory, use extends, sections etc for designing, please read Laravel Documentation for Templates
You can use partials for this.
If your issue is adding the active class to the correct link then you can just use request()->is() to check the current url:
<li class="{{ request()->is('picture/externalphotoviewer') ? 'active' : '' }}">
<a href="{{ url('picture/externalphotoviewer') }}">
<i class="icon-bar-chart"></i> View Photo </a>
</li>
<li class="{{ request()->is('picture/about') ? 'active' : '' }}">
<a href="{{ url('picture/about') }}">
<i class="icon-bulb"></i> About </a>
</li>
<li class="{{ request()->is('picture/contact') ? 'active' : '' }}">
<a href="{{ url('picture/contact') }}">
<i class="icon-graph"></i> Contact </a>
</li>
Simply create a blade file inside your resources/views directory and then use #include e.g.
reasources/views/partials/header.blade.php:
<div class="nav-collapse collapse navbar-collapse navbar-responsive-collapse">
<ul class="nav navbar-nav">
<li class="dropdown dropdown-fw dropdown-fw-disabled active open selected">
<a href="javascript:;" class="text-uppercase">
<i class="icon-home"></i> Home </a>
<ul class="dropdown-menu dropdown-menu-fw">
<li class="{{ request()->is('picture/externalphotoviewer') ? 'active' : '' }}">
<a href="{{ url('picture/externalphotoviewer') }}">
<i class="icon-bar-chart"></i> View Photo </a>
</li>
<li class="{{ request()->is('picture/about') ? 'active' : '' }}">
<a href="{{ url('picture/about') }}">
<i class="icon-bulb"></i> About </a>
</li>
<li class="{{ request()->is('picture/contact') ? 'active' : '' }}">
<a href="{{ url('picture/contact') }}">
<i class="icon-graph"></i> Contact </a>
</li>
</ul>
</li>
<li class="dropdown dropdown-fw dropdown-fw-disabled">
<a href="javascript:;" class="text-uppercase">
<i class="icon-home"></i> Upload </a>
<ul class="dropdown-menu dropdown-menu-fw">
<li>
<a href="{{ url('photo/create') }}">
<i class="icon-bar-chart"></i> Upload Photo </a>
</li>
<li>
<a href="{{ url('photo') }}">
<i class="icon-bulb"></i> View Photo List </a>
</li>
</ul>
</li>
</ul>
</div>
Then replace the header code in your other blade files with:
#include('partials.header')
NB
If you change the location of the header you will need to update the #include's as well e.g.
e.g. resources/views/layouts/main/header.blade.php would be #include('layouts.main.header')
Hope this helps!

Undefined variable: users

I'm trying to show users in my view, but i have this error Undefined variable: users
I don't know what is the problem, because in my controller for that view the user method is called.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Role;
use App\Http\Requests;
class pagesController extends Controller
{
public function viewIndex(){
$users = User::all();
return view('index', ['users' => $users]);
//return view('index');
}
}
And this is the route where my controller is called
Route::get('/index', [
'uses' => 'pagesController#getIndex',
'as' => 'admin',
'middleware' => 'roles',
'roles' => ['Admin']
]);
This is my blade file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Laravel Blog</title>
<!-- CHANGE THIS TITLE FOR EACH PAGE -->
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Default Bootstrap Navbar -->
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<!-- Right Side Of Navbar -->
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
#if (Auth::guest())
<li><a class="btn btn-default" href="{{ url('/login') }}">Login</a></li>
<li><a class="btn btn-default" href="{{ url('/register') }}">Register</a></li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
#foreach($users as $user)
#if($user->hasRole('Admin'))
<li>Edit users</li>
<li>
<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
#else
<li>Edit page</li>
<li>
<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
#endif
#endforeach
</ul>
</li>
#endif
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="jumbotron">
<h1>Welcome to My Blog!</h1>
<p class="lead">Thank you so much for visiting. This is my test website built with Laravel. Please read my popular post!</p>
</div>
</div>
</div>
</div>
<!-- end of .container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>
Any ideas?
I think you're pointing to wrong controller method in your routes.
Your route will be:
Route::get('/index', [
'uses' => 'pagesController#viewIndex',
'as' => 'admin',
'middleware' => 'roles',
'roles' => ['Admin']
]);
and your controller (pagesController) will go like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Role;
use App\Http\Requests;
class pagesController extends Controller
{
public function viewIndex(){
$users = User::all();
return view('index', compact('users'));
}
}
I think this will help to solve your problem.
Note: Please follow the naming conventions in your code so that it would be easy to understand and some frameworks (like laravel itself) runs on some followed conventions.
Thanks!

How to format Laravel Link with <i> and <span> tags

How to format Laravel Link with <li> and <span> tags, for an example:
Laravel Link
{{ link_to('home', 'Home') }}
Should have to set like this
<li>
<a href="home">
<i class="icon-text-width"></i>
<span class="menu-text"> Home </span>
</a>
</li>
can someone please help me to create Laravel base link with that html tags, thank you
As far as i know, you can not do that using {{ link_to('home', 'Home') }}. However, instead you can do something like the following:
<li>
<a href="{{ route('home') }}">
<i class="icon-text-width"></i>
<span class="menu-text"> Home </span>
</a>
</li>
or you can use url() helper function.
<li>
<a href="{{ url('/') }}">
<i class="icon-text-width"></i>
<span class="menu-text"> Home </span>
</a>
</li>

Laravel 4 base/master view

I've recently gotten some problems. There is an model from which I pull some data and things. There is a menu link that I want to be on every page I enter, so I've put it into the base/master view. But the problem is, I need to enter ->with blabla thing on every public function in every controller. How could I not do that? I mean is there anyway around it? I don't want to do that with thingy on every controller method/function. Here's my code:
#if ( Auth::guest() )
<li style="float: right;padding-right: 0">
<ul class="nav">
<li>
<a href="{{ URL::to('register') }}">
<i class="icon-black icon-plus">
</i>
<strong>
Register
</strong>
</a>
</li>
<li>
<a href="{{ URL::to('login') }}">
<i class="icon-black icon-lock">
</i>
<strong>
Log in
</strong>
</a>
</li>
</li>
</li>
</ul>
#else
<li class="divider-vertical">
</li>
<li style="float: right;padding-right: 0">
<div class="btn-group">
<div class="btn btn-primary">
<i class="icon-user">
</i>
{{ (Auth::user()->name) }}
</div>
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
<span class="icon-caret-down">
</span>
</a>
<ul class="dropdown-menu">
<li>
<a href="{{ URL::to('account/managment') }}">
<i class="icon-user">
</i>
Account Managment
</a>
</li>
<li>
<a href="{{ URL::to('account/managment/change_credentials') }}">
<i class="icon-lock">
</i>
Change Password
</a>
</li>
<li class="divider">
</li>
<li>
<a href="{{ URL::to('account/logout') }}">
<i class="icon-off">
</i>
Log out
</a>
</li>
</ul>
</div>
#endif
You can define a View Composer :
View::composer(array('your.first.view','your.second.view'), function($view)
{
$view->with('count', User::count());
});
Everytime you call your view, a user count will be bound to it automatically.
Edit:
About where to use it, it's up to you and it depends on your app, but you might use pp/start/global.php if you don't have a better place to put it. It just have to be executed before your those views.
#Antonio's answer is a good way to do this. You can also use View::share(); to accomplish this with a shorter code.
View::share(array(
'foo' => 'bar'
));

Resources