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');
}
Related
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
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
i want to send filter request to show only my discussions
it's my route
Route::resource('/forum','ForumsController');
<div class="list-group-item">
My Discussions
</div>
its my ForumController
switch (request('filter'))
{
case 'me':
$discussions = Discussion::where('user_id',Auth::id())->paginate(3);
}
Found a solution to send link :)
Home
This make the route like below:-
http://localhost/forum/public/forum?filter=me
If you user Route::resource function, it has default route name .
Route web.php
Route::resource('/forum','ForumsController');
View.php
<div class="list-group-item">
My Discussions
</div>
Controller.php
public function index(Request $request){
switch ($request->filter){
case 'me':
$discussions = Discussion::where('user_id', Auth::id())->paginate(3);
}
return view('View.php', compact('discussions'));
}
When the user save the object in the blade view, a function into my controller execute with post request. If the save was success I want to reload the page with the changes.
So I need to call index() again to get the refreshed data. Also, I want to show a success message. So the steps are:
index() function execute so the user can show his items.
the user choose an item and save it
save(Request $request) is called
everything save fine
save function call index() with sending a parameter with the flash success message?
I have this structure in my controller:
public function index(){
...
return view('agenda')->with(['agenda'=>$response]);
}
public function save(Request $request){
...
// here where I need to return index function with success message
}
How can I solve my problem?
Assuming the save action was called from the index page, simply redirect back
public function save(Request $request){
...
return redirect()->back()->with('message', 'Item saved');
}
If it was called from a different page, redirect to index page. Assuming your index() function matches a route /:
return redirect()->to('/')->with('message', 'Item saved');
Then display the flash message in your index view.
#if(Session::get('message'))
{{ Session::get('message') }}
#endif
For styling, if you are using bootstrap you can do
#if(Session::get('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
#endif
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]);
});