I'm having some problems trying to return a view with the user address. I don't know if the real problem is in my controller or in my models. I'd like to know the correct way to return a user with his address creating a relationship with the models.
Address Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Address extends Model {
protected $fillable = ['name','last_name','street_address','street_address2', 'country', 'city', 'state-province', 'phone-number', 'phone-number2', 'address-type'];
public function user() {
return $this->hasOne('App\User');
}
}
User Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* 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', 'remember_token',
];
public function address() {
return $this->belongsTo('App\Address');
}
}
In the UserController, I'm using the method getAddress, but I really don't know how to get the user address and how to create a user with that relation.
UserController
namespace App\Http\Controllers;
use App\User;
use App\Address;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class UserController extends Controller
{
public function userProfile() {
$user = Auth::user();
return view('user.profile', ['user' => $user]);
}
public function userAccount(User $user) {
$user = Auth::user();
return view('user.account', compact('user'));
}
public function nameUpdate(User $user)
{
$this->validate(request(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255'
]);
$user->first_name = request('first_name');
$user->last_name = request('last_name');
$user->save();
return redirect()->back();
}
public function emailUpdate(User $user)
{
$this->validate(request(), [
'email' => 'required|string|email|max:255|unique:users',
]);
$user->email = request('email');
$user->save();
return redirect()->back();
}
public function passwordUpdate(User $user) {
$this->validate(request(), [
'password' => 'required|min:8|confirmed',
]);
$user->password = bcrypt(request('password'));
$user->save();
return redirect()->back();
}
public function getAddress() {
$user=Auth::user();
$adress = $user->adress;
}
}
First of all you must reverse the relationship so that the address belongs to the user. If a user can have multiple addresses then a user cannot belong to each of those addresses. The addresses must belong to the user.
In the address table you will need a user_id column to start with.
User.php
public function addresses()
{
return $this->hasMany(Address::class);
}
Address.php
public function user()
{
return $this->belongsTo(User::class);
}
Then you can simply get all the addresses like so:
foreach(Auth::user()->addresses as $address){
//You can now access your address here
}
Your current controller:
public function getAddresses() {
$user = Auth::user();
$addresses = $user->addresses;
return $addresses;
}
In a blade file you could do something like:
@foreach(Auth::user()->addresses as $address){
<li>{{ $address->column_name }}</li>
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments