2017-07-16 63 views
0

我現在正在學習Laravel,並且很難理解如何從屬於另一個表上的記錄的一個表中獲取一組記錄關鍵。使用自定義鍵從Laravel中的一對多關係中獲取數組

我有兩個表:

titles 
------------------- 
id | title_name | created_at | updated_at 

posts 
------------------- 
id | titles_id | content 

我的路線/ {} TITLE_NAME由read()方法對我PagesController.php

public function read($title){ 

    $title_name = $title; 
    $title_id = Title::find($title)->id; 
    $posts = Title::find($title)->posts; 

    return view('pages/read')->with([ 
     'title_name' => $title_name, 
     'title_id' => $title_id, 
     'posts' => $posts 
    ]); 
} 

被控制但這似乎並不輸出任何東西。我有我的模型的設置是這樣的:

Title.php

class Title extends Model 
{ 
    // Table Name 
    protected $table = "titles"; 
    // Primary Key 
    protected $primaryKey = "title"; 
    // Timestamps 
    public $timestamps = "true"; 
    // Custom primaryKey 
    public $incrementing = false; 
    //relationship 
    public function posts(){ 
      return $this->hasMany('App\Post', 'titles_id')->orderBy('created_at', 'desc'); 
    } 
} 

post.php中

class Post extends Model 
{ 
    // Table Name 
    protected $table = "posts"; 
    // Primary Key 
    protected $primaryKey = "id"; 
    // Timestamps 
    public $timestamps = "true"; 
    //relationship 
    public function titles(){ 
      return $this->belongsTo('App\Title'); 
    } 
} 

我認爲問題是,當我做題::發現($標題) - > post,laravel試圖找到titles_id = title_name的帖子,因爲我將title_name設置爲primaryKey,但我需要它在標題表中尋找id列,而不是名稱...

+1

您可以使用標題::在哪裏( '身份證',$ yourIdWhichWant) - >崗位; – Th3

+1

它不輸出什麼?你有白色屏幕還是帖子沒有顯示? –

+0

謝謝Th3我會盡力而爲,讓你們知道它是怎麼回事。 @Jan它只是不顯示帖子 – MerrickC

回答

1

好吧,我會的給你一個例子,說明你做錯了什麼。

表:

titles 
------------------- 
id | title_name | created_at | updated_at 

posts 
------------------- 
id | title_id | content 

titles_idtitle_id,雄辯喜歡這個更多。

你的控制器:

public function read($titleName){ 
    // The first function argument is the name of the title, 
    // not the title model. 
    // Also don't use snake_case in laravel(Except helpers) but camelCase. 
    // We are not going to use find, you might have set the name as 
    // primary key, but the id column still exists. 
    // firstOrFail() means get the first result, if there isn't, throw 
    // a model not found exception(404). 
    $title = Title::where('name', $titleName)->firstOrFail(); 

    return view('pages/read')->with([ 
     // You could just do 'title' => $title, and do the rest in the view. 
     'title_name' => $title->name, 
     'title_id' => $title->id, 
     'posts' => $title->posts 
    ]); 
} 

名稱型號:

class Title extends Model 
{ 
    // $table not needed, laravel knows this(Yes pure magic). 

    // No, we don't want name as primary key. 

    // Timestamps is true by default, so we don't need it. 

    public function posts(){ 
      return $this->hasMany(\App\Post::class)->orderBy('created_at', 'desc'); 
    } 
} 

Post模型:

class Post extends Model 
{ 
    // This function should be called title, not titles. 
    public function title(){ 
      return $this->belongsTo(App\Title::class); 
    } 
} 
+0

Thanks for the in-深度響應,我會試試這個並報告回來! – MerrickC

相關問題