【发布时间】:2020-05-02 19:25:02
【问题描述】:
我正在尝试在我的 Laravel 项目中创建刷新选项。我在我的CarsController 中创建了一个public function refresh()。这个想法是让created_at 获得当前时间,因为我的汽车是由orderBy('created_at'. 'desc')->get(); 订购的。所以这是我的public function refresh() 在我的CarsController:
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function refresh($id)
{
$car = Car::find($id);
$car->created_at = Carbon::now();
$car->save();
return redirect('/cars');
}
这是我在刀片中的Form:
{!!Form::open(['action' => ['CarsController@refresh', $car->id], 'method' => 'PUT'])!!}
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Refresh', ['class' => 'btn btn-success'])}}
{!!Form::close()!!}
这是在我的web.php 路线中:
Route::get('/cars/{id}', 'CarsController@refresh');
我做错了什么?请帮忙。谢谢!
【问题讨论】:
-
您在迁移文件中使用
$table->timestamps();吗? -
@user8555937 是的,我愿意
-
使用 Car::create() 创建记录后会自动插入时间戳;为什么要更新
created_at时间戳? -
@user8555937 我想在我的项目中创建
refresh选项。因此,当刷新帖子时,它将在帖子列表中排在第一位,因为我的public function index()是这样排序的orderBy('created_at', 'desc') -
你已经重载了 PUT 方法,而你的路由需要 GET 方法:)