【问题标题】:Displaying pivot table data显示数据透视表数据
【发布时间】:2020-03-15 11:56:32
【问题描述】:

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

型号:

<?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>

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    您的数据透视表仅包含 returndatemaxreturndate 以及默认时间戳;你为什么要从 pivot 属性中获取titlename?你没有说你的“问题”是什么,但我希望那会是其中之一。

    有关此设置工作的示例,请参见此处:https://implode.io/1OCqbL


    一般来说,由于您已将关系设置为多对多,因此您将获得数据透视表的优势。实际上,您的书不会被超过一个人签出,因此将accessor 设置为一种伪关系可能是有意义的:

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

    现在你可以在合适的地方参考$book-&gt;reader-&gt;pivot

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

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

    因此,您可以轻松获取已签出图书的归还日期。

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多