Sto costruendo un’app di tipo Twitter. C’è un feed in cui voglio mostrare solo i post degli utenti che seguo.
Ho provato tutto con join, ma niente sembra funzionare.
Ho 3 tabelle: Users
, Followers
, Shares
Le tabelle hanno questo aspetto:
Utenti : id
Seguaci : user_id
, follower_id
Condivisioni : user_id
Quello che devo ottenere è “TUTTE le condivisioni DOVE share.user_id = followers.follower_id” “ANDWHERE followers.user_id = users.id”
Supponiamo che users.id sia 3, ho provato questo:
$shares = DB::table('shares') ->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id') ->leftjoin('users', 'followers.user_id', '=', 'users.id') ->where('users.id', 3) ->where('shares.user_id', 'followers.follower_id') ->get();
Ma non funziona.
Qualsiasi aiuto è apprezzato 🙂
Credo che il tuo intervento sia sbagliato:
$shares = DB::table('shares') ->join('users', 'users.id', '=', 'shares.user_id') ->join('followers', 'followers.user_id', '=', 'users.id') ->where('followers.follower_id', '=', 3) ->get();
Ti suggerisco anche di dare un nome al tuo tavolo come follows
, invece, è un po ‘più naturale dire che l’ user has many followers through follows
e user has many followees through follows
user has many followers through follows
user has many followees through follows
.
Esempio
$shares = DB::table('shares') ->join('users', 'users.id', '=', 'shares.user_id') ->join('follows', 'follows.user_id', '=', 'users.id') ->where('follows.follower_id', '=', 3) ->get();
Non avevo capito che stavi usando DB::
queries e non modelli. Quindi sto correggendo la risposta e fornendo molta più chiarezza. Ti suggerisco di usare i modelli, è molto più facile per quelli che iniziano con il framework e specialmente con SQL.
Esempio di modelli:
class User extends Model { public function shares() { return $this->hasMany('Share'); } public function followers() { return $this->belongsToMany('User', 'follows', 'user_id', 'follower_id'); } public function followees() { return $this->belongsToMany('User', 'follows', 'follower_id', 'user_id'); } } class Share extends Model { public function user() { return $this->belongsTo('User'); } }
Esempio di utilizzo del modello:
$my = User::find('my_id'); // Retrieves all shares by users that I follow // eager loading the "owner" of the share $shares = Share::with('user') ->join('follows', 'follows.user_id', '=', 'shares.user_id') ->where('follows.follower_id', '=', $my->id) ->get('shares.*'); // Notice the shares.* here // prints the username of the person who shared something foreach ($shares as $share) { echo $share->user->username; } // Retrieves all users I'm following $my->followees; // Retrieves all users that follows me $my->followers;
In termini di syntax generale di MySQL, è meglio scrivere:
SELECT * FROM USER a JOIN FOLLOWERS b ON (a.id = b.user_id) JOIN SHARES c on (b.follower_id = c.user_id) WHERE a.id = 3
restituirà un set di dati di tutti i follower e le loro rispettive condivisioni.
Credo che vorrai quanto segue in Laravel
DB::table('USER') ->join('FOLLOWERS', 'USER.id', '=', 'FOLLOWERS.user_id') ->join('SHARES', 'FOLLOWERS.follower_id', '=', 'SHARES.user_id') ->where('USER.id', 3) ->get();
$data[shares] = DB::table('shares') ->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id') ->leftjoin('users', 'users.id', '=', 'users.id') ->where('users.id','=', 3) ->get();
per vedere i risultati.
print_r($data[shares]);die;
per altre query Basta dare una descrizione della tua tabella
Invece di
->where('shares.user_id', 'followers.follower_id')
Dovrebbe essere
->whereRaw('shares.user_id=followers.follower_id')
perché nell’esempio originale il ‘followers.follower_id’ è interpretato come una stringa.