我试图显示,更新和删除两个不同表中的记录。 这是显示控制器功能:
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)
答案 0 :(得分:0)
您可以使用compact()传递值
public function show($user_id){
$staffinfo = Staff::find($user_id);
$userinfo = User::find($user_id);
return view('staff.view', compact('staffinfo','userinfo'));
}
答案 1 :(得分:0)
我不确定->with()
是将数据传递到视图所需的方法。
正如Shanu所说,您可以使用紧凑型
或者您可以简单地将数据作为第二个参数传递给关联数组中的视图...
return view('staff.edit', [
'staffinfo' => $staffinfo,
'userinfo' => $userinfo
]);
答案 2 :(得分:0)
我认为我有问题,但无法解决。有 用户表和人员表中的user_id字段。如果从发送ID 查看index.blade.php中的链接,然后开始工作。
<a href="/staff/{{$info->id}}">View</a>
如果发送user_id,则它不起作用。显示以上错误。
<a href="/staff/{{$info->user_id}}">View</a>
如何从查看链接发送user_id表单?