【发布时间】:2020-06-08 03:41:45
【问题描述】:
我正在尝试显示来自 2 个数据库表(boxes 和 items)的数据,这些表在 Laravel 5.8 中具有 2 个具有一对多关系的模型。当我尝试在视图中显示数据时,出现以下错误:
SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列 'items.box_box_barcode'(SQL:从 `items` 中选择 *,其中 `items`.`box_box_barcode` = TRTB0001 和 `items`。` box_box_barcode` 不为空)
查看我的代码:
Box.php(模型)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Box extends Model
{
protected $guarded = [];
protected $primaryKey = 'box_barcode';
public $incrementing = false;
protected $keyType = 'string';
public function items(){
return $this->hasMany(Item::class);
}
}
Item.php(模型)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $primaryKey = 'item_barcode'; // or null
public $incrementing = false;
// In Laravel 6.0+ make sure to also set $keyType
protected $keyType = 'string';
public function company(){
return $this->belongsTo(Company::class);
}
}
create_boxes_table.php(迁移 1)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBoxesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('boxes', function (Blueprint $table) {
//$table->bigIncrements('id');
$table->string('box_barcode')->primary();
; //want this to be my id that can increment
$table->string('sort_description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('boxes');
}
}
create_items_table.php(迁移 2)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function (Blueprint $table) {
//$table->bigIncrements('id');
$table->string('item_barcode')->primary();
; //want this to be my id that can increment
$table->string('its_box_barcode');
$table->string('item_quality');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('items');
}
}
boxesController.php(控制器)
<?php
namespace App\Http\Controllers;
use App\Box;
use Illuminate\Http\Request;
class boxesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$boxes = Box::all();
return view('boxes.index', compact('boxes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Box $box
* @return \Illuminate\Http\Response
*/
public function show(Box $box)
{
return view('boxes.show', compact('box'));
}
}
index.blade.php(视图)
@extends('layout')
@section('title', 'Boxes')
@section('content')
<h1>Boxes</h1>
<ul style="list-style-type: none;">
@foreach($boxes as $box)
<li>
<a href="/boxes/{{ $box->box_barcode }}">
{{$box->box_barcode}}
</a>
</li>
@endforeach
</ul>
@endsection
show.blade.php(查看)
@extends('layout')
@section('title', 'Show Box')
@section('content')
@if ($box->items->count())
<div>
@foreach ($box->items as $item)
<div>
<form method="POST" action="/items/{{ $item->id }}">
@method('PATCH')
@csrf
<!-- USE TO STRIKETHROUGH A CONPLETED TASK IN WEB PAGE -->
<label class="checkbox {{ $item->in ? 'is-complete' : '' }}" for="in" >
<input type="checkbox" name="in" onChange="this.form.submit()" {{ $item->in ? 'checked' : '' }}>
{{ $item->item_barcode }}
</label>
</form>
</div>
@endforeach
</div>
@endif
@endsection
【问题讨论】:
-
你有 its_box_barcode 和 box_barcode 但没有 box_box_barcode !?
-
嗨,B.Go。你的意思是? box_barcode 是 box 表的 PK。 item_barcode 是项目表的 PK。我不想查看 its_box_barcode
标签: php mysql laravel eloquent