Php Laravel 4用户和朋友表之间的关系

Php Laravel 4用户和朋友表之间的关系,php,laravel-4,Php,Laravel 4,我有两个表用户和朋友。如何使用userID(1)写出用户的所有好友? 我应该使用哪种关系 User.php <?php use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; class User extends El

我有两个表
用户
朋友
。如何使用userID(1)写出用户的所有好友? 我应该使用哪种关系

User.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('email', 'username', 'password', 'password_temp', 'code', 'active', 'remember_token');



    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    public function friends()
    {
      return $this->belongsToMany('Friend');
    }

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');


}
<?php


class Friend extends Eloquent {

    protected $fillable = array('friend_one', 'friend_two', 'status');




    protected $table = 'friends';
  public function users() {

        return $this->belongsToMany('User');
       }


}
     <ul>
        @foreach($friends as $friend)
        <li><a href="">{{  }}</a></li>
        @endforeach
    </ul> 
friends.blade.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('email', 'username', 'password', 'password_temp', 'code', 'active', 'remember_token');



    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    public function friends()
    {
      return $this->belongsToMany('Friend');
    }

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');


}
<?php


class Friend extends Eloquent {

    protected $fillable = array('friend_one', 'friend_two', 'status');




    protected $table = 'friends';
  public function users() {

        return $this->belongsToMany('User');
       }


}
     <ul>
        @foreach($friends as $friend)
        <li><a href="">{{  }}</a></li>
        @endforeach
    </ul> 
    @foreach($friends as$friend)
  • @endforeach
将控制器更改为

public function getFriendShow(){

        $id = Auth::user()->id;
        $friends = Friend::where('userID','=', "4")->get();

        return View::make('profile.friends')->with('friends', $friends);

    }

这有帮助吗?

如何使用username from users表写出所有好友?在上面的代码中,它获取当前登录用户的所有好友。您有不同的意图吗?这将从friend表中写出记录,其中类似:{“id”:26,“friend\u one”:4,“friend\u two”:4,“status”:“1”,“created\u at”:“2014-11-29 16:37:22”,“Update\u at”:“2014-11-29 16:37:22”},我想使用用户的用户名,id是4,这是什么意思?