Create relationships between addreses and users. Return it on the view on Laravel

user8946984

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; 
    }
}
Colin Barstow

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return a view via ajax in laravel

From Dev

Cypher - Best way to create relationships between two arrays of nodes

From Dev

How to create relationships between objects in Spring JDBC?

From Dev

Is there a way to define relationships between create_resources

From Dev

Users relationships with other users

From Dev

Cypher: Create relationships between nodes based on a common property key id

From Dev

How to create session between two users in Quickblox?

From Dev

How could I create relationships between tables in JetBrains' DataGrip?

From Dev

Laravel: Is it good practice to use model relationships within a view?

From Dev

Laravel 5 - Return array to view

From Dev

return count value to view laravel

From Dev

How to return multiple relationships with Laravel Eloquent?

From Dev

Displaying posts of users the user follows through Laravel relationships

From Dev

Relationships and with in Laravel?

From Dev

MySQL query to return a conversation between 2 users

From Dev

How do I create relationships between a single table?

From Dev

Is it possible to return relationships between two objects in SPARQL?

From Dev

Laravel - Download a view to the users computer as a PDF

From Dev

Laravel relationships between my models

From Dev

return count value to view laravel

From Dev

Laravel return view show message

From Dev

How to return multiple relationships with Laravel Eloquent?

From Dev

Is it possible to create custom relationships in Laravel models?

From Dev

Laravel Relationships (With)

From Dev

Laravel return value to the same view

From Dev

Laravel return model relationships in JSON

From Dev

AppSheet. How to create a view for different users

From Dev

Limit a view to only invited users Laravel Middleware

From Dev

Laravel model create relationships twice between two tables

Related Related

  1. 1

    Return a view via ajax in laravel

  2. 2

    Cypher - Best way to create relationships between two arrays of nodes

  3. 3

    How to create relationships between objects in Spring JDBC?

  4. 4

    Is there a way to define relationships between create_resources

  5. 5

    Users relationships with other users

  6. 6

    Cypher: Create relationships between nodes based on a common property key id

  7. 7

    How to create session between two users in Quickblox?

  8. 8

    How could I create relationships between tables in JetBrains' DataGrip?

  9. 9

    Laravel: Is it good practice to use model relationships within a view?

  10. 10

    Laravel 5 - Return array to view

  11. 11

    return count value to view laravel

  12. 12

    How to return multiple relationships with Laravel Eloquent?

  13. 13

    Displaying posts of users the user follows through Laravel relationships

  14. 14

    Relationships and with in Laravel?

  15. 15

    MySQL query to return a conversation between 2 users

  16. 16

    How do I create relationships between a single table?

  17. 17

    Is it possible to return relationships between two objects in SPARQL?

  18. 18

    Laravel - Download a view to the users computer as a PDF

  19. 19

    Laravel relationships between my models

  20. 20

    return count value to view laravel

  21. 21

    Laravel return view show message

  22. 22

    How to return multiple relationships with Laravel Eloquent?

  23. 23

    Is it possible to create custom relationships in Laravel models?

  24. 24

    Laravel Relationships (With)

  25. 25

    Laravel return value to the same view

  26. 26

    Laravel return model relationships in JSON

  27. 27

    AppSheet. How to create a view for different users

  28. 28

    Limit a view to only invited users Laravel Middleware

  29. 29

    Laravel model create relationships twice between two tables

HotTag

Archive