<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index($id)
{
$user = User::find($id);
$photos = $user->getPhotos();
$interests = $user->getInterests();
$gifts = $user->getGifts();
$visits = $user->totalVisits();
$likes = $user->totalLikes();
if ($user->id == Auth::user()->id) {
$page_name = 'My Profile';
return view('profile.my_profile', compact('page_name', 'user', 'photos', 'interests', 'likes', 'visits', 'gifts'));
} else {
$page_name = $user->name;
return view('profile.profile', compact('page_name', 'user', 'photos', 'interests', 'likes', 'visits', 'gifts'));
}
}
}
I have the following controller, which manages the "Profile" page in my web app. I feel like in my current code, there is a separation in a way that photos, interests, gifts, visits and likes look like they do not belong to the User.
How would I go to refactor all these different functions into one? Should I change the User Model's __construct to execute all of the functions? Ideally, I would like to pass only two variables to my view ("user" and "page_name" variables)
Here is my model, for reference:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function getPhotos()
{
return DB::table('photos')
->where('user_id', $this->id)
->get();
}
public function getInterests()
{
return DB::table('interests')
->where('user_id', $this->id)
->get();
}
public function getGifts()
{
return DB::table('gifts')
->join('sent_gifts', function ($join) {
$join->on('gifts.id', '=', 'sent_gifts.gift_id')
->where('user_2', $this->id);
})
->join('users', function ($join) {
$join->on('users.id', '=', 'sent_gifts.user_1');
})
->get();
}
public function getVisits()
{
return DB::table('user_reactions')
->join('users', function ($join) {
$join->on('users.id', '=', 'user_reactions.user_1')
->where('user_2', $this->id)
->where('reaction', 'visit');
})
->get();
}
public function getLikes()
{
return DB::table('user_reactions')
->join('users', function ($join) {
$join->on('users.id', '=', 'user_reactions.user_1')
->where('user_2', $this->id)
->where('reaction', 'like');
})
->get();
}
public function getFilter()
{
return DB::table('filters')
->where('user_id', $this->id)
->limit(1)
->get();
}
public function totalVisits()
{
return DB::table('user_reactions')
->where('reaction', 'visit')
->where('user_2', $this->id)
->get()
->count();
}
public function totalLikes()
{
return DB::table('user_reactions')
->where('reaction', 'like')
->where('user_2', $this->id)
->get()
->count();
}
}