簡體   English   中英

如何使用Laravel獲取屬於外鍵的用戶名?

[英]How to get the name of user that belongs to foreign key using Laravel?

我想使用Laravel Eloquent獲取用戶名稱所屬的外鍵。

我有帖子型號:

Class Posts Extends Eloquent{
protected $table = 'posts';
protected $fillable = array('title, image, text, user_id');
public $timestamps = false;

}

和用戶模型:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

我想將要查看的用戶名,標題,文本,圖像的值發送給控制器。

public function index(){
    // get all the bears
        $posts = Posts::all();
      return View::make('welcome', compact('posts'));
  }

要為用戶->帖子設置關系,則可以使用hasMany

public function posts(){
    return $this->hasMany('App\Post');
}

這將在posts表中查找任何user_id 如果名稱不同,則可以將其作為第二個參數傳遞。

public function posts(){
    return $this->hasMany('App\Post', name_of_column_in_post_table, name_of_column_in_user_table);
}

在posts表中,您需要hasOnebelongsTo 兩者的工作方式相同:

public function users() {
    return $this->belongsTo('App\User', name_of_column_in_user_table, name_of_column_in_post_table);
}

然后,您可以通過執行$post->user->name獲取用戶信息。

在模型類中添加關系,例如

Class Posts Extends Eloquent{
  protected $table = 'posts';
  protected $fillable = array('title, image, text, user_id');
  public $timestamps = false;
  public function user()
  {
    return $this->belongsTo('App\User','user_id);
  }
}

現在,您可以在控制器或每個Post實例的視圖中使用:通過示例

$post->user

閱讀有關多對一關系,甚至渴望加載的文檔。

將模型之間的一對多關系定義為

class Posts extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }
    // Whatever your code in Post model
}


class User extends Model
{
    public function posts(){
        return $this->hasMany('App\Post');
    }
    // Whatever your code in User model
}

該文件另有說明這里 現在,您可以獲取該帖子所屬的用戶的名稱。

將路線定義為

Route::get('/all-posts','PostController@getAllPosts')->name('get_all_posts');

編寫控制器類以獲取帖子

class PostController extends Controller
{
    public function getAllPosts() {  
         $posts = Posts::all();

         foreach ($posts as $post){

            $username = $post->user->name;
            //do something with $username

        }
        return view('all_posts')->with('detailed_posts');
        //here the $detailed_posts can be defined in the 'do something' above
    }
}

在這里,您可以創建一個新的用戶名數組,並將其傳遞給視圖,

要么

將PostController設置為

class PostController extends Controller
    {
        public function getAllPosts() {  
           return view(all_posts);
    }
}

然后將all_posts.blade.php設置為使用如下刀片語法直接訪問視圖中的用戶名,

<html>
    <div>
        <h1>All Posts</h1> 
        @foreach (App\Post::all() as $post)
            <span> Title : {{ $post->title}}</span>
            <span> Username: {{$post->user->name}}</span>
            .......
        @endforeach
    </div>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM