Displaying a user's information to the user is pretty much straight forward using their id or name in the url as parameter and displaying specific details using {{ user.name }}, {{ user.phone }} etc. But how can i display a specific user info to other users. When i click on www.example.com/profile/joe, it takes me to joe's profile. Trying it logged out or from a different account throws up an error.
This what i have done so far. my profile url has
url = "/:id"
and my php block
[session]
==
<?php function onStart(){
$profile = $this->param('id');
}
?>
==
this links up to the profile page
<button style="font-family: arial" class="btn btn-sm btn-light rounded--lg-pill shadow rounded border col-6">View Profile</button>
using {{ user.name }}, {{ user.phone }} etc shows all the logged in user's info. I am just at lost here how to make the profile of a user available to other users.
Edit:
Currently working if i use $userProfile = Auth::findUserById($profile)
connected to <a href = ''{{ id: users.id }}''>View Profile></a>
It still works if I change it to
$userProfile = Auth::findUserByLogin($profile)
connected to
<a href = ''{{ id: users.username }}''>View Profile></a>
or
<a href = ''{{ id: users.email }}''>View Profile></a>
but i would have loved it to work using a different field
<a href = ''{{ id: user.company_name }}''>View Profile></a> instead of id or login details
Suppose we are generating link : www.example.com/profile/joe
so id will be joe now we need to find that user's record and use it
to find it in code
[session]
==
<?php function onStart(){
$profile = $this->param('id');
$this['userProfile'] = Auth::findUserByLogin($profile);
// you can use other field if you need
// $this['userProfile'] = \RainLab\User\Models\User::where('username', $profile)->first();
}
?>
==
use it in template
{{ userProfile.name }}, {{ userProfile.phone }}
if any doubts please add comment.
Related
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.
Hi there once again SO community. I've been developing a site and so far it's going pretty well. But today after a long day searching for a solution I can't understand nor find what the right path is...
I want to click on a button and a profile page where you can edit the fields appear. I can redirect to the page I want but I don't know how to send the user data so I can populate the fields.
Here is my button code on my view
<button class="btn btn-xs btn-warning dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false" style="border-color: black" id="dados_{{ $user->username }}"> Alterar Dados Pessoais
<i class="glyphicon glyphicon-cog"></i>
</button>
Here is the button AJAX request handler
if((this.id).indexOf("dados") != -1){
var content = this.id.replace("dados_", "");
$.get('callPermissions', {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
And here is my callPermission Controller
public function callPermissions(Request $request)
{
if($request->ajax()){
$usernames = Input::get('usernameSend');
if(isset($usernames)){
$user = User::Where('username', '=', $usernames)->first();
$returnHTML = view('userOptions.settings')->render();
return view('userOptions.settings');
}else{
Log::warning("Username não existe na base de dados.");
}
}
}
and here my Settings Controller
public function settings(Request $request)
{
return view('userOptions.settings');
}
And here is the route
Route::get('/callPermissions', 'SidebarController#callPermissions');
I know the controller is wrong and from what I've read I should verify if the AJAX is successful and if it is handle i on the AJAX request. But from what I've understand I'm not using the Controller at all (even though it goes there). How should I send the user information (in this case the username, then I can get everything from the database) and then send it to the view? I've been searching and trying out stuff that doesn't work...since the return view("your_view") on the Controller doesn't work.
Sorry if I've been confusing and if you need additional information feel free to ask!
Thanks for your help!!
Edit: If I return this on the controller
return view('userOptions.settings', compact('user'));
and do a replace with the Ajax request as show above and add this to the settings view
<p> {{ $user->name }} </p>
I get the following error Undefined variable: user (View: C:\wamp64\www\siteXL\ideiasxl\resources\views\userOptions\settings.blade.php)
Is there anyway to send the parameters with a compact alike or I need to send it through the link? Was avoiding to show the username on the url.
Edit2: For further clarification, this works as intended
<button onclick="window.location='{{url('/settings/' . $user->username)}}'" type="button" id="dadosPessoais" class="btn btn-default">Alterar Dados Pessoais
<i class="glyphicon glyphicon-wrench"></i>
</button>
but I was trying not to send id's and usernames through the URL.
If this is not achievable it's ok, but if there's a way I can't find it, that's why I'm asking
I think you have to add a parameter in the Route and receive the data in the controller function. I'd do something like this:
Route:
Route::get('/callPermissions/{user}', 'SidebarController#callPermissions');
Controller:
public function callPermissions(Request $request, $user)
{
//get data related to $user
}
Ajax call:
$.get('callPermissions/'+userIdVariable, {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
This would send the user id through the route.
To get the user id with JavaScript, you can make a hidden field in the Blade file and set the user id as the value. For example, if you using Form helper:
{{ Form::hidden('user_id', $user->id, array('id' => 'js-user-id')) }}
And then, in the JavaScript, you can get the value using something like this:
var userIdVariable = $('#js-user-id')->val();
I have this in Laravel
{{ link_to_route('users.publications.create', 'text of link', ['users' => $user->username]) }}
What it does is create a "< a >" HTML tag to a view in the site link this:
<a href="http://kinbu.co/users/luis02lopez/publications/create >text of link</a>
I want to have the same result in JQuery but it uses the "users" in the link route as other user:
var action_call = $({{ link_to_route('users.publications.create', 'publica uno como intercambio',
['users' => $user->username], ['style' => 'color:blueviolet']) }}
});
body.append(action_call);
With which I have the next error: http://imgur.com/a/Lrmbg
Do I need to create other var with the users name?
If any idea will be a great help. Thanks
A solution for get the logged (and principal) user information is:
Auth::user()->username
This bring the data of the logged in user.
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;
we are trying to get the value sent with the redirect method in blade template in laravel.
Here is how my return statement looks like:
public function movetotrash($id){
$page = Pages::where('id', $id) -> first();
$page -> active = 0;
//$page -> save();
//return redirect()->route('pages.index')->with('trash','Page Moved To Trash');
return redirect('pages')->with('trash','Page Moved To Trash');
}
Now once its redirect to pages it creates are URL like this http://localhost:8888/laravelCRM/public/pages.
Then i want to print the message to user that "Page is moved to trash" with $trash variable. For which i used this :
#if(isset($trash) ))
<div class="alert alert-error">
{{ $trash }}<br><br>
</div>
#endif
What should i do to print the value?
Thank you! (in advance)
The with method flashes data to the session which is only accessible in the next request.
So you can retrieve it from the session like this:
#if(session()->has('trash'))
<div class="alert alert-error">
{{ session()->get('trash') }}
</div>
#endif
In redirect with value pass as session flash,so you need to check value like that
#if(session()->has('trash'))
then you can show like this
{{ session('trash') }}
You can send data from controller like this,
You have to assign the message into variable
$trash = Page Moved To Trash';
return redirect('pages')->with('trash');
and then in view,
use like this.
#if(session($trash) ))
<div class="alert alert-error">
{{ session($trash) }}<br><br>
</div>
#endif
The data will store in session and we can use it on redirect page
Hope this will help :)