Php 如何更新laravel中行的状态?

Php 如何更新laravel中行的状态?,php,laravel,Php,Laravel,我正在尝试使用按钮更新我的基金表中某一行的状态。 这是我的控制器: public function changeStatus(Funds $funds) { if ($funds > status == false) { $funds->status = true; $funds->update(['status' => $funds->status]); return redirect('funds,ind

我正在尝试使用按钮更新我的
基金
表中某一行的状态。 这是我的控制器:

public function changeStatus(Funds $funds)
{
    if ($funds > status == false) {
        $funds->status = true;
        $funds->update(['status' => $funds->status]);

        return redirect('funds,index')->with('success', 'Good Job, Fund mark is done!');
    } else {
        $funds->status = true;
        $funds->update(['status' => $funds->status]);

        return redirect('funds.index')->with('success', ' Fund is pending!');
    }
}
public function changeStatus(Funds $funds)
{
    if ($funds->status == false) {
        $funds->update(['status' => true]);

        return redirect('funds.index')->with('success', 'Good Job, Fund mark is done!');
    } else {
        $funds->update(['status' => false]);

        return redirect('funds.index')->with('success', ' Fund is pending!');
    }
}
然后我为
FundsController@changeStatus

Route::patch('transactions/{funds}/completed', 'FundsController@changeStatus');
我在index.blade.php文件中使用的HTML代码

                <div class="card-body">
                   <table class="table table-bordered">
                     <thead>                  
                       <tr>
                         <th style="width: 10px">Account Number</th>
                         <th>Other Account Number</th>
                         <th>Remarks</th>
                         <th>acc_type</th>
                         <th>Status</th>
                         <th>Actions</th>
                       </tr>
                     </thead>
                     <tbody>
                     @foreach($funds as $fund)
                       <tr>
                         <td> {{ $fund->accno }} </td>
                         <td>{{ $fund->accnumber }}</td>
                         <td> {{ $fund->Remarks }} </td>
                         <td>{{ $fund->acc_type }}</td>
                         <td>{{ $fund->status }}</td>
                         <td>
                             {!! Form::model($fund, ['method' => 'PATCH', 'route' => ['funds.changeStatus', $fund->id]]) !!}
                             <button type="submit" class="btn btn-info">{{ $fund->status == false ? 'Marked Pending' : 'Marked complete' }}</button>
                             {!! Form::close() !!}
                         </td>
                       </tr>
                     @endforeach
                     </tbody>
                   </table>
 ```


but I ended up with this error:
> Undefined property: stdClass::$slug (View: C:\Users\user pc\Desktop\dkn\resources\views\funds\index.blade.php)

Where did I go wrong and how can I update funds status using this method?

帐号
其他帐号
评论
acc_型
地位
行动
@foreach($基金作为$基金)
{{$fund->accno}
{{$fund->accnumber}
{{$fund->备注}
{{$fund->acc_type}
{{$fund->status}
{!!Form::model($fund,['method'=>'PATCH','route'=>['funds.changeStatus',$fund->id]])
{{$fund->status==false?'Marked Pending':'Marked complete'}
{!!Form::close()!!}
@endforeach
```
但我最后犯了一个错误:
>未定义的属性:stdClass::$slug(视图:C:\Users\user pc\Desktop\dkn\resources\views\funds\index.blade.php)
我哪里出错了?如何使用此方法更新资金状态?

确保查看地址文件正确。

确定您应该首先尝试创建正确的表单

首先,我建议您使用命名路由:

Route::patch('transactions/{funds}/completed', 'FundsController@changeStatus')->name('funds.changeStatus');
这将使你更容易在表格中找到正确的路线。 那么表单应该如下所示:

Form::model($fund, ['method' => 'PATCH', 'route' => ['funds.changeStatus', $fund->id]]);
如果要使用slug(而不是id):

如果要使用slug,请确保将以下方法添加到基金模型中:

public function getRouteKeyName()
{
    return 'slug';
}
然后在控制器中:

public function changeStatus(Funds $funds)
{
    if ($funds > status == false) {
        $funds->status = true;
        $funds->update(['status' => $funds->status]);

        return redirect('funds,index')->with('success', 'Good Job, Fund mark is done!');
    } else {
        $funds->status = true;
        $funds->update(['status' => $funds->status]);

        return redirect('funds.index')->with('success', ' Fund is pending!');
    }
}
public function changeStatus(Funds $funds)
{
    if ($funds->status == false) {
        $funds->update(['status' => true]);

        return redirect('funds.index')->with('success', 'Good Job, Fund mark is done!');
    } else {
        $funds->update(['status' => false]);

        return redirect('funds.index')->with('success', ' Fund is pending!');
    }
}


谢谢,我更正了返回视图,但这不是我面临的问题。你能为
'funds.index'
路线添加路线声明吗?如果($funds>status==false),你在
if($funds>status==false)
应该是
如果($funds->status==false)
您确定返回索引刀片的控制器正在将
$fund
变量传递到查看文件吗?您的
changeStatus
方法也没有传递它。并且
重定向
帮助程序使用的路由不是刀片文件应该使用
返回视图()
$funds->slug应该是$fund->slug(这将删除您的错误)请您解释一下,当您将
$funds->slug
更改为
$fund->slug
时,有什么不起作用。我将表单更改为
{!!form::model($fund,['method'=>'PATCH','route'=>['funds.changeStatus',$fund]]){{$fund->status==false?'Marked Pending':'Marked complete'}{!!Form::close()!!}
但是我收到了这个错误,类stdClass的对象无法转换为字符串(视图:C:\Users\user pc\Desktop\dkn\resources\views\funds\index.blade.php)