How to create multi language menu in Laravel 5 - laravel

What is the best way to create a main menu in Laravel 5? And how to show menu items only when an user is logged-in? And What is the best way to make this multi language?

Laravel provides an easy way to check if the user is logged-in by using the facade Auth::check().
if (Auth::check()) {
// The user is logged in...
}
About the translation, you can check here: Localization
The structure is defined like this, as per documentation:
/resources
/lang
/en
messages.php
/es
messages.php
Laravel also provides an easy way to translate phrases using the trans('string.to.translate'), which can be seen here trans().
Inside messages.php (in both lang directories), you must set the translation string. In en/messages.php:
return [
'welcome' => 'Welcome'
];
In es/messages.php:
return [
'welcome' => 'Bienvenido'
];
With these two, you can do the following in you application for example:
// Get the user locale, for the sake of clarity, I'll use a fixed string.
// Make sure is the same as the directory under lang.
App::setLocale('en');
Inside your view:
// Using blade, we check if the user is logged in.
// If he is, we show 'Welcome" in the menu. If the lang is set to
// 'es', then it will show "Bienvenido".
#if (Auth::check())
<ul>
<li> {{ trans('messages.welcome') }} </li>
</ul>
#endif

Try with https://laravel.com/docs/5.1/authentication
For example:
if (Auth::check()) {
return something to view
}

Related

Passing View Variable to Included Layout Template in Laravel 8

This is probably something simple, but it's doing my head in.
So, my layout blade template has this:
#include('layouts.partials.sidebar')
{{ $slot }}
#include('layouts.partials.footer')
#include('layouts.partials.scripts')
I create a view which loads a template. This I assume gets parsed in $slot.
return view('request', [
'boo' => 'Hoo'
]);
No problems, the page loads and the variable 'boo' is accessible as {{ $boo }} in the 'requests' template.
But my question is, how can I pass the 'boo' variable to an included file in the layout file? In this case the following:
#include('layouts.partials.scripts')
So, in 'layouts.partials.scripts' how can I access {{ $boo }}? At the moment I just get an undefined index error.
Thank you very much for the help.
#include('layouts.partials.scripts', ['boo' => 'Hoo'])
Laravel Docs
https://laravel.com/docs/8.x/blade#including-subviews
If you have a partial like a nav, header, or sidebar, part of the master layout from which you are composing other views. It requires data that doesn't change from one view to another, like navigation links. Then, instead of passing the data from each controller method, you can define a view composer in a service provider's boot() method:
Service Provider's boot method
public function boot()
{
View::composer('layouts.partials.sidebar', function ($view) {
//$links = get the data for links
return $view->with('links', $links);
});
}
Laravel Docs
https://laravel.com/docs/master/views#view-composers

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

laravel 5.2 carousel only in index page doesn't wok

I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')

Customize #can() to show a permissions overlay for pages/sections

At work I maintain a fairly complex Laravel application which is still growing as new features are implemented and improved upon.
We have non-technical administrators in this system who manage other users permissions and sometimes it can be hard to know what permission ends up blocking a user from accessing a certain page or what might give a user too much access. Better descriptions for permissions and the ability to simulate a user to see what they have access to is already something we have done.
In addition to this we would like to toggle overlays for permissions defined in blade templates, we might defines this permissions with
#can('update', $post)
<!-- Menu button to update a $post -->
#endcan
or
#can('manage_user_roles_and_permissions')
<!-- A table with many different functions
for managing user roles + permissions -->
#endcan
Is there a way I can modify the way the #can() works in blade templates so that I can add some javascript to show a popover for where a section starts and ends, like "The permission 'Show Post' is needed for this menu button to show" or "To see the following section a user needs the 'Manage user roles and permissions' permissions". Or even better if I could add a div with a red border around the section.
How can I append additional javascript/html where #can() is used in a blade template to show an overlay.
To solve this issue I need to extend blade, see Extending Blade in the Laravel documentation.
The following is a quick test that I did just to see if this was possible. $value in this case is a string which contains the content of a blade file before being processed. So I can use preg_match_all() to find the #can statements and then append my javascript where needed. I can find the #endcan in the same way but it is harder to know which #endcan belongs to which #can but it should be fairly easy to match from this point on.
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Blade::extend(function($value)
{
$can_array = array();
preg_match_all('/(\s*)#(can)\(([^\)]*)\)(\s*)/', $value, $matches);
if (count($matches) > 0 && isset($matches[3])) {
foreach ($matches[3] as $match) {
if (!in_array($match, $can_array)) {
$can_array[] = $match;
}
}
}
foreach ($can_array as $ca) {
$value = str_replace("#can(" . $ca . ")", "#can(" . $ca . ") \r\n <!-- My javascript code goes here! -->", $value);
}
// TODO need to figure out a better way to handle this
$value = str_replace("#endcan", "#endcan \r\n <!-- Section ended here -->", $value);
return $value;
});
}
...
My source code now looks like this when viewing it, goal achieved!

Laravel Sub-menu Within View

Hi I am very new to Laravel and MVC frameworks in general and am looking to create a list of links (in a view within a template) that links to some content. I am using this to display a list of nine people and to display their profile description when the link is clicked on. I have created a model of what the page looks like at http://i.imgur.com/8XhI2Ba.png. The portion that I am concerned with is in blue. Is there a way to route these links to something like /about/link2 or /about?link2 while maintaining the same exact page structure but modifying the ‘link content’ section (on the right of the link menu) to show the specific link's content? I would greatly appreciate it if someone could point me in the right direction, as I have literally no clue where to go with this!
There are a couple ways you can go about doing this.
Templates
Create your route.
Im assuming a lot about your app here but hopefully you get the picture. If you need help with anything in particular, be sure to update your question with the code youve tried so it will be easier to help you.
Route::get('about/{page}', function($page)
{
$profile = Profile::where('name', $page)->first();
return View::make('about')->with('profile', $profile);
});
Modify Template.blade.php
Put this line where you wish for About.blade.php to appear.
#yield('content')
Create your view which will extend your template
#extends('Template')
#section('content')
<h2>User Profile</h2>
<ul>
<li>Name: {{ $profile->name }}</li>
<li>Last Updated: {{ $profile->updated_at }}</li>
</ul>
#stop
AJAX
This solution will utilize AJAX to grab the data from the server and output it on the page.
Route for initial page view
Route::get('about', function($page)
{
$profiles = Profile::all();
return View::make('about')->with('profiles', $profiles);
});
Feel free to follow the same templating structure as before but this time we need to add some javascript into the template to handle the AJAX. Will also need to id everything which needs to be dynamically set so we can easily set it with jquery.
#extends('Template')
#section('content')
<h2>Links</h2>
#foreach($profiles as $profile)
{{ $profile->name }}
#endforeach
<h2>User Profile</h2>
<ul>
<li>Name: <span id="profile_name">{{ $profile->name }}</span></li>
<li>Last Updated: <span id="profile_updated_at">{{ $profile->updated_at }}</span></li>
</ul>
<script>
function setProfile(a)
{
$.ajax({
method: 'get',
url: 'getProfile',
dataType: 'json',
data: {
profile: $(a).data('id')
},
success: function(profile) {
$('#profile_name').html(profile.name);
$('#profile_updated_at').html(profile.updated_at);
},
error: function() {
alert('Error loading data.');
}
});
}
</script>
#stop
Route to handle the AJAX request
Route::get('getProfile', function()
{
$profile_id = Input::get('profile');
$profile = Profile::find($profile_id);
return $profile->toJson();
});
Now, the page should not have to reload and only the profile information should be updated.
Making some assumptions here as no code posted and assuming you're using the latest version of Laravel, Laravel 5.
Lets say you have a table in your database named users and you have a Model named Users (Laravel 5 comes with the Users model as default, see app/Users.php). The users will be the base of our data for the links.
Firstly, you want to register a route so you can access the page to view some information. You can do this in the routes file. The routes file can be found here: app/Http/routes.php.
To register a route add the following code:
Route::get('users', ['uses' => 'UserController#index']);
Now what this route does is whenever we hit the URL http://your-app-name/public/users (URL might be different depending on how you have your app set up, i.e. you may not have to include public) in our web browser it will respond by running the index method on the UserController.
To respond to that route you can set up your UserController as so:
<?php namespace App\Http\Controllers;
class UserController extends Controller {
public function index()
{
}
}
Controllers should be stored in app/Http/Controllers/.
Now lets flesh out the index method:
public function index()
{
// grab our users
$users = App\Users::all();
// return a view with the users data
return view('users.index')->with('users');
}
This grabs the users from the database and loads up a view passing the users data.
Here's what your view could look like:
<!DOCTYPE html>
<html>
<head>
<title>Users Page</title>
</head>
<body>
#foreach($users as $user)
<a href="{{ URL::route('user', ['id' => $user->id]) }}">
{{ $user->username }}
</a>
#endforeach
</body>
</html>
The view code will loop through each user from the $users data we passed to the view and create a link to their user page which is different for each user based on their id (their unique identifier in the DB)
Due to the way I've named it, this would be found in app/views/users/index.blade.php - if you save files ending in blade.php you can use Laravel's templating language, blade.
Now you need to finally set up another route to respond to a user page, for example http://your-app-name/public/user/22.
Route::get('user/{id}', ['uses' => 'UserController#show']);
Then add the show method to UserController
public function show($id)
{
// this will dump out the user information
return \App\User::find($id);
}
Hope that helps a little! Wrote most of it off the top of my head so let me know if you get any errors via comment.
This question is very bare, and it is difficult to actually help your situation without you showing any code. Just to point you in the right direction though, here is what you would need.
A Model called People, this is how you will access your data.
A controller. In this controller you will do the following
Get the ID of the profile you want from the functions parameters
Find that persons information e.g. People::find($person_id);
return the profile view with that persons data e.g. return view('profile')->with('person', $person);
In your view you can then use that data on that page e.g. {{ $person->name }}
For the page that needs to display the links to the people you would have a method in your controller which..
Get all the people data e.g. People::all();
Return a view with that data return view('all-people')->with('people', $people);
You will then need a route to access an individual person. The route will need to pass the persons ID into a controller method e.g.
Route::get('get-person/{id}',
[ 'as' => 'get-person',
'uses' => 'PeopleController#getPerson' ]);
You can then use this route in your view to get the links to each person
#foreach($people as $person)
{{$person->name}}
#endforeach
This would produce the list of links you want.

Resources