2

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)
Omar Osama
  • 23
  • 5
  • Sorry, I am looking at the documentation (as I do not know Voyager) and I don't see anything about you having the migration created, so did you create the column `design_id` on the `posts` table ? – matiaslauriti Mar 20 '21 at 01:19
  • No I haven't created the column `design_id` in the `posts` table as it isn't a requirement in voyager admin panel. Do you think I should try doing so ? – Omar Osama Mar 20 '21 at 01:25
  • From what I am looking at (the error) it seems that it is literally trying to do a `where` in that table using `desing_id` column (like a normal Eloquent relationship) so I am assuming that you have to do everything normally except use Voyager for admin stuff (so it can allow you to BREAD anything you configure using it) – matiaslauriti Mar 20 '21 at 01:27
  • Okay, but the issue is that when I add `design_id` in the `posts` table the `design_id` shows up as a field in voyager admin panel – Omar Osama Mar 20 '21 at 01:30
  • Isn't that okay ? I think you have to edit the BREAD config or redo the Voyager relationship, I think it should be hidden once the relation is correct, or maybe you have to hide it in the BREAD config. I am reading the [docs](https://voyager-docs.devdojo.com/bread/relationships) and it does not say if it will be hidden or if you can do it, but you have to do the migration manually, so you have to add it or it will not work. – matiaslauriti Mar 20 '21 at 01:34
  • Okay got it, Thank you so much! – Omar Osama Mar 20 '21 at 01:35
  • No problem ! I saw more documentation and you should edit the BREAD config for that field and uncheck all `Visibility` marks so the field should become invisible, hence problem solved ! – matiaslauriti Mar 20 '21 at 01:39
  • 1
    Yes, It finally worked. Thanks again for the help, I really appreciate it – Omar Osama Mar 20 '21 at 02:04

0 Answers0