无法从db获取行,laravel

lc8prwob  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(338)

我想设置从表中的一行到第二个表和其中的行的值。我开始用这个做控制器方法,因为我试图首先从视图中获取值:

public function duplicate(Request $request, Proform $proform)
{
    var_dump($proform);
    var_dump($proform->id);

    $proform = DB::table('proforms')->where('id', $proform->id)->first();

    var_dump($proform);
}

我从视图和这条线上获取值(第一个)

var_dump($proform);

我得到这个身份证:

object(App\Proform)[1349]
  protected 'fillable' => 
    array (size=24)
      0 => string 'proformnumber' (length=13)
      1 => string 'proformdate' (length=11)
      2 => string 'id' (length=2)
      3 => string 'selldate' (length=8)
      4 => string 'user_id' (length=7)
      5 => string 'paymentmethod' (length=13)
      6 => string 'paymentdate' (length=11)
      7 => string 'status' (length=6)
      8 => string 'comments' (length=8)
      9 => string 'city' (length=4)
      10 => string 'autonumber' (length=10)
      11 => string 'automonth' (length=9)
      12 => string 'autoyear' (length=8)
      13 => string 'name' (length=4)
      14 => string 'PKWIU' (length=5)
      15 => string 'quantity' (length=8)
      16 => string 'unit' (length=4)
      17 => string 'netunit' (length=7)
      18 => string 'nettotal' (length=8)
      19 => string 'VATrate' (length=7)
      20 => string 'grossunit' (length=9)
      21 => string 'grosstotal' (length=10)
      22 => string 'form_id' (length=7)
      23 => string 'currency_id' (length=11)
  public 'sortable' => 
    array (size=13)
      0 => string 'proformnumber' (length=13)
      1 => string 'proformdate' (length=11)
      2 => string 'id' (length=2)
      3 => string 'selldate' (length=8)
      4 => string 'user_id' (length=7)
      5 => string 'paymentmethod' (length=13)
      6 => string 'paymentdate' (length=11)
      7 => string 'status' (length=6)
      8 => string 'comments' (length=8)
      9 => string 'grosstotal' (length=10)
      10 => string 'nettotal' (length=8)
      11 => string 'form_id' (length=7)
      12 => string 'currency_id' (length=11)
  protected 'connection' => null
  protected 'table' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'keyType' => string 'int' (length=3)
  public 'incrementing' => boolean true
  protected 'with' => 
    array (size=0)
      empty
  protected 'withCount' => 
    array (size=0)
      empty
  protected 'perPage' => int 15
  public 'exists' => boolean false
  public 'wasRecentlyCreated' => boolean false
  protected 'attributes' => 
    array (size=0)
      empty
  protected 'original' => 
    array (size=0)
      empty
  protected 'changes' => 
    array (size=0)
      empty
  protected 'casts' => 
    array (size=0)
      empty
  protected 'classCastCache' => 
    array (size=0)
      empty
  protected 'dates' => 
    array (size=0)
      empty
  protected 'dateFormat' => null
  protected 'appends' => 
    array (size=0)
      empty
  protected 'dispatchesEvents' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'relations' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  public 'timestamps' => boolean true
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)

但我不知道为什么我在这个(秒)上得到空值

var_dump($proform->id);

之后呢

$proform = DB::table('proforms')->where('id', $proform->id)->first();

此为空(第三个)

var_dump($proform);

/home/laravel/web/laravel.swt101.eu/public_html/abonamenty/app/Http/Controllers/ProformController.php:33:null
/home/laravel/web/laravel.swt101.eu/public_html/abonamenty/app/Http/Controllers/ProformController.php:37:null

首先,我通过按“复制”按钮从该视图中获取值:

@extends('layouts.app')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Szczegóły abonamentu</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('proforms.index') }}"> Wstecz</a>
            </div>
        </div>
    </div>

<div class="col-md-4">
<form action="/duplicate" method="get">
<div class="input-group">
<input type="button" value="$proform->id" name="duplicate" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wystaw fakturę</button>
</span>
</div>
</form>
</div>   

    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Data wystawienia:</strong>
                {{ $proform->proformdate }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Kontrahent:</strong>
                {{ $proform->user_id }}
            </div>
        </div>
       <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Data sprzedaży:</strong>
                {{ $proform->selldate }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Termin płatności:</strong>
                {{ $proform->paymentdate }}
            </div>
        </div>
                <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Forma płatności:</strong>
                {{ $proform->paymentmethod }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Miejsce wystawienia:</strong>
                {{ $proform->city }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Status:</strong>
                {{ $proform->status }}
            </div>
        </div>
       <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Uwagi:</strong>
                {{ $proform->comments }}
            </div>
        </div>
            <div class="pull-left" style="margin: 15px;">
                <h3>Pozycje proformy</h3>
            </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Nazwa towaru lub usługi:</strong>
                {{ $proform->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>PKWiU:</strong>
                {{ $proform->PKWIU }}
            </div>
        </div>
       <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Ilość:</strong>
                {{ $proform->quantity }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Jednostka:</strong>
                {{ $proform->unit }}
            </div>
        </div>
                <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Cena netto jednostki:</strong>
                {{ $proform->netunit }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Netto razem:</strong>
                {{ $proform->nettotal }}
            </div>
        </div>
       <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Stawka VAT:</strong>
                {{ $proform->VATrate }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Brutto jednostka:</strong>
                {{ $proform->grossunit }}
            </div>
        </div>
       <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Brutto razem:</strong>
                {{ $proform->grosstotal }}
            </div>
        </div>

    </div>
@endsection
<p class="text-center text-primary"><small>Artplus 2020</small></p>

这是我的路由器:

...
    Route::get('/duplicate', 'ProformController@duplicate');
  ...
ujv3wf0j

ujv3wf0j1#

您的路由名称应该与要注入的模型的模型匹配。在这种情况下,路线应具有 proform 争论。在您的情况下,模型没有绑定,返回一个空的intance。
如果它是重复的@kurt说你的路线应该是这样的

Route::get('/duplicate/{proform?}', 'ProformController@duplicate');

在这种情况下,它应该是可选的。
但我建议你像下面这样改变。为复制post创建post路由,并使用get路由显示表单

Route::get('/duplicate/{proform}', 'ProformController@duplicateForm')->name('proforms.duplicateForm');
Route::post('/duplicate', 'ProformController@duplicatePost')->name('proforms.duplicate');

控制器

public function __construct(Proform $proform)
{
   $this->proform = $proform;
}

public function duplicateForm(Request $request, Proform $proform)
{
    return view('the form view here', ['proform' => $proform]);
}

public function duplicatePost(Request $request)
{
    $proform = $this->proform->findOrFail($request->duplicate);

    //something like this
    $duplicated = $this->proform->create($proform->toArray());

    dump($duplicated);
}

复制表单

<div class="col-md-4">
<form action="{{ route('proforms.duplicate') }}" method="POST">
@csrf

<div class="input-group">
<input type="button" value="{{$proform->id}}" name="duplicate" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wystaw fakturę</button>
</span>
</div>
</form>
</div>

此外,在laravel中,可以使用以下函数代替 var_dump() ```
dd() //die and dump, same as var_dump();exit;
dump() //dump, same as var_dump();

相关问题