在不是输入值的情况下在前端更新后保存更新的表值Laravel

时间:2018-09-20 16:39:24

标签: mysql laravel

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

用户迁移

:check_ins

Homecontroller

#<ActiveRecord::AssociationRelation

Home.blade.php文件

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();
        });
    }

1 个答案:

答案 0 :(得分:0)

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

例如:

public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         final Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_preferences, container, false);

    final SeekBar s1 = (SeekBar) root.findViewById(R.id.s1Bar);

    Button button = (Button) root.findViewById(R.id.savebutton);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int spi = s1.getProgress();

            String fileContents = String.valueOf(spi);

            try {
                FileOutputStream outputStream = getActivity().openFileOutput("myText.txt", Context.MODE_PRIVATE);
                outputStream.write(fileContents.getBytes());
                outputStream.close();
                Toast.makeText(getContext(), "File saved!", Toast.LENGTH_SHORT).show();}
            catch (Exception e){

                e.printStackTrace();
                Toast.makeText(getContext(), "File not saved!", Toast.LENGTH_SHORT).show();

            }
        }
    });

    s1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) {
            spicinessText.setText("Spiciness: "+i);
            int spiciness = i;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    try {

        FileInputStream inputStream = getActivity().openFileInput("myText.txt");


        String m = inputStream.toString();

        int c = Integer.parseInt(m);

        s1.setProgress(c);

        Toast.makeText(getContext(), "File Received!", Toast.LENGTH_SHORT).show();

    } catch (Exception e){

        e.printStackTrace();

        Toast.makeText(getContext(), "File not received", Toast.LENGTH_SHORT).show();

    }

    return root;
}

否则,您可以创建自己的用户模型或仅使用DB Facade更新数据:

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

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