How to view voyager model relationship in laravel site?

Viewed 792

I am using voyager admin panel in my laravel app. And there are two models:

Design
Post

I created a relationship through the voyager admin panel, the built in "Create a relationship" button.

The relationship is as follows:

class Design extends Model
{
    public function posts()
    {
       return $this->hasMany(Post::class);
    }
}



class Post extends Model
{
   public function design()
   {
     return $this->belongsTo(Design::class);
   }
}

In the admin panel everything is working properly but I want to display the posts related to a specific design in the site itself not the admin panel.

I tried the following:

DesignController.php

public function show($slug)
{
    $design = Design::where('slug', '=', $slug)->firstOrFail();
    
    return view('design.show', compact('design'));
}

design/show.blade.php

@extends('layouts.app')
@section('content')

   <div class="container">
      <h1>{{ $design->name }}</h1>

      @foreach ($design->posts as $post)
        <h1>{{ $post->title }}</h1>
      @endforeach

   </div>

@endsection

Unfortunately, I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.design_id' in 'where clause' (SQL: select * from `posts` where `posts`.`design_id` = 12 and `posts`.`design_id` is not null)
0 Answers
Related