小编Kim*_*ens的帖子

如果集合包含外键,则检查Laravel

我想知道是否有一个函数或其他东西,你可以从集合中获得一个元素而不是主键...例如,如果投票有一个外键'user_id',我该如何检查?在laravel doc上,只有一个示例通过使用contains()来检查主键.谁能帮我吗?

检查是否存在id = 2的投票的示例

@foreach($projects as $project)
  @if ($project->votes->contains(2))
  //
  @endif
@endforeach
Run Code Online (Sandbox Code Playgroud)

我想要检查一下是否有一个'user_id'=签名用户ID的投票

@foreach($projects as $project)
  @if ($project->votes->contains('user_id' == Auth::id()))
  //
  @endif
@endforeach
Run Code Online (Sandbox Code Playgroud)

php mysql collections laravel eloquent

7
推荐指数
1
解决办法
3323
查看次数

使用$ set中的变量使用node.js更新mongodb

我正在制作投票系统,投票是通过链接完成的.在我的index.js中,我得到了所需的值并将它们放在变量中."type"变量代表我的mongodb中需要更新的字段,我把它放在一个变量中,因为它取决于点击了哪个链接.

现在在$ set函数中,他们需要db字段和一个新值,因为我使用变量但我的"type"变量不起作用.当我去我的mongodb时,会创建一个名为"type"的新表.怎么解决这个问题?

router.get('/vote/:Id/:Value/:Type', function(req, res) {
var db = req.db;
var id = req.params.Id;
var type = req.params.Type;
var value = parseInt(req.params.Value);
var newValue = value + 1;
var collection = db.get('games');

collection.update(
    {"_id" : id},
    {$set: {type: newValue}}
, function (err, doc) {
    if (err) {
        res.send("There was a problem");
    }
    else {
        res.location("../../../admin");
        res.redirect("../../../admin");
    }
});
Run Code Online (Sandbox Code Playgroud)

});

javascript mongodb node.js

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

Laravel voting system

I am implementing a voting system in my laravel school project, it will be an online school platform where students can upload projects and other students can vote on the published content.

Somehow what I have done isn't working... Here is my code so far, hope someone can help me, and maybe help me on the way how to show the number of votes on a project.

这是我的数据库迁移:

Schema::create('votes', function($table){
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->integer('project_id')->unsigned();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('project_id')->references('id')->on('projects');
});
Run Code Online (Sandbox Code Playgroud)

我的模特:

Class Vote …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel

2
推荐指数
1
解决办法
2483
查看次数

标签 统计

laravel ×2

mysql ×2

php ×2

collections ×1

eloquent ×1

javascript ×1

mongodb ×1

node.js ×1