php - Laravel 4.2 Join Table and Pagination

61

i learn Laravel 4.2 and try to use join table and pagination. My code work when use pagination. But when i combine with join table it not works.

Here is my Controller : BookController.php

public function index()
    {
        // Get All Books
        //$booksList = Book::all();
        $booksList = Book::with('category')->paginate(2);

        return View::make('books.index', compact('booksList'));
    }

And i get error like this :

Call to undefined method Illuminate\Database\Query\Builder::category()

My Category Model just like this :

<?php 
    class Category extends Eloquent
    {

    }

My Book model like this :

<?php 
    class Book extends Eloquent
    {
        protected $fillable = array('isbn', 'title', 'author', 'publisher', 'language');
    }

And in index.blade.php :

<tr>
            <td>{{ $book->id }}</td>
            <td>{{ $book->isbn }}</td>
            <td>{{ $book->category_id }}</td>
            <td>{{ $book->title }}</td>
            <td>{{ $book->author }}</td>
            <td>{{ $book->publisher }}</td>
            <td>@if ($book->language == 1) {{ 'English' }} @else {{ 'Indonesian' }} @endif</td>
            <td>
                {{ link_to_route('books.show', 'Read', array($book->id), array('class' => 'btn btn-primary btn-xs')) }}
            </td>
            <td>
                {{ link_to_route('books.edit', 'Edit', array($book->id), array('class'=>'btn btn-warning btn-xs')) }}
            </td>
            <td>
                {{ Form::open(array('method'=>'DELETE', 'route'=>array('books.destroy', $book->id))) }}
                {{ Form::submit('Delete', array('class'=>'btn btn-danger btn-xs', 'onclick' => 'return confirm("Are you sure?")')) }}

                {{ Form::close() }}
            </td>
        </tr>

My Table Structure like this : enter image description here

enter image description here

Please help thank you.

898

Answer

Solution:

When you using join in eloquent you have to implement the relation withing the models like this :

<?php 
class Book extends Eloquent
{
    protected $fillable = array('isbn', 'title', 'author', 'publisher', 'language');

     public function category(){
        return $this->belongsTo('Category');
     }

}

and

<?php 
class Category extends Eloquent
{
    public function book(){
        return $this->hasMany('Category');
     }
}
900

Answer

Solution:

with('category') doesn't mean a join but eager loading

It will actually perform a join to eager load your data BUT you have to define relations for your models in order for laravel to understand what to do.

In short, you can only usewith() on existing model relationships. In your case you should change your models as follows:

Category

class Category extends Eloquent
{
  public function books(){
    return $this->hasMany('Book');
  }
}

Book

class Book extends Eloquent
{
  protected $fillable = array('isbn', 'title', 'author', 'publisher', 'language');

  public function category(){ 
    return $this->belongsTo('Category');
  }
}

More on Laravel Relationships

People are also looking for solutions to the problem: php - array_combine with key has only one from array 1 and multiple values from array 2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.