ERaufi
ERaufi

Reputation: 1663

belongsTo and hasMany relationship in laravel

I've three tables books,Category and authors even after creating relationship between them I only get the ID instead of Name. I saw many links but didn't figure it out what is the problem with my code

My Books Model

class books extends Model
{
    //
    protected $table='books';
    protected $primarykey = 'bookId';

    public function books_categories(){
        return $this->belongsTo('app\books_categories', 'catId', 'catId');
    }

    public function authors(){
        return $this->belongsTo('app\authors','authId','authId');
    }
}

My authors Model

class authors extends Model
{
    //
    public function books(){
        return $this->hasMany('app\books','authId','authId');
    }
}

My books_categories Model

class books_categories extends Model
{
    //
    public function books(){
        return $this->hasMany('app\books');
    }
}

My books table

    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('bookId');
            $table->string('bookTitle', 255)->nullable('false');
            $table->unsignedInteger('edition');
            $table->unsignedInteger('authId');
            $table->unsignedInteger('catId');
            $table->foreign('catId')->references('catId')->on('books_categories');
            $table->foreign('authId')->references('authId')->on('authors');
            $table->unsignedInteger('totalAvail')->default(0);
            $table->unsignedInteger('totalIss')->default(0);
            $table->timestamps();
        });
    }

My Controller

    public function index()
    {
        $books = books::all();
        return view('booksList', ['books'=> $books]);

    }

My View

<div class="panel panel-default">
    <div class="panel-heading">Books List</div>
    <div class="panel-body">
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <th scope="col">BookID</th>
                <th scope="col">Title</th>
                <th scope="col">Edition</th>
                <th scope="col">Category</th>
                <th scope="col">Author</th>
                <th scope="col">Books Available</th>
                <th scope="col">Action</th>

            </tr>
            </thead>
            <tbody>

            @foreach($books as $book)
                <tr>
                    <td>{{$book->bookId}}</td>
                    <td>{{$book->bookTitle}}</td>
                    <td>{{$book->edition}}</td>
                    <td>{{$book->catId}}</td>
                    <td>{{$book->authId}}</td>
                    <td>{{$book->totalAvail}}</td>
                    <td>
                        <span><button type="button" class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-edit"></i></button></span>
                        <span><button type="button" class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-trash"></i></button></span>

                    </td>
                </tr>
        @endforeach
            </tbody>
        </table>
    </div>
</div>

enter image description here How to get Name instead of ID

Upvotes: 0

Views: 548

Answers (2)

ERaufi
ERaufi

Reputation: 1663

I figuer it out actually there was two error in my code 1.I change my view from <td>{{$book->authId}}</td> to this <td>{{$book->authors->name}}</td> and in my books model I changed

    public function authors(){
        return $this->belongsTo('app\authors','authId','authId');
    }

to this

    public function authors(){
        return $this->belongsTo(authors::class,'authId','authId');
    }

Upvotes: 0

Hamza Mogni
Hamza Mogni

Reputation: 720

It is showing the ID because you are telling it to show that by writing <td>{{$book->authId}}</td>

change it to

<td>{{$book->authors->name}}</td>

change name to match the attribute name you have on your database.

refer to the official docs here for more insight on how to handle Eloquent relationships

https://laravel.com/docs/6.x/eloquent-relationships#one-to-many-inverse

Upvotes: 3

Related Questions