pass auth::user as data variable with route - php

I want that when the user click the profile page i want to pass Auth::user()->username as argument to my userController's show method.I have the profile link as following:
<li>Profile</li>
And in my route i have the following route
Route::get('/profile/{username}',function(){
return View::make('user.show')->with($username);
});
my question is how i can set username in my '/profile/{username}' as Auth::user()->username when i click the profile link?currently the profile link does not attach any parameter with it

First of all
{{URL::to('/profile')}} is not pointing to Route::get('/profile/{username}) url,there are two different routes
So what you need to do is either change the link , i.e.
{{URL::to('/profile/' . \Auth::user()->username)}}
and then in your route file
Route::get('/profile/{username}',function($username){
return View::make('user.show')->with(['username' => $username]);
});
//note that you need to pass the array in with() method
or you can do this
Route::get('/profile/{username}',function($username){
return View::make('user.show',compact('username'));
});

When the user clicks on profile link:
<li>
My Profile
</li>
The UserController#show method is called.
<?php
// routes.php
Route::get('profile/{username}', 'UserController#show')->name('user.show');
// UserController.php
public function show($username)
{
$user = User::whereUsername($username)->first();
return view('user.show', compact('user'));
}
and a View response is returned to the user.
#update
If you need is just redirect the control to the UserController#show method, you can do this:
<li>
My Profile
</li>
<?php
// routes.php
Route::get('profile/{username}', function ($username) {
return redirect()->route('user.show', Auth::id());
})->name('user.profile');
Now if you want customize the UserController#show action:
<li>
My Profile
</li>
The UserController#show method is called.
<?php
// routes.php
Route::resource('user', 'UserController', ['except' => ['show']);
Route::get('profile/{username}', 'UserController#profile')->name('user.profile');
Now you can delete the UserController#show method if you want or change the profile method name to show.
// UserController.php
public function profile($username)
{
$user = User::whereUsername($username)->first();
return view('user.show', compact('user'));
}

A quick way is to setup a redirect from /profile and it won't break the functionality if they want to view someone else's profile.
Route::get('/profile',function(){
return Redirect::to('/profile/'.Auth::user()->username);
}
However, I'd recommend doing an Auth::check() before the redirect.

i did something like the following
<li>Profile</li>
and in route.php:
Route::get('/profile',function(){
return redirect()->route('user.show',[Auth::user()->username]);
});

Related

Undefined variable problem on Laravel 9.x

I'm trying to get a my title variable from my control page and display it on the about page.
I don't think I have a typo, but it might me. I'm not sure.
Here is my control page code;
class PagesController extends Controller
{
public function index(){
$title = 'Welcome to Laravel';
return view ('pages.index')->with('title', $title);
}
public function about(){
$title = 'About us';
return view ('pages.about')->with('title', $title);
}
public function services(){
$title = 'The services';
return view ('pages.services')->with('title', $title);
}
}
In this page, the index and services functions work fine, but I can't get the about page.
Here is my display pages;
This is Index page
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the Laravel Application</p>
#endsection
This is the about page:
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the About page</p>
#endsection
The error I have
Do this:
return view ('pages.index', compact('title'));
or:
return view ('pages.index', [
'title' => $title
]);
Since you are returning just the title, there isn't any need to call any verbs. Rather you should directly call the view:
route::view('/about', 'Pagecontroller#about');
or
pass the parameter by compact:
return view ('pages.index', compact('title'));
or
return view ('pages.index', ['title' => $title]);
Since this is a test application from a lesson, I forgot to delete some extra code in my route file.
This is my route file:
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#about');
Route::get('/services', 'App\Http\Controllers\PagesController#services');
The commented area shouldn't be here. That was the whole problem over here...
// Route::get('/about', function(){
// return view ('pages.about');
// });
This form of passing variables is a short-lived entry of a variable into the session. Then accessing the variable on the page should look like this:
{{ session('title') }}
If you want to pass data to the view, then you need to use the method
return view('pages.services', ['title' => $title]);
Laravel views

How can I show items belonging to a logged in user in Laravel?

I want to show the emails that the logged in user has sent from the database
This is the route code:
Route::get('/myEmails/{id}','PagesController#myEmailsShow');
This is the function in the controller:
public function myEmailsShow($id)
{
$myEmails = DB::table('emails')->where('user_id',$id)->get();
return view('content.myEmails', compact('myEmails'));
}
This is the a link where the user click to open the page:
#if(Auth::check())
<a class="nav-link text-white" href="/myEmails/{id}"> my emails</a>
#endif
And here where i want to show the data (i am showing only the name for test):
<div class="row">
#foreach($myEmails as $myEmail)
{{$myEmail->name}}
#endforeach
</div>
I think the best way to accomplish your goals here would be using a hasMany relationship between User and Emails (if emails is a Model).
//User.php
public function emails()
{
return $this->hasMany('App\Models\Email');
}
In the controller, apply the Auth middleware to the myEmailsShow method in a constructor:
//PagesController.php
public function __construct()
{
$this->middleware('auth')->only(['myEmailsShow']);
}
Then, in your myEmailsShow method, do something like the following:
//PagesController.php
public function myEmailsShow()
{
// Middleware Eliminates the need for ID in the function.
$user = auth()->user();
$myEmails = $user->emails;
return view('content.myEmails', compact('myEmails'));
}
You can remove the ID parameter from the route and just make it something like Route::get('/myEmails', 'PagesController#myEmailsShow');. Only users who are logged in will be able to access this page, and they will only see emails belonging to them.
Route::get('/myEmails/{user}','PagesController#myEmailsShow')->name('myemails');
with the controller
use App\Email;
use App\User;
public function myEmailsShow(User $user)
{
///calling the model Email at parameters instead of $id eloquent automatically the data from DB
$myEmails = Email::where('user_id',$user->id)->get();
return view('content.myEmails')->with('myEmails', $myEmails);
}
The link has little modifications
#if(Auth::check())
<a class="nav-link text-white" href="{{route('myemails', $user->id)}}"> my emails</a>
#endif
displaying the value
#foreach($myEmails as $myEmail)
{{$myEmail->name}}
#endforeach

Laravel5 - After loggin in Auth method doesn't work

I have problem with Auth method from Laravel5. After logging in, in my website, the HTML doc cannot see that "I am logged".
#if(Auth::user())
--><li class="userNav__item">
<a href="#" class="userNav__link">
<i class="icon-user-add userNav__image"></i>
Hello, {{{ Auth::user()->name }}}
</a>
</li>
#endif
There is my "Loggin in" Controller
public function SignIn(Request $request){
if(Auth::attempt(['email'=>$request->email, 'password'=>$request->password])){
return redirect('mainPage');
}
else{
return back()->withInput()->withErrors(['email' => 'Zły email lub hasło']);
}
}
And routes.php file
http://pastebin.com/68CB0r7c <-- cannot post it in "Code" element..
And my question. Why Auth method in HTML doc is not working? What am i doing wrong? :(
One more ( it's funny... )
When i do something like this
public function SignIn(Request $request){
if(Auth::attempt(['email'=>$request->email, 'password'=>$request->password])){
return view('layout.index');
// return redirect('mainPage');
}
else{
return back()->withInput()->withErrors(['email' => 'Zły email lub hasło']);
}
}
The Auth::user method in HTML doc is working, but when i use "mainPage" route it's not working. LOL?
Your form must be post but you
mainPage route is get
change it to post or any
Route::any('mainPage', function () {
return view('layout.index');
});
This might solve your issue.
For authentication in Laravel most is done for you just have a look at
https://laravel.com/docs/5.2/authentication#authentication-quickstart
Try defining a construct in your mainpage controller like this
public function __construct()
{
$this->middleware('auth');
}

Controller Method not defined error - Laravel 5.1

I am trying to display the currently logged in username, as a link to the user info, in my main navigation. I can get the name to display, so I am pulling the info from the db, and passing it to the view OK. But, when I try to make it a link, I get the method not defined error.
Here is how I pass the user info to the navigation (as the var $userInfo):
public function index()
{
$Clients = \Auth::user()->clients()->get();
$userInfo = \Auth::user();
return view('clients.index', compact('Clients', 'userInfo'));
}
Here is the relevant bit from my navigation:
<ul class="nav navbar-nav">
<li>{!! link_to_action('AuthController#show', $userInfo->username, [$userInfo->id]) !!}</li>
</ul>
The method from my controller:
protected function show($id)
{
$userInfo = User::findOrFail($id);
return view('users.show', compact('userInfo'));
}
And, the route definition:
// User Display routes
Route::get('auth/{id}', 'Auth\AuthController#show');
Here is the error I get:
Action App\Http\Controllers\AuthController#show not defined.
Can anyone tell me what I am missing?
First, you need to make your AuthController::show() method public:
public function show($id)
{
$userInfo = User::findOrFail($id);
return view('users.show', compact('userInfo'));
}
Second, as your controllere is in App\Http\Controllers\Auth namespace, you need to use the **Auth** prefix in the view:
<ul class="nav navbar-nav">
<li>{!! link_to_action('Auth\AuthController#show', $userInfo->username, [$userInfo->id]) !!}</li>
</ul>

How to set URL so it loads the user page

In route.php I have the following...
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController#user'
));
In ProfileController I have the following...
class ProfileController extends BaseController {
public function user($username) {
$user = User::where('username', '=', $username);
if($user->count()) { // if the corresponding user exists...
$user = $user->first();
return View::make('profile.user')
->with('user', $user);
}
return App::abort(404);
}
}
In navigation.blade.php I have the following...
<li>User Profile</li>
How can I make it so that navigation.blade.php will provide the correct link to the user profile? At the moment the link looks like the following in html...
http://website.dev/user/%7Busername%7D
I'd like it to look like this:
http://website.dev/user/currentlyLoggedInUserName
You can try using HTML::link. Example:
{{ HTML::link('/user/<?=$user_name?>', 'User Profile')}}
You should use
<li>User Profile</li>
because it is a Laravel PHP function url() that must resolve first, and the result is passed to Handlebars.
This is how I solved it. I'll leave the answer here for other people.
<li>User Profile</li>

Categories