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

时间:2019-10-10 03:41:02

标签: laravel laravel-6

我在我的网络节目id Supplier列中显示值0时遇到问题 当保存的值不是0enter 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 :(得分:0)

尝试一下。

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

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

答案 1 :(得分:0)

如果您的主键idSupplier,则必须使用

在您的模型中:

public $incrementing = false; 

在您的控制器中:

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