如何在Laravel中使用控制器将数据更新到数据库-错误非静态方法 [英] How to update data to database using controller in Laravel - Error Non-static method

查看:44
本文介绍了如何在Laravel中使用控制器将数据更新到数据库-错误非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图使用控制器将编辑后的数据提交回数据库,但出现错误非静态方法Illuminate \ Database \ Eloquent \ Model :: update()不应被静态调用"我认为更新功能中的代码需要更改.

So Im trying to submit the edited data back to the database using the controller, yet I am getting the error "Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically" I think the code in the update function needs to be changed.

web.php文件

Route::post('/my-updated-routes', 'MyroutesController@update');

控制器

public function update(Request $request)
{
    Myroutes::update([ //updateing to myroutes table
        'start' => $request->start,
        'end' => $request->end,
        'waypoints' => implode(",", $request->waypoints)
    ]);
    return redirect('/my-saved-routes');
}

show.blade.php

show.blade.php

<form method="post" action="{{ url('/my-updated-routes') }}">
    {{ csrf_field() }}
    <div class="form-group">
        <label>Start Point</label>
        <input type="text" id="start" name="start" class="form-control" value="{{ $myroute->start }}" required/>
    </div>

    <div class="form-group">
        <label>End Point</label>
        <input type="text" id="end" name="end" class="form-control" value="{{ $myroute->end }}" required/>
    </div>

    <div>
    <label for="mode">Mode of Travel</label>
        <select id="mode" class="form-control" onchange="calculateAndDisplayRoute();">
          <option value="DRIVING" name="driving">Driving</option>
          <option value="WALKING" name="walking">Walking</option>
          <option value="BICYCLING" name="cycling">Cycling</option>
          <option value="TRANSIT" name="public-transport">Public Transport</option>
        </select>
    </div>

    <p>Note: Public Transport is only available for start and end points.</p>


    <div id="dynamicInput" class="form-group">
        <label>Additional Destinations</label>
        <input type="text" name="waypoints" class="form-control" autocomplete="on" value="{{ $myroute->waypoints }}">
    </div>

    <input type="button" class="btn btn-secondary" value="+" onClick="addInput('dynamicInput');" style="padding:0 10px;">               
    </br></br>
    <input type="button" id="calc-route" style="color:#2b2b2b" class="btn btn-light" value="Calculate Route"/>

    <input type="submit" id="update-route" class="btn btn-success" value="Update"/>
    <input type="button" class="btn btn-danger" value="Delete"/>

</form> <!-- end of form -->    

推荐答案

问题是您到底想在控制器中更新什么?

The problem is what do you exactly want to update in the controller?

更新方法是非静态方法,您不能使用(::)"pamayim nikodatiyim"来调用它.这意味着您必须从数据库中获取要更新的行,然后调用update方法.

the update method is none-static method And you can't call it with (::)"pamayim nikodatiyim". And it means you must get the row that you want to update from database and then call the update method.

要做到这一点,您有两种方法:

to do that you have two ways:

通过laravel模型绑定,您可以在此处阅读文档: https://laravel.com/docs/5.6/routing#route-parameters

by laravel model binding, you can read the docs here: https://laravel.com/docs/5.6/routing#route-parameters

或者您可以只发送要更新的行的ID,检索数据,然后调用更新方法:

Or you can just send the id of the row that you want to update, retrieve the data and then call the update method:

$data = YourModelName::whereId($request['row_id'])->first();

$data->update([ //updateing to myroutes table
        'start' => $request->start,
        'end' => $request->end,
        'waypoints' => implode(",", $request->waypoints)
        ]); 

如果不指定要更新的行,则无法更新.这没有任何意义.

You can not update without specifying which row you want to update. it doesn't make any sense.

这篇关于如何在Laravel中使用控制器将数据更新到数据库-错误非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆