vendredi 1 mai 2020

how to show user name with id from 2 tables in laravel

when i send a letter, i catch sender_id with auth()->user()->id and save into the letter table

but just user_id will save, like 2 or 3 ...

how can i show user name in my view ?

list.blade.php :

<tr>
     <td></td>
</tr>

LetterController.php -> public function index :

$letters = Letter::latest()->paginate(10)->where('recieve_id', '=', auth()->user()->id);

LetterController.php -> public function store :

Letter::create([
    'sender_id' => auth()->user()->id,
]);

letter table :

$table->string('sender_id');

and i have a relation but i dont know it is correct or not

Letter.php :

public function user()
{
    return $this->belongsTo(User::class, 'id', 'sender_id');
}

User.php:

public function letter()
{
    return $this->hasMany(Letter::class);
}

UPDATE :

in veiw when use this :

<td></td>

but i have this error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.sender_id' in 
'where clause' (SQL: select * from `users` where `users`.`sender_id` = 1 limit 
1) (View: D:\Web\Projects\automation\resources\views\Letter\list.blade.php)


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire