显示数据透视表数据 [英] Displaying pivot table data

查看:114
本文介绍了显示数据透视表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发图书馆应用程序,我想创建一个函数,用户可以在其中向客户签出一本书.因此,我有2个表booksreaders,并且创建了一个名为book_reader的数据透视表以创建checkout方法.但是,我在显示该表中的数据时遇到了一些问题.我已经阅读了几篇关于此的文章,但是他们没有考虑到数据透视表中有一些数据包含新信息,而不仅仅是两个表的联接.如果有人可以帮助我显示数据,我将不胜感激.

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $fillable = ['title', 'year', 'language_id', 'isbn', 'pages', 'user_id'];

public function readers()
    {
        return $this
            ->belongsToMany(Reader::class, 'book_reader')
            ->using(Checkout::class)
            ->withPivot(['returndate', 'maxreturndate']);
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Reader extends Model
{
    protected $fillable = ['name', 'email', 'employee_number'];

    public function books()
    {
        return $this
            ->belongsToMany(Book::class, 'book_reader')
            ->using(Checkout::class)
            ->withPivot(['returndate', 'maxreturndate']);
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Checkout extends Pivot
{
    $table = "book_reader";

    $dates = [
        "maxreturndate",
        "returndate",
    ];
}

迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCheckedOutsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('book_reader', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('book_id')->unsigned();
            $table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
            $table->bigInteger('reader_id')->unsigned();
            $table->foreign('reader_id')->references('id')->on('readers')->onDelete('cascade');
            $table->date('maxreturndate');
            $table->date('returndate')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('book_reader');
    }
}

CheckedOutController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Book;
use App\Reader;
use Illuminate\Support\Carbon;



class CheckedOutController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $books = Book::doesntHave("readers")->get();
        $readers = Reader::all();

        return view('checkedouts/index', compact('books','readers'));
    }

index.blade.php:

@foreach($readers->books as $book)
      <tr>
        <td>{{$book->pivot->id}}</td>
        <td>{{$book->pivot->title}}</td>
        <td>{{$book->pivot->name}}</td>
        <td>{{$book->pivot->created_at}}</td>
        <td >{{$book->pivot->maxreturndate}}</td>
        <td>{{$book->pivot->returndate}}</td>
        <td></td>

解决方案

您的数据透视表仅包含returndatemaxreturndate以及默认时间戳;为什么要尝试从数据透视表属性中获取titlename?您没有说出您的问题"是什么,但我希望那将是其中之一.

请参阅此处以获取此设置工作的示例: https://implode.io/1OCqbL


更笼统地说,由于您已经建立了多对多关系,因此您可以获得数据透视表的优点.实际上,您的书不会被一个以上的人签出,因此拥有访问器设置为一种伪关系:

public function getReaderAttribute()
{
    return $this->readers->first();
}

现在,您可以在适当的地方参考$book->reader->pivot.

您可能希望对返回日期执行相同的操作:

public function getReturndateAttribute()
{
    return $this->reader ? $this->reader->pivot->returndate : null;
}

因此,您可以轻松获取退房的书的归还日期.

I am working on a library application, and I want to create a function, where the user can check out a book to a customer. So I have 2 tables, books and readers and I have created a pivot table, called book_reader as well to create the checkout method. However, I have some problems displaying the data from this table. I have read several articles about this, but they don't take in consideration that there is some data in the pivot table that contains new information and isn't just the joining of the 2 tables. I would be very appreciative if someone could help me with how I can display this data.

Models:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $fillable = ['title', 'year', 'language_id', 'isbn', 'pages', 'user_id'];

public function readers()
    {
        return $this
            ->belongsToMany(Reader::class, 'book_reader')
            ->using(Checkout::class)
            ->withPivot(['returndate', 'maxreturndate']);
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Reader extends Model
{
    protected $fillable = ['name', 'email', 'employee_number'];

    public function books()
    {
        return $this
            ->belongsToMany(Book::class, 'book_reader')
            ->using(Checkout::class)
            ->withPivot(['returndate', 'maxreturndate']);
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Checkout extends Pivot
{
    $table = "book_reader";

    $dates = [
        "maxreturndate",
        "returndate",
    ];
}

Migrations:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCheckedOutsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('book_reader', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('book_id')->unsigned();
            $table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
            $table->bigInteger('reader_id')->unsigned();
            $table->foreign('reader_id')->references('id')->on('readers')->onDelete('cascade');
            $table->date('maxreturndate');
            $table->date('returndate')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('book_reader');
    }
}

CheckedOutController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Book;
use App\Reader;
use Illuminate\Support\Carbon;



class CheckedOutController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $books = Book::doesntHave("readers")->get();
        $readers = Reader::all();

        return view('checkedouts/index', compact('books','readers'));
    }

index.blade.php:

@foreach($readers->books as $book)
      <tr>
        <td>{{$book->pivot->id}}</td>
        <td>{{$book->pivot->title}}</td>
        <td>{{$book->pivot->name}}</td>
        <td>{{$book->pivot->created_at}}</td>
        <td >{{$book->pivot->maxreturndate}}</td>
        <td>{{$book->pivot->returndate}}</td>
        <td></td>

解决方案

Your pivot table contains only returndate and maxreturndate as well as the default timestamps; why are you trying to get title and name from the pivot property? You don't say what your "problems" are, but I expect that would be one of them.

See here for an example of this setup working: https://implode.io/1OCqbL


On a more general note, since you've set up the relationship as many-to-many, you get the advantages of the pivot table. In reality, your book will not be checked out by more than one person so it might make sense to have an accessor set up as a sort of pseudo-relationship:

public function getReaderAttribute()
{
    return $this->readers->first();
}

Now you can refer to $book->reader->pivot where it's appropriate.

You may want to do the same with return date:

public function getReturndateAttribute()
{
    return $this->reader ? $this->reader->pivot->returndate : null;
}

So you can easily get the return date of a book that's checked out.

这篇关于显示数据透视表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆