无法从 DB,Laravel 获取行
Posted
技术标签:
【中文标题】无法从 DB,Laravel 获取行【英文标题】:Can't get row from DB, Laravel 【发布时间】:2020-11-21 05:05:37 【问题描述】:我想将表中的一行的值设置为第二个表和其中的行。我开始用这个制作控制器方法,因为我试图首先从视图中获取值:
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);
我正在从视图和这一行(FIRST)中获取值
var_dump($proform);
我用 id 得到这个:
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');
...
【问题讨论】:
你能把路线贴在这里吗? 是的,@aimme 是正确的,我假设您没有正确配置路由模型绑定。route/to/duplicate/proform
如果您尚未自定义路由模型绑定,则 proform
需要是您尝试在路由中解析的模型的 ID。
我编辑了帖子并添加了路线。
那是哪条路线?
Route::get('/duplicate', 'ProformController@duplicate');
【参考方案1】:
您的路由名称应与要注入的模型的模型相匹配。在这种情况下,路由应该有一个proform
参数。在您的情况下,模型未绑定并返回一个空实例。
如果它像@Kurt 所说的那样重复,你的路线应该是这样的
Route::get('/duplicate/proform?', 'ProformController@duplicate');
在这种情况下,它应该是可选的。
但我建议您像下面这样更改它。为重复的帖子创建一个帖子路由,并使用 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();
【讨论】:
之后出现错误 404 Route::get('/duplicate/proform', 'ProformController@duplicate'); 我已经更新了答案。这是因为您使用的是相同的获取路线,并且正在尝试将其设为可选。 假设您有 proform 模型,使用依赖注入再次更新。 您也遇到了这个问题。现已修复 value="$proform->id" 你可以使用 dd() 和 dump() 代替 var_dump以上是关于无法从 DB,Laravel 获取行的主要内容,如果未能解决你的问题,请参考以下文章
从 db 获取数据到 laravel 5.2 中的视图的问题
如何从 db 中的两个表中路由、获取和显示数据到 laravel 5.2 中的单个网页