Laravel Показывает 0 вместо сохраненного значения в базе данных - PullRequest
0 голосов
/ 10 октября 2019

У меня проблема с моим веб-шоу id Supplier В столбце отображается значение 0, когда сохраненное значение не равно 0 enter image description here

, где моя база данных

enter image description here

это мой контроллер

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Supplier;

class SuppliersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $suppliers = Supplier::all();
        return view('suppliers.index')->with('suppliers', $suppliers);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 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  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $supplier = Supplier::find($id);
        return view('suppliers.show')->with('suppliers', $supplier);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

это моя модель

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Supplier extends Model
{
    // Table Name
    protected $table = 'suppliers';
    // Primary Key
    protected $primaryKey = 'idSupplier';
    // Timestamps
    public $timestamps = true;
}

и это мой взгляд

@extends('layouts.app')

@section('content')

    <br style = "line-height:4">

    <div class="navbar navbar-light bg-light">
        <a class="btn btn-primary" href="/suppliers/create" role="button">
            <i class="fa fa-plus-square"></i>
            Tambah Supplier Baru
        </a>
        <form class="form-inline my-2 my-lg-0">
          <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>

    @if (count($suppliers) > 0)
        <div class="table-responsive">
            <table class="table table-hover table-bordered">
                <thead align="center">
                    <tr class="table-primary">
                        <th>id Supplier</th>
                        <th>Nama</th>
                        <th>Alamat</th>
                        <th>Kontak</th>
                        <th>Keterangan</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($suppliers as $supplier)
                        <tr>
                            <th>{{ $supplier -> idSupplier }}</th>
                            <th><a href="/suppliers/{{$supplier->id}}">{{ $supplier -> nama }}</a></th>
                            <th>{{ $supplier -> alamat }}</th>
                            <th>{{ $supplier -> kontak }}</th>
                            <th>{{ $supplier -> keterangan }}</th>
                            <th>
                                <a class="btn btn-warning" href="#" role="button">
                                    <i class="fa fa-tools"></i>
                                    Edit</a>
                                <a class="btn btn-danger" href="#" role="button">
                                    <i class="fa fa-eraser"></i>
                                    Hapus</a>
                            </th>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @else
        <h6>No Suppliers Found</h6>
    @endif
@endsection

В другом столбце может отображаться то же значение, что и в базе данных, за исключением столбца idSupplier

РЕДАКТИРОВАТЬ

каким-либо образом при изменениипервичный ключ в модели от idSupplier до id. Решенная проблема столбца id supplier в представлении показывает то же значение с базой данных.

Ответы [ 2 ]

0 голосов
/ 10 октября 2019

если ваш первичный ключ равен idSupplier, тогда вы должны использовать

в вашей МОДЕЛИ:

public $incrementing = false; 

в вашем контроллере:

 public function show($id)
    {
        $supplier = Supplier::where('idSupplier',$id)->first();
        return view('suppliers.show')->with('suppliers', $supplier);
    }
0 голосов
/ 10 октября 2019

Попробуй это.

Supplier::where('idSupplier',$id)->get();

Примечание: Laravel findOrFail () эта функция всегда ищет первичный идентификатор таблицы. Когда вы используете свой собственный настроенный первичный идентификатор, вы должны определить это в файле модели как первичный ключ.

...