Laravel 显示 0 而不是数据库中保存的值

Laravel Shows 0 instead of saved value in database

我的网络显示有问题 id Supplier 列显示值 0 当保存的值不是 0

我的数据库在哪里

这是我的控制器

<?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 显示与数据库相同的值。

试试这个。

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

注意:Laravel findOrFail() 这个函数总是寻找 table 主 ID。当您使用自己的自定义主 ID 时,您应该在模型文件中将其定义为主键。

如果您的 主键 idSupplier 那么您必须使用

在您的模型中:

public $incrementing = false; 

在你的控制器中:

 public function show($id)
    {
        $supplier = Supplier::where('idSupplier',$id)->first();
        return view('suppliers.show')->with('suppliers', $supplier);
    }