lundi 24 février 2020

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'items.box_box_barcode' in 'where clause'

I am trying to display specific data from 2 database tables (boxes and items) that have 2 models with a hasMany belongsTo relationship in Laravel 5.8 but I get the error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'items.box_box_barcode' in 'where clause' (SQL: select * from items where items.box_box_barcode = TRTB0001 and items.box_box_barcode is not null)

See my code:

Box.php (model)

<?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 (model)

<?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 (migration 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 (migration 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 (controller)

<?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 (view)

@extends('layout')


@section('title', 'Boxes')  


@section('content')


    <h1>Boxes</h1>

     <ul style="list-style-type: none;">

       @foreach($boxes as $box) 

         <li>
            <a href="/boxes/">
                  
            </a>
         </li>

       @endforeach

    </ul>           

 @endsection

show.blade.php (view)

@extends('layout')


@section('title', 'Show Box')  


@section('content')


@if ($box->items->count())
    <div>
        @foreach ($box->items as $item)

            <div>

                <form method="POST" action="/items/">
                    @method('PATCH')
                    @csrf

                    <!-- USE TO STRIKETHROUGH A CONPLETED TASK IN WEB PAGE -->
                    <label class="checkbox " for="in" >

                    <input type="checkbox" name="in" onChange="this.form.submit()" >
                            

                    </label>


                </form>



            </div>

        @endforeach

    </div>
@endif

@endsection


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/37RjAVZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire