小编use*_*140的帖子

Laravel Eloquent只有在做出更改后才会更新

有没有办法使用雄辩模型更新Laravel中的记录,只要对该记录进行了更改?我不希望任何用户一遍又一遍地请求数据库没有正当理由,只需按下按钮即可保存更改.我有一个javascript功能,根据页面中是否有更改,启用和禁用保存按钮,但我想知道是否可以确保在服务器端执行此类功能.我知道我可以自己完成它(意思是:没有吸引框架的内部功能)只是通过检查记录是否有变化,但在这样做之前,我想知道Laravel雄辩的模型是否已经处理好了那,所以我不需要重新发明轮子.

这是我用来更新记录的方式:

$product = Product::find($data["id"]);
$product->title = $data["title"];
$product->description = $data["description"];
$product->price = $data["price"];
//etc (string values were previously sanitized for xss attacks)
$product->save();
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent

69
推荐指数
3
解决办法
7万
查看次数

如何在 Laravel 中创建模型?

我在正确理解 Laravel 模型时遇到一些困难。我们以框架提供的默认模型为例。

这是模型的全部内容:

用户.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

}
Run Code Online (Sandbox Code Playgroud)

我理解之间发生的一切{}。但有些事情我完全迷失了:

比如,如何设置:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
Run Code Online (Sandbox Code Playgroud)

如果我要创建一个模型来表示表products而不是表,我是否需要它users …

php laravel

11
推荐指数
2
解决办法
8万
查看次数

如何在Ruby中拆分字符串变量

我有这个迷你功能:

def dummy_string_management(val)
    parts = val.split(' ')
    return "#{parts[0]}" + " -done!"
end
Run Code Online (Sandbox Code Playgroud)

我正在调用这样的函数:

myString = "This is a string"
<%= dummy_string_management(myString) %>
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

undefined method `split' for This is a string
Run Code Online (Sandbox Code Playgroud)

如何拆分作为函数参数发送的变量?

ruby ruby-on-rails

-5
推荐指数
1
解决办法
1502
查看次数

标签 统计

laravel ×2

php ×2

eloquent ×1

ruby ×1

ruby-on-rails ×1