dongyata3336 2018-05-22 20:20
浏览 42
已采纳

创建addreses和users之间的关系。 把它放在Laravel的视野上

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; 
    }
}
  • 写回答

1条回答 默认 最新

  • dsfsd43523 2018-05-22 22:40
    关注

    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>    
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝