所以我使用相同的代码为我的控制器,其中一个不工作。我发现它是web.php / route:list。
如果我使用
Route::resource('/jaybills', 'Jay_BillsController');
尝试访问jaybills / {jay_bill} / edit时出现此错误
Trying to get property of non-object (View: C:\Users\hayes\Desktop\laravel\resources\views\jaybills\edit.blade.php)
我在控制器中使用了dd(),出于某些奇怪的原因我使用了它
public function edit(Jay_Bill $jay_bill)
{
$jay_bill = Jay_Bill::find($jay_bill->id);
return view('jaybills.edit', [
'jay_bill' => $jay_bill
]);
}
返回null。我在其他3个控制器中使用过这个并且运行正常。如果我手动输入id,那么代码工作正常......
如果我像这样手动添加web.php中的路由
Route::get('jaybills/{jay_bill}/edit', 'Jay_BillsController@edit');
Route::get('jaybills', 'Jay_BillsCOntroller@index');
然后代码按预期工作并返回我期望的数据而不是NULL ...
我会喜欢一些想法,因为我花了几个小时调试这个并得出这个结论并且不知道为什么或如何解决它。
更新 - 添加了控制器和视图和模型。
CONTROLLER
<?php
namespace App\Http\Controllers;
use App\Jay_Bill;
use Illuminate\Http\Request;
class Jay_BillsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$jay_bill = Jay_Bill::all();
return view('jaybills.index', [
'jay_Bill' => $jay_bill
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('jaybills.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$jay_bill = Jay_Bill::create([
'jay_bill_name' => $request->input('jay_bill_name'),
'jay_bill_account' => $request->input('jay_bill_account'),
'jay_bill_cost' => $request->input('jay_bill_cost'),
'jay_bill_due' => $request->input('jay_bill_due'),
'jay_bill_paid' => $request->input('jay_bill_paid')
]);
return redirect('bills/');
}
/**
* Display the specified resource.
*
* @param \App\Jay_Bill $jay_bill
* @return \Illuminate\Http\Response
*/
public function show(Jay_Bill $jay_bill)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Jay_Bill $jay_bill
* @return \Illuminate\Http\Response
*/
public function edit(Jay_Bill $jay_bill)
{
//
dd(Jay_Bill::find($jay_bill->id));
// return view('jaybills.edit', [
// 'jay_bill' => $jay_bill
// ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Jay_Bill $jay_bill
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Jay_Bill $jay_bill)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Jay_Bill $jay_bill
* @return \Illuminate\Http\Response
*/
public function destroy(Jay_Bill $jay_bill)
{
//
}
}
模型
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Jay_Bill extends Model
{
//
protected $table = "jay_bills";
protected $fillable = [
'jay_bill_name',
'jay_bill_account',
'jay_bill_cost',
'jay_bill_due',
'jay_bill_paid'
];
// Adds carbon to the view, so that its possible to use carbon's functions in blade view.
public function getJayBillDueAttribute()
{
return Carbon::parse($this->attributes['jay_bill_due']);
}
}
视图
@extends('layouts.app')
@section('content')
<form method="post" action="jaybills/{{$jay_bill->id}}">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label for="jay_bill_name">Bill Name</label>
<input type="text" class="form-control" name="jay_bill_name" id="jay_bill_name" placeholder="Enter Bill Name" value="{{ $jay_bill->jay_bill_name }}">
</div>
<div class="form-group">
<label for="jay_bill_account">Bill Account</label>
<input type="text" class="form-control" name="jay_bill_account" id="jay_bill_account" placeholder="Enter Bill Account" value="{{ $jay_bill->jay_bill_account }}">
</div>
<div class="form-group">
<label for="jay_bill_cost">Bill Cost</label>
<input type="number" step="0.01" class="form-control" name="jay_bill_cost" id="jay_bill_cost" placeholder="Enter Bill Cost" value="{{ $jay_bill->jay_bill_cost }}">
</div>
<div class="form-group">
<label for="jay_bill_due">Bill Due</label>
<input type="date" class="form-control" name="jay_bill_due" id="jay_bill_due" value="{{ $jay_bill->jay_bill_due->format('Y-m-d') }}">
</div>
<div class="checkbox">
<label>
@if($jay_bill->jay_bill_paid == 1)
<input type="hidden" name="jay_bill_paid" value="0" unchecked/>
<input type="checkbox" name="jay_bill_paid" value="1" checked> Paid
@else
<input type="hidden" name="jay_bill_paid" value="0" checked/>
<input type="checkbox" name="jay_bill_paid" value="1" unchecked> Paid
@endif
</label>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
所以如果你不确定是什么问题。基本上我有一个按钮去jaybills / {jayBill} / edit - jayBill基本上是数据的id。
然后我得到上面提到的错误,但我在另一个控制器/视图/模型中有相同的代码,它的工作原理。我发现如果我手动创建它工作的路由,如果我手动输入id到:: find(1);它也工作正常。
更新#2
我试过这个
dd(JayBill::find($jayBill->id));
这返回NULL。我在find函数中手动输入1,一切正常。我在其他控制器上尝试过dd(),它返回带有id的数据。为什么它不能从上面的代码中找到id?它不接受它?
更新#3
array:2 [▼
0 => Jay_Bill {#229 ▼
#table: "jay_bills"
#fillable: array:5 [▶]
#connection: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => "1"
这是因为您的路由参数格式错误,如果您想使用隐式模型绑定,您应该使用下划线将其称为jay_bill
,而不是jayBill
。
试试这个:
Route::resource('/jaybills', 'JayBillsController',[
'parameters' => [
'jaybills' => 'jaybill'
]]);
你必须将控制器$jayBill
更改为$jaybill
模型绑定不识别驼峰情况