0

I'm learning Laravel right now and I'm stumped on how to get an array of records from one table that belong to a record on another table based on a key.

I have two tables:

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

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

I have the route /{title_name} being controlled by the read() method on my 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
    ]);
}

But this doesn't seem to output anything. I have my models setup like this:

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');
     }
}

I think the problem is that when I do Title::find($title)->post, laravel is trying to find posts where the titles_id = title_name, because I set title_name as the primaryKey, but I need it to be looking for the id column in the Titles table, and not the name...

4
  • 1
    you can use Title::where('id',$yourIdWhichWant)->post; Commented Jul 16, 2017 at 19:29
  • 1
    What doesn't it output? Do you have a white screen or are the posts not displayed. Commented Jul 16, 2017 at 19:53
  • Thanks Th3 I'll try than and let you guys know how it goes. @Jan it just isn't displaying the posts Commented Jul 17, 2017 at 0:29
  • So I tried that and I'm getting this error: (1/1) ErrorException Undefined property: Illuminate\Database\Eloquent\Builder::$posts Commented Jul 17, 2017 at 1:09

1 Answer 1

1

Alright I will give you an example where I explain everything you do wrong.

Tables:

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

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

Not titles_id but title_id, eloquent likes this more.

Your controller:

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
    ]);
}

Title model:

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 model:

class Post extends Model
{
     // This function should be called title, not titles.
     public function title(){
          return $this->belongsTo(App\Title::class);
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the in-depth response, I'll try this out and report back!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.