mysql - 当不是输入值时,在前端更新后保存更新的表值,Laravel

标签 mysql laravel

我正在创建一个游戏,并在我的用户表中为每个用户添加了一个名为“金钱”的值。 “money”的默认值为 100。我将该值传递给前端,以便玩家可以看到该值。在游戏过程中,该值会发生变化(假设从 100 变为 125)。我的问题是如何将该值保存并更新到用户表中。在文档中,似乎只能更新输入值,而这不是输入值。它只是一个改变了值的数字。

用户迁移

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->integer('money')->default(100);
            $table->rememberToken();
            $table->timestamps();
        });
    }

家庭 Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = DB::table('users')->where('money', 100)->first();
        return view('home')->with('users', $users);
        $users->save();
    }
}

Home.blade.php 文件

<div class="col-xs">
    <div class="amountOfMoney">{{$users->money}}</div>
</div>

最佳答案

检查并确保您有一个 User 模型,该模型应包含在 Laravel 安装中。如果是这样,您可以简单地使用 Eloquent相当容易地更新属性。

例如:

//Get user
$user = Users::where('money', '=', 100)->first();

//Update money value
$user->money = 125;
$user->save();

如果没有,您可以创建自己的用户模型或仅使用数据库外观更新数据:

$id = DB::table('users')->where('money', 100)->pluck('id')->first();
DB::table('users')->where('id', '=', $id)->update(['money' => 125]);

关于mysql - 当不是输入值时,在前端更新后保存更新的表值,Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429631/

相关文章:

Laravel 分页不适用于 group by 子句

mysql - 我可以在不使用过程/函数的情况下在 MySQL 中运行循环吗?

mysql - 嵌套的 MySQL 查询

php - 使用 php 出现 MySQL 错误

mysql - SQL中按索引计数

php - 如何处理电子邮件错误的重复输入?

mysql - mysql中的拆分功能

php - 类别名冲突 Laravel 4

php - Laravel 别名找不到类

php - Laravel 5 每个页面都会返回 404 错误