P
P
Pavel Sachenko2020-06-17 14:18:19
Laravel
Pavel Sachenko, 2020-06-17 14:18:19

How to bind a parameter to another table?

The error itself:
5ee9f86a625a6682187055.png
Parsing the error:
There is a Book model in which the links are registered:

public function categories(){
        return $this->belongsTo(Category::class, 'category_id');
    }
    public function publishers(){
        return $this->belongsTo(Publisher::class, 'publisher_id');
    }


In controller method:
public function index()
    {
        $products = Book::paginate(10);
        return view('admin.product.product.index', compact('products'));
    }


Migration books
public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->date('published')->nullable();
            $table->float('price');
            $table->string('description')->nullable();
            $table->boolean('available');
            $table->string('author');

            $table->integer('category_id')->nullable();
            $table->integer('publisher_id')->nullable();
            $table->integer('country_id')->nullable();

            $table->timestamps();
        });
    }


Migration categories
public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }


And the view code:
@foreach($products as $product)
                <tr class="table-dark">
                    <td>{{$product->title}}</td>
                    <td>{{$product->author}}</td>
                    <td>{{$product->publishers->name}}</td>
                    <td>{{$product->categories->name}}</td>
                    <td>{{$product->count}} шт.</td>
                    <td>{{$product->price}} руб</td>
                    <td>
                        <form action="{{route('admin.product.destroy', $product)}}" method="POST"class="d-inline">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger">delete</button>
                        </form>
                        <a href="{{route('admin.product.edit', $product)}}" class="btn btn-warning">edit</a>
                        <a href="{{route('admin.product.show', $product)}}" class="btn btn-info">overview</a>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        {{$products->links()}}


Description of the problem:

The first page loads without problems, but when switching to the second or any other (except the first) pagination, an error occurs, but if you remove this line {{$product->categories->name}} , the error disappears. But why then does it work with {{$product->publishers->name}} if it is similar to {{$product->categories->name}}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
Xcho Margaryan, 2020-06-17
@PavelSachenko

Do all categories have a name?
can you show the error?

P
Pavel Sachenko, 2020-06-17
@PavelSachenko

Thank you for pushing the idea, the mistake was that one of the books had a category that is not in the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question