【发布时间】:2019-11-10 09:30:31
【问题描述】:
我正在尝试从两个不同的表中显示、更新和删除记录。 这是显示控制器功能:
public function show($user_id)
{
//
$staffinfo = Staff::find($user_id); // Loading all data from
$userinfo = User::find($user_id);
return view('staff.view')
->with('staffinfo', $staffinfo)
->with('userinfo', $userinfo);
}
这里是 view.blade.php 代码:
<table class="table table-hover table-dark">
<tbody>
<tr>
<th scope="row">Name</th>
<td>{{ $staffinfo->name }}</td>
</tr>
<tr>
<th scope="row">Mobile</th>
<td>{{ $userinfo->mobile }}</td>
</tr>
<tr>
<th scope="row">Designation</th>
<td>{{ $staffinfo->designatin }}</td>
</tr>
</tbody>
</table>
控制器中的编辑和更新功能:
public function edit($user_id)
{
//
$staffinfo = Staff::find($user_id); // Loading all data from
$userinfo = User::find($user_id);
return view('staff.edit')
->with('staffinfo', $staffinfo)
->with('userinfo', $userinfo);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $user_id)
{
//
$staffinfo = Staff::find($user_id);
$staffinfo->name = $request->input('stfname');
$staffinfo->user_id = $request->input('stfid');
$staffinfo->designaton = $request->input('stfdesig');
$staffinfo->dob = $request->input('staffdob');
$staffinfo->save();
$userinfo = User::find($user_id);
$userinfo->user_id = $request->input('stfid');
$userinfo->user_type = $request->input('stfdesig');
$userinfo->mobile = $request->input('staffmobile');
$userinfo->email = $request->input('staffmail');
$userinfo->password = $request->input('staffpw');
$userinfo->save();
}
edit.blade.php 代码部分:
<h2>Update Staff</h2>
<form method="post" action="{{ route('staff.update', $staffinfo->user_id) }}" enctype="multipart/form-data">
@method('PATCH')
@csrf
<div class="form-row">
<div class="form-group col-md-3">
<label for="stfdesig">Designation</label>
<input type="text" name="stfdesig" value="{{ $staffinfo->designation }}" class="form-control" id="stfid"
</div>
<div class="form-group col-md-3">
<label for="stfid">ID</label>
<input type="text" name="stfid" value="{{ $staffinfo->user_id }}" class="form-control" id="stfid">
</div>
<div class="form-group col-md-6">
<label for="stfname">Name</label>
<input type="text" name="stfname" value="{{ $staffinfo->name }}" class="form-control" id="stfname">
</div>
<div class="form-group col-md-6">
<label for="stfmobile">Mobile</label>
<input type="text" name="stfmobile" value="{{ $userinfo->mobile }}" class="form-control" id="stfmobile">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
查看页面错误:
试图获取非对象的属性“名称”(查看: F:\xampp\htdocs\gchsc\resources\views\staff\view.blade.php)
编辑页面错误:
试图获取非对象的属性'user_id'(查看: F:\xampp\htdocs\gchsc\resources\views\staff\edit.blade.php)
【问题讨论】:
-
尝试像这样引用所有变量:$staffinfo['name']。
-
@BradGoldsmith 不,你不应该。 Laravel 为您提供了可以使用的对象,而不是数组。
-
@Riyad 您确定在这两种情况下都加载了
staffinfo(这是导致错误的那个)吗?结果可能是NULL,因此会导致错误。 -
@DouwedeHaan 是的,它已加载。我想知道使用单个表单查看多个表中的记录和编辑多个表的记录是否正确?我没有在表之间创建任何关系。我使用了 Elequent。
-
@Riyad 您尝试访问的字段是否存在于表中?
staffinfo是否包含 name 和 user_id 字段?
标签: laravel