I am trying to implement a watchlist feature on my job site application. I am abit confused as to the exact eloquent queries to use to fetch the data from the job_listings table. Currently the user can add a job to their watchlist successfully.
What I want to be able to do:
View all of the jobs that the user has put in their respective watchlists with all the details e.g the Job position, description etc.
Current DB layout:
Routes:
Bigger pic of routes here
WatchlistController
<?php namespace App\Http\Controllers;
use App\Watchlist;
use App\JobListing;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use Redirect;
class WatchlistController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
// Need to run queries in here to get the
$user = User::with('watchlist')->find(\Auth::id());
var_dump($user);
// Then return my main watchlist view which will send through an
// array so I can loop through it and display what the user
// has put in their watchlist
// return view('watchlist/viewWatchlist');
}
/**
* Adds the username of the user that is currently logged in
* and adds the job listing ID that the user clicks on to the Watchlist table.
*/
public function addToWatchlist($id)
{
$createItemInWatchlist = Watchlist::create([
'user_id' => Auth::user()->id,
'job_id' => $id
]);
return Redirect::to('watchlist');
}
Watchlist Model
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Watchlist extends Model {
protected $table = 'watchlist';
protected $fillable = ['user_id', 'job_id'];
}
JobListing Model
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class JobListing extends Model {
protected $table = 'job_listings';
protected $fillable = ['user_id', 'position_title', 'description', 'category', 'job_type',
'city','company_name','company_url','logo_path','how_to_apply', 'expiry_date', 'status'];
}
User Model
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['username','firstname', 'lastname', 'email', 'password', 'role'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function watchlist() {
return $this->hasMany('\App\JobListing', 'watchlist', 'user_id', 'job_id');
}
}
This is not the (direct) answer, but you may take a look at the eloquent relations -> http://laravel.com/docs/5.0/eloquent to see the advantages.
If you could use ids for your users, the solution looks like this: The watchlist table is the connection between the user and your jobs. Each user can track multiple jobs, so there is a belongsToMany relation between them. BelongsToMany takes up to four parameter:
your Model that you would like to "connect" to and
sql table that includes both ids of the user and the job to link between user and jobs
local id (user)
foreign id (job)
More here: http://laravel.com/docs/5.0/eloquent#many-to-many. Extend your user model with the following relation:
public function watchlist() {
return $this->belongsToMany('\App\JobListing', 'watchlist', 'user_id', 'job_listing_id');
}
To get the watchlist, you can use (eager loading):
$user = User::with('watchlist')->find(\Auth::id());
To access to watchlist items, use $user->watchlist
Alternatively you can retrieve the watchlist directly in the template from the user by using:
#foreach(Auth::User()->watchlist as $job)
{{$job}}
#endforeach
To read more about the relations, take a look at this: https://laracasts.com/series/laravel-5-fundamentals -> part 14, relations and something to read (version 4) http://daylerees.com/codebright/eloquent-relationships
Related
I'm building a Laravel 8 API and want to automatically join user_settings onto a user whenever the User model is queried.
My thinking is that I can achieve this with the belongsTo relationship since user_settings "belongs" to a user.
However, when I attach this to my UserSetting model and query a user I'm not seeing any user settings attached to my User despite having data in the user_settings table.
Where am I going wrong?
Model: User
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UserSetting extends Model
{
use HasFactory, SoftDeletes;
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'user_settings';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'user_id',
'theme',
'refreshButtonPlacement',
'animationSpeed',
'fetchTimeout'
];
/**
* Get the user that owns the comment.
*/
public function user()
{
return $this->belongsTo(UserSetting::class);
}
}
Model: User
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password'
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime'
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* #return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* #return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
I also tried using a One To One relationship and defined a settings method on my User model but in Tinker when I ran User::findOrFail(1)->settings; I had nothing either.
Relationship setup:
class User extends Model
{
//some custom stuff
/**
* Get the phone associated with the user.
*/
public function user_setting()
{
return $this->hasOne(UserSetting::class);
}
}
class UserSetting extends Model
{
//some custom things
/**
* Get the user that owns the comment.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Afterwards you can use eager laoding by default, in your case you will have to add $with = ['user_setting'] to your User class.
You could also use the ->with() method, for that you will have to use either:
User::with('user_setting')->find(Auth::id());
//or
Auth::user()->with('organisation')->first()
Laravel doesn't load the relationship values in every call because of the obvious overhead. So you will either define the relationship to be loaded by default or you will have to work with the ->with() method for eager loading the relationship.
Add this method to your User model
And you can access the user settings through a dynamic attribute $user-> user_setting
on each User model instance
For more informations
https://laravel.com/docs/8.x/eloquent-relationships#one-to-one
public function user_setting(){
return $this->hasOne(UserSetting::class);
}
I have 2 tables and 1 pivot table with many to many relationship. However the relationship only works for the first record, for the second record onwards, the relationship can't be detected.
These are my tables. Roles, Admins and my pivot table is admin_role.
Model
Admin.php
<?php
namespace App;
use App\Role;
use App\Notifications\AdminResetPasswordNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
use Notifiable;
//Send Notification
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new AdminResetPasswordNotification($token));
}
/**
* 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',
];
/**
* Relationships
*/
public function role()
{
return $this->belongsToMany(Role::class)->using('App\RoleAdmin');
}
}
Role.php
<?php
namespace App;
use App\Admin;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function admin()
{
return $this->belongsToMany(Admin::class)->using('App\RoleAdmin');
}
}
RoleAdmin.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
class RoleAdmin extends Pivot
{
protected $table = 'admin_role';
protected $fillable = ['admin_id' , 'role_id'];
}
So the problem right now is
$admin = App\Admin::find(1);
$admin->role()->get();
When I run the above method, I can retrieve back record.
Same for this
$role = App\Role::find(1);
$role->admin()->get();
However for this,
$admin = App\Admin::find(2);
$admin->role()->get();
And
$role = App\Role::find(2);
$role->admin()->get();
There are no records.
UPDATE : AdminRoleTable looks like this
id admin_id role_id
1 1 1
2 2 2
I'm creating a school platform where students, teachers,... can login using their credentials. To reduce duplicate data I did not make a separate table called students, instead I keep all the data in the users table.
To know if a user is a student I a have a table that is called enrolments, in this table a user_id , schoolyear_id and class_id is stored.
I already made a student model that refers to the users table, but how can I ensure that this model only passes students?
EER:
Student.php:
<?php
namespace App;
class Student extends User
{
protected $table= 'users';
public function enrollments(){
return $this->belongsToMany(Enrollment::class);
}
}
User.php:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Support\Facades\Auth;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name','last_name', 'password'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profiles(){
return $this->hasOne(Profile::class);
}
}
What I want to achieve is that when I call the Student::all(); function I get all the users who are enrolled in the school,hence students.
Check out model events: https://laravel.com/docs/5.5/eloquent#events
You should be able to drop this into your student model for a test:
protected static function boot(){
parent::boot();
static::retrieved(function($thisModel){
if($thisModel->isNotAStudent or whatever logic you need){
return false;
}
}
}
I'm still on 5.4, which does not have the retrieved model event built in, but returning false generally stops the call from going through. So applying that logic to the retrieved event may stop that model instance from being returned if it is not a student, but allow students to be returned. Just a thought.
Your provided solution lead me in the right direction. My problem is solved by using global scope:
<?php
namespace App;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class Student extends User
{
protected $table= 'users';
protected static function boot()
{
parent::boot();
static::addGlobalScope('student', function (Builder $builder) {
$builder->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('enrollments')
->whereRaw('enrollments.user_id = users.id');
});
});
}
public function enrollments(){
return $this->belongsToMany(Enrollment::class);
}
}
I am using Laravel 5 to build a user based application. Some models have a manyToMany relationship in my app and therefore I am using pivot tables.
When I delete a user from the system, I use this simple function:
/**
* Delete user.
*
* #param $id
* #return mixed
*/
public function deleteUser($id)
{
return $this->user->whereId($id)->delete();
}
However, when the user is deleted, the rows in the pivot tables (for example role_user) do not get deleted.
I have read on the laravel site that I can use model events to "clear up" my pivot tables, but i'm really unsure how I would implement that.
Can anyone point me in the right direction?
Edit
Below is my current model setup:
namespace App\Models\User;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Scopes\MultiTenantTrait;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, MultiTenantTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['cust_id', 'first_name', 'last_name', 'email', 'status', 'activation_code'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Boot the model.
*
*/
public static function boot()
{
parent::boot();
static::deleting(function($user)
{
$user->roles()->delete();
$user->supervisors()->delete();
$user->types()->delete();
$user->rates()->delete();
$user->miscs()->delete();
});
}
...
You can add a boot method to your models, like the following:
public static function boot() {
parent::boot();
// This is a deleting event on the model
static::deleting(function($model) {
$model->... //Here your model is still available
// You could add something like this
DB::table('role_user')->where('user_id', $model->id)->delete();
})
}
But you can also extend the delete method in your models:
public function delete() {
DB::table('role_user')->where('user_id', $this->id)->delete();
parent::delete();
}
I have the following tables:
User: [id,username,password]
Producer: [id,user_id,...]
Admin: [id,user_id,...]
I've configured Authentication through User table.
So, if I get logged in when I execute Auth::user() I only can access to the user's fields. However, I would need be able to get producer's fields. I have to say that, a user can be Producer or Admin but not both.
Do you know good documentation or something to do it? Thanks a lot.
You class should hasMany relation to it
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function producers()
{
return $this->hasMany('Producer');
}
}
$user = Auth::user();
print_r($user->producers);