Caroline Dorchies
Caroline Dorchies

Reputation: 27

Laravel 9 - Can get the datas from the correct table

I'm creating a user account. This user can join a brotherhood('called confrerie on my table with an in called idconfrerie'). This is what it looks like on PHPMyAdmin :

My 'users' table, each user has an iduser, and an idconfrerie.

enter image description here

And this is my 'confreries' table. As you can see there are 4 brotherhoods ('confreries' in french). Each confrerie has an id called 'confrerie'. And you can find this id on the 'users' table.

enter image description here

For example => A user called Caro is the 1rst user so she hase 1 as iduser. She has 1 as idconfrerie, so she joined "Confrérie de l'Acier" brotherhood.

Now what I want to do ?

I want to have a section on her profile where she can find the name of her brotherhood ('confreries'). This is what I wrote on the UserController (do not care about the items, it's another thing users can find on their profile) :

    public function AffichageMonCompte()
{
    $session = session('iduser');

    // Confréries
    // Get permet d'obtenir une COLLECTION
    $confreries = Confrerie::where('confrerie', '=', $session)->get();
    dd($confreries);

    // Items
    $items = Item::where('iduser', '=', $session)->get();

    if ($items->isEmpty()) {
        return view('users.MonCompte')->with('items', null)->with('confreries', null);
    } else {
        return view('users.MonCompte')->with('items', $items)->with('confreries', $confreries);
    }
}

And the code on my HTML page where I call the datas from the 'confreries' table which correspond to the user logged :

        @foreach ($confreries as $confrerie)
          <p>{{ $confrerie->nom }}</p>
          <p>{{ $confrerie->chef }}</p>
        @endforeach

Now what's the result of that code ?

For example => Caro has 1 as iduser and also 1 for her confrerie. It show only the datas of the confrerie ACCORDING to the iduser.

If she had an idconfrerie like 3, it will show again the datas according to 1 (her iduser) and not 3.

I hope you can understand what I mean... Thank you for your help !

EDIT :

This is my Confrerie Model :

class Confrerie extends Model

{

protected $fillable  = [
    "confrerie",
    "img",
    "nom",
    "chef",
    "description",
];
use HasFactory;

}

This is mu User Model :

class User extends Authenticatable implements MustVerifyEmail

{

protected $fillable  = [
    "iduser",
    "idconfrerie",
    "nom",
    "prenom",
    "email",
    "mdp",
    "sexe",
    "age",
    "avatar",
    "type",
    "isVerified",
];

use HasFactory, Notifiable;


protected $hidden = [
    'mdp', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'isVerified' => 'timestamp',
];

}

Upvotes: 0

Views: 38

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

In User Model

define a relationship

/**
 * 
 */
class User extends Model
{
    
    protected $primaryKey = 'iduser';

    public function confrerie()
    {
        return $this->belongsTo(Confrerie::class, 'Confrerie', 'iduser');
    }
}

In Confrerie Model

/**
 * 
 */
class Confrerie extends Model
{
    
    protected $primaryKey = 'confrerie';
}

In Controller

$user = User::with('confrerie')->where('iduser', '=', auth()->id)->get();
dd($user); // check the result

auth()->id used to retrive session logged in user ID. Can use Auth:: as well

Upvotes: 1

Related Questions