无法从db获取行,laravel

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

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

  1. public function duplicate(Request $request, Proform $proform)
  2. {
  3. var_dump($proform);
  4. var_dump($proform->id);
  5. $proform = DB::table('proforms')->where('id', $proform->id)->first();
  6. var_dump($proform);
  7. }

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

  1. var_dump($proform);

我得到这个身份证:

  1. object(App\Proform)[1349]
  2. protected 'fillable' =>
  3. array (size=24)
  4. 0 => string 'proformnumber' (length=13)
  5. 1 => string 'proformdate' (length=11)
  6. 2 => string 'id' (length=2)
  7. 3 => string 'selldate' (length=8)
  8. 4 => string 'user_id' (length=7)
  9. 5 => string 'paymentmethod' (length=13)
  10. 6 => string 'paymentdate' (length=11)
  11. 7 => string 'status' (length=6)
  12. 8 => string 'comments' (length=8)
  13. 9 => string 'city' (length=4)
  14. 10 => string 'autonumber' (length=10)
  15. 11 => string 'automonth' (length=9)
  16. 12 => string 'autoyear' (length=8)
  17. 13 => string 'name' (length=4)
  18. 14 => string 'PKWIU' (length=5)
  19. 15 => string 'quantity' (length=8)
  20. 16 => string 'unit' (length=4)
  21. 17 => string 'netunit' (length=7)
  22. 18 => string 'nettotal' (length=8)
  23. 19 => string 'VATrate' (length=7)
  24. 20 => string 'grossunit' (length=9)
  25. 21 => string 'grosstotal' (length=10)
  26. 22 => string 'form_id' (length=7)
  27. 23 => string 'currency_id' (length=11)
  28. public 'sortable' =>
  29. array (size=13)
  30. 0 => string 'proformnumber' (length=13)
  31. 1 => string 'proformdate' (length=11)
  32. 2 => string 'id' (length=2)
  33. 3 => string 'selldate' (length=8)
  34. 4 => string 'user_id' (length=7)
  35. 5 => string 'paymentmethod' (length=13)
  36. 6 => string 'paymentdate' (length=11)
  37. 7 => string 'status' (length=6)
  38. 8 => string 'comments' (length=8)
  39. 9 => string 'grosstotal' (length=10)
  40. 10 => string 'nettotal' (length=8)
  41. 11 => string 'form_id' (length=7)
  42. 12 => string 'currency_id' (length=11)
  43. protected 'connection' => null
  44. protected 'table' => null
  45. protected 'primaryKey' => string 'id' (length=2)
  46. protected 'keyType' => string 'int' (length=3)
  47. public 'incrementing' => boolean true
  48. protected 'with' =>
  49. array (size=0)
  50. empty
  51. protected 'withCount' =>
  52. array (size=0)
  53. empty
  54. protected 'perPage' => int 15
  55. public 'exists' => boolean false
  56. public 'wasRecentlyCreated' => boolean false
  57. protected 'attributes' =>
  58. array (size=0)
  59. empty
  60. protected 'original' =>
  61. array (size=0)
  62. empty
  63. protected 'changes' =>
  64. array (size=0)
  65. empty
  66. protected 'casts' =>
  67. array (size=0)
  68. empty
  69. protected 'classCastCache' =>
  70. array (size=0)
  71. empty
  72. protected 'dates' =>
  73. array (size=0)
  74. empty
  75. protected 'dateFormat' => null
  76. protected 'appends' =>
  77. array (size=0)
  78. empty
  79. protected 'dispatchesEvents' =>
  80. array (size=0)
  81. empty
  82. protected 'observables' =>
  83. array (size=0)
  84. empty
  85. protected 'relations' =>
  86. array (size=0)
  87. empty
  88. protected 'touches' =>
  89. array (size=0)
  90. empty
  91. public 'timestamps' => boolean true
  92. protected 'hidden' =>
  93. array (size=0)
  94. empty
  95. protected 'visible' =>
  96. array (size=0)
  97. empty
  98. protected 'guarded' =>
  99. array (size=1)
  100. 0 => string '*' (length=1)

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

  1. var_dump($proform->id);

之后呢

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

此为空(第三个)

  1. var_dump($proform);
  2. /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/app/Http/Controllers/ProformController.php:33:null
  3. /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/app/Http/Controllers/ProformController.php:37:null

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

  1. @extends('layouts.app')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Szczegóły abonamentu</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('proforms.index') }}"> Wstecz</a>
  10. </div>
  11. </div>
  12. </div>
  13. <div class="col-md-4">
  14. <form action="/duplicate" method="get">
  15. <div class="input-group">
  16. <input type="button" value="$proform->id" name="duplicate" class="form-control">
  17. <span class="input-group-prepend">
  18. <button type="submit" class="btn btn-primary">Wystaw fakturę</button>
  19. </span>
  20. </div>
  21. </form>
  22. </div>
  23. <div class="row">
  24. <div class="col-xs-12 col-sm-12 col-md-12">
  25. <div class="form-group">
  26. <strong>Data wystawienia:</strong>
  27. {{ $proform->proformdate }}
  28. </div>
  29. </div>
  30. <div class="col-xs-12 col-sm-12 col-md-12">
  31. <div class="form-group">
  32. <strong>Kontrahent:</strong>
  33. {{ $proform->user_id }}
  34. </div>
  35. </div>
  36. <div class="col-xs-12 col-sm-12 col-md-12">
  37. <div class="form-group">
  38. <strong>Data sprzedaży:</strong>
  39. {{ $proform->selldate }}
  40. </div>
  41. </div>
  42. <div class="col-xs-12 col-sm-12 col-md-12">
  43. <div class="form-group">
  44. <strong>Termin płatności:</strong>
  45. {{ $proform->paymentdate }}
  46. </div>
  47. </div>
  48. <div class="col-xs-12 col-sm-12 col-md-12">
  49. <div class="form-group">
  50. <strong>Forma płatności:</strong>
  51. {{ $proform->paymentmethod }}
  52. </div>
  53. </div>
  54. <div class="col-xs-12 col-sm-12 col-md-12">
  55. <div class="form-group">
  56. <strong>Miejsce wystawienia:</strong>
  57. {{ $proform->city }}
  58. </div>
  59. </div>
  60. <div class="col-xs-12 col-sm-12 col-md-12">
  61. <div class="form-group">
  62. <strong>Status:</strong>
  63. {{ $proform->status }}
  64. </div>
  65. </div>
  66. <div class="col-xs-12 col-sm-12 col-md-12">
  67. <div class="form-group">
  68. <strong>Uwagi:</strong>
  69. {{ $proform->comments }}
  70. </div>
  71. </div>
  72. <div class="pull-left" style="margin: 15px;">
  73. <h3>Pozycje proformy</h3>
  74. </div>
  75. <div class="col-xs-12 col-sm-12 col-md-12">
  76. <div class="form-group">
  77. <strong>Nazwa towaru lub usługi:</strong>
  78. {{ $proform->name }}
  79. </div>
  80. </div>
  81. <div class="col-xs-12 col-sm-12 col-md-12">
  82. <div class="form-group">
  83. <strong>PKWiU:</strong>
  84. {{ $proform->PKWIU }}
  85. </div>
  86. </div>
  87. <div class="col-xs-12 col-sm-12 col-md-12">
  88. <div class="form-group">
  89. <strong>Ilość:</strong>
  90. {{ $proform->quantity }}
  91. </div>
  92. </div>
  93. <div class="col-xs-12 col-sm-12 col-md-12">
  94. <div class="form-group">
  95. <strong>Jednostka:</strong>
  96. {{ $proform->unit }}
  97. </div>
  98. </div>
  99. <div class="col-xs-12 col-sm-12 col-md-12">
  100. <div class="form-group">
  101. <strong>Cena netto jednostki:</strong>
  102. {{ $proform->netunit }}
  103. </div>
  104. </div>
  105. <div class="col-xs-12 col-sm-12 col-md-12">
  106. <div class="form-group">
  107. <strong>Netto razem:</strong>
  108. {{ $proform->nettotal }}
  109. </div>
  110. </div>
  111. <div class="col-xs-12 col-sm-12 col-md-12">
  112. <div class="form-group">
  113. <strong>Stawka VAT:</strong>
  114. {{ $proform->VATrate }}
  115. </div>
  116. </div>
  117. <div class="col-xs-12 col-sm-12 col-md-12">
  118. <div class="form-group">
  119. <strong>Brutto jednostka:</strong>
  120. {{ $proform->grossunit }}
  121. </div>
  122. </div>
  123. <div class="col-xs-12 col-sm-12 col-md-12">
  124. <div class="form-group">
  125. <strong>Brutto razem:</strong>
  126. {{ $proform->grosstotal }}
  127. </div>
  128. </div>
  129. </div>
  130. @endsection
  131. <p class="text-center text-primary"><small>Artplus 2020</small></p>

这是我的路由器:

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

ujv3wf0j1#

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

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

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

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

控制器

  1. public function __construct(Proform $proform)
  2. {
  3. $this->proform = $proform;
  4. }
  5. public function duplicateForm(Request $request, Proform $proform)
  6. {
  7. return view('the form view here', ['proform' => $proform]);
  8. }
  9. public function duplicatePost(Request $request)
  10. {
  11. $proform = $this->proform->findOrFail($request->duplicate);
  12. //something like this
  13. $duplicated = $this->proform->create($proform->toArray());
  14. dump($duplicated);
  15. }

复制表单

  1. <div class="col-md-4">
  2. <form action="{{ route('proforms.duplicate') }}" method="POST">
  3. @csrf
  4. <div class="input-group">
  5. <input type="button" value="{{$proform->id}}" name="duplicate" class="form-control">
  6. <span class="input-group-prepend">
  7. <button type="submit" class="btn btn-primary">Wystaw fakturę</button>
  8. </span>
  9. </div>
  10. </form>
  11. </div>

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

展开查看全部

相关问题