I have 2 tables which are user
and customer
, la first table has like fields following:
(id, name, email, password)
.
Then, the table 'customer' has like fields (id, name, firstname)
.
In my Controller Customer, via my function index(), I have to retrieve id_user from my table user.
1 question // is it my relationship is correct between my 2 tables?
Model Customer
protected $fillable = ['name', 'firstname', 'user_id'];
public function user()
{
return $this->belongsTo('App\User');
}
Model User
public function customers()
{
return $this->hasOne('App\Customer');
}
2 question // How retrieve ID of the user which is connected ?
I have for now this only...
public function index()
{
$customers = Customer::orderby('id', 'desc')->paginate(5);
return view('customers.index', compact('customers'));
}
3 question // I wait to understand the answer two, but I would like to understand. How I have to adapt my file index.blade.php via my loop please?
@foreach($customers as $customer)
<tr>
<td> </td>
<td> </td>
Thank you for your explanations.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2P2B65y
via IFTTT
Aucun commentaire:
Enregistrer un commentaire