【问题标题】:Laravel 5.8 show, update, delete record form two different tableLaravel 5.8 从两个不同的表中显示、更新、删除记录
【发布时间】: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


【解决方案1】:

您可以使用 compact() 传递值

public function show($user_id){

     $staffinfo = Staff::find($user_id);
     $userinfo = User::find($user_id);

     return view('staff.view', compact('staffinfo','userinfo'));
}

【讨论】:

  • 您可以使用各种方法将数据传递给视图,但这并不是解决手头问题的方法。
【解决方案2】:

我不确定-&gt;with() 是您需要将数据传递到视图的方法。

正如 Shanu 所说,你可以使用 compact。

或者您可以简单地将数据作为第二个参数传递给关联数组中的视图...

return view('staff.edit', [
    'staffinfo' => $staffinfo,
    'userinfo' => $userinfo
]);

【讨论】:

  • with 是向视图传递数据的有效方法。 Documentation
  • 很奇怪。我已经看到这个问题被问过几次人们在哪里使用with(),但它不起作用。呃,好吧!干杯。
【解决方案3】:
   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>

【讨论】:

    【解决方案4】:

    我想我有问题,但无法解决。有 用户表和人员表中的 user_id 字段。如果如果从 从 index.blade.php 查看链接然后就可以了。

    <a href="/staff/{{$info->id}}">View</a>
    

    如果是发送 user_id 则它不起作用。显示上述错误。

    <a href="/staff/{{$info->user_id}}">View</a>
    

    如何从视图链接发送 user_id 表单?

    【讨论】:

      猜你喜欢
      • 2018-02-19
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多