why profile link redirects to homepage? - php

i'm trying to view the profile page of my website and keeps redirecting me to homepage instead of vendors profile
No visible error is been displayed.
can someone help please
here is the link i'm trying to view https://booksafariafrica.com/en/profile/32
this is the code
<div class="owner-info widget-box">
<div class="media">
<div class="media-left">
<a href="{{route('user.profile',['id'=>$vendor->id])}}" target="_blank" >
#if($avatar_url = $vendor->getAvatarUrl())
<img class="avatar avatar-96 photo origin round" src="{{$avatar_url}}" alt="{{$vendor->getDisplayName()}}">
#else
<span class="avatar-text">{{ucfirst($vendor->getDisplayName()[0])}}</span>
#endif
</a>
</div>
My controller
<?php
/**
* Created by PhpStorm.
* User: h2 gaming
* Date: 8/17/2019
* Time: 3:05 PM
*/
namespace Modules\User\Controllers;
use App\User;
use Illuminate\Http\Request;
use Modules\FrontendController;
class ProfileController extends FrontendController
{
public function profile(Request $request,$id){
$user = User::find($id);
if(empty($user)){
abort(404);
}
if(!$user->hasPermissionTo('dashboard_vendor_access'))
{
return redirect('/');
}
$data['user'] = $user;
$data['page_title'] = $user->getDisplayName();
$this->registerCss('module/user/css/profile.css');
return view('User::frontend.profile.profile',$data);
}
My routes
Route::group(['prefix'=>'profile'],function(){
Route::match(['get'],'/{id}','ProfileController#profile')->name("user.profile");

if(!$user->hasPermissionTo('dashboard_vendor_access'))
{
return redirect('/');
}
Your problem is probably being caused by this code block, as it does a redirect to the homepage if the condition is met.
So make sure the $user has the proper permissions assigned to it.

wow, thank's for giving me proper solution
the problem was
if(!$user->hasPermissionTo('dashboard_vendor_access'))
{
return redirect('/');
}
then have to remove the Exclamation mark and worked just fine.
Thank you very much

Related

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

Route [/forum?filter=me] not defined

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'));
}

How to output 'Favourites/Likes' list in Laravel 5.3?

I have implemented a like/favourite function in my application, where a user can favourite a Charity - this is then stored in a table in my database - which is working reasonably well.
How would I go about outputting the users favourites list to them on their profile page?
Profile View (Where I want to output):
<div class="favourite_section">
<div class="col-md-5 pull-right">
<div class="panel panel-default">
<div class="panel-heading">
Edit
<h3 class="panel-title"> Your Favourites </h3>
</div>
<div class="panel-body">
<!-- Output users' favourites. -->
<h4> </h4>
</div>
</div>
</div>
Like Controller:
<?php
namespace App\Http\Controllers;
use App\Like;
use Illuminate\Support\Facades\Auth;
class LikeController extends Controller
{
public function likePost($id)
{
$this->handleLike('App\charity', $id);
return redirect()->back();
}
public function handleLike($type, $id)
{
$existing_like = Like::withTrashed()->whereCharityImg($type)->whereCharityDesc($type)->whereCharityName($type)->whereCharityId($id)->whereUserId(Auth::id())->whereId($id)->first();
if (is_null($existing_like))
{
Like::create([
'id' => $id,
'user_id' => Auth::id(),
'charity_id' => $id,
'charity_name' => $type,
'charity_desc' => $type,
'charity_img' => $type
]);
}
else
{
if (is_null($existing_like->deleted_at))
{
$existing_like->delete();
}
else
{
$existing_like->restore();
}
}
}
}
You're able to retrieve the currently logged in user with Laravel's Authentication Facade or, if you prefer using helpers, auth()->user(). In a similar vein, you can do Auth::id() to skip the object and get the user's id directly. In your controller, you'll want to query the Like model where the user_id of the Like is equal to the user id of the logged in user:
$likes = Like::whereUserId(Auth::id())->get();
This will return a collection of likes which should be passed to the view from the controller, like so:
return view('profile', compact('likes'));
Replace 'profile' with whatever the view file for the profile page is. Then in the Blade template, you'll have access to a $likes variable, which you can iterate over:
#foreach($likes as $like)
// Do what you want with each like
#endforeach
Your situation is called many to many relationship.
Laravel docs
Laravel makes it easy. You should write a method to you User model.
Something like:
public function favourites() {
return $this->belongsToMany('App\Charity', '[Like model table name]', 'user_id', 'charity_id');
}
You then can use it like: $user->favourites and it will return collection of favourite charities.
You can read docs if you want.

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');
}

Let Authenticated User view the profile of other users in Laravel 4 using Sentry 2

I'm sort of a newb to Laravel 4 and Sentry 2, but i've managed to survive so far. I'm running into a problem right now, cause when im logged in as userid(1) and i want to view the profile of userid(2) i'm just seeing the information of userid(1) on the profile of userid(2).
I'm aware that using filters might come in handy at this, but if i have to be honest. I have no idea what i should look at etc.
I know this site is not meant for giving answers. But if someone could give me a bit of an answer, where to look, what i should keep in mind etc. that would be very much appreciated.
---EDIT---
Route:
Route::group(array('before'=>'auth'), function(){
Route::get('logout', 'HomeController#logout');
Route::get('profile/{username}', 'ProfileController#getIndex');
});
Route::filter('auth', function($route)
{
$id = $route->getParameter('id');
if(Sentry::check() && Sentry::getUser()->id === $id) {
return Redirect::to('/');
}
});
ProfileController
public function getIndex($profile_uname)
{
if(Sentry::getUser()->username === $profile_uname) {
// This is your profile
return View::make('user.profile.index');
} else {
// This isn't your profile but you may see it!
return ??
}
}
View
#extends('layouts.userprofile')
#section('title')
{{$user->username}}'s Profile
#stop
#section('notification')
#stop
#section('menu')
#include('layouts.menus.homemenu')
#stop
#section('sidebar')
#include('layouts.menus.profilemenu')
#stop
#section('content')
<div class="col-sm-10 col-md-10 col-xs-10 col-lg-10">
<div class="panel panel-info">
<div class="panel-heading"><h3>{{ $user->username }}</h3></div>
</div>
</div>
#stop
#section('footer')
#stop
This might work for you:
<?php
public function getIndex($profile_uname)
{
if(Sentry::getUser()->username === $profile_uname) {
// This is your profile
return View::make('user.profile.index');
} else {
// This isn't your profile but you may see it!
return View::make('user.profile.index')->with('user', Sentry::findUserByLogin($profile_uname));
}
}
If username is not your login column, then you can do it in two steps:
$userId = \Cartalyst\Sentry\Users\Eloquent\User::where('username', $profile_uname)->first()->id;
return View::make('user.profile.index')->with('user', Sentry::findUserById($userId));
If you have a User model tied to your users table, you can just do:
$userId = User::where('username', $profile_uname)->first()->id;
return View::make('user.profile.index')->with('user', Sentry::findUserById($userId));
And in this last case you probably will be able to use that same model, since they would be the same in Sentry and pure Eloquent:
$user = User::where('username', $profile_uname)->first();
return View::make('user.profile.index')->with('user', $user);
Also, to avoid a conflict between your views related to the current logged user, you should rename the $user variable you are instantiating via View::share() or View::composer() from $user to $loggedUser or something like that.

Categories