dimanche 23 février 2020

Have Laravel 5.8 count data in other fields not just unsignedInteger

Hi I am new to Laravel,

I have two database tables (boxes and items) aka a hasMany() relationship. I am trying to get laravel to display 4 results of the boxbarcode column not 5 of the box_id as you see in the screenshot. Basically, all items that are of box TRTB0001

enter image description here

enter image description here

The problem is it is looking at the box_id (1,1,1,1,1) not the boxbarcode (TRTB0001). How can I adjust my Model, Controller, View to display this? See code below.

Box.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Box extends Model
{

    protected $guarded = [];


    public function items(){

        return $this->hasMany(Item::class);
    }

}

Item.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{

    public function company(){

        return $this->belongsTo(Company::class);
    }

}

boxesController.php (Controller)


namespace App\Http\Controllers;

use App\Box;
use Illuminate\Http\Request;

class boxesController extends Controller
{

    /**
     * Display the specified resource.
     *
     * @param  \App\Box  $box
     * @return \Illuminate\Http\Response
     */
    public function show(Box $box)
    {        
        return view('boxes.show', compact('box'));
    }


}

show.blade.php

@extends('layout')


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


@section('content')

<h1 class="title"></h1>

<p> <a href="/projects//edit">Edit Box</a></p>

<h3 class="content">Status: </h3>

<hr>

<h5 class="content">List of Box Items:</h5>

<!-- ONLY SHOW TASK <DIV> IF A TASK EXISTS -->
@if ($box->items->count())
    <div>
        @foreach ($box->items as $item)

            <div>

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

                    <label class="checkbox " for="in" >

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

                    </label>

                </form>         

            </div>

        @endforeach

    </div>
@endif

@endsection



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire