小编Hel*_*815的帖子

Laravel:选择Eloquent Eager Loading的关系

我有两个数据库表:

帖子

$table->increments('id');
$table->integer('country_id')->unsigned();
$table->foreign('country_id')->references('id')->on('countries');
Run Code Online (Sandbox Code Playgroud)

国家

$table->increments('id');
$table->string('name', 70);
Run Code Online (Sandbox Code Playgroud)

我使用laravel作为后端.现在我想为我的前端实现过滤数据.因此,用户可以选择国家/地区名称,而laravel应仅使用具有指定名称的国家/地区的帖子来回答请求.

如何将此条件添加到我现有的分页查询中?我试过这个:

$query = app(Post::class)->with('country')->newQuery(); 
// ...
if ($request->exists('country')) {
        $query->where('country.name', $request->country);
}
// ...
Run Code Online (Sandbox Code Playgroud)

...导致以下错误:

Column not found: 1054 Unknown column 'country.name' in 'where clause' (SQL: select count(*) as aggregate from `posts` where `country`.`name` = Albania)
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel eloquent laravel-5

10
推荐指数
1
解决办法
7480
查看次数

Sequelize:检查并添加BelongsToMany(N到N)关系

我的应用程序有很多用户可以喜欢很多帖子(N到N).这就是我为我的模型(Sequelize Doc)分配以下"belongsToMany"关系的原因:

// Post Model
models.Post.belongsToMany(models.User, { through: models.PostLikes});

// User Model
models.User.belongsToMany(models.Post, { through: models.PostLikes});
Run Code Online (Sandbox Code Playgroud)

在我的Post Controller中,我得到了"likePost"函数的以下用例:

  1. 检查帖子是否存在.(似乎工作)
  2. 如果是,请检查用户是否已经喜欢此帖子.
  3. 如果不是,则在User和Post之间分配N到N的关系.(似乎工作)

    // User likes Post
    exports.likePost = async (req, res) => {
     const postToLike = await Post.findById(req.body.postId);
     // Check if the Post exists
     if (!postToLike) {
      return res.status(404).json({ error: { message: 'Post not found.' }});
     }
    
     // Did user already like the post?
     // HERE IS THE NOT WORKING PART:
     if (await req.user.hasPost(postToLike).isFulfilled()) { …
    Run Code Online (Sandbox Code Playgroud)

javascript mysql orm node.js sequelize.js

8
推荐指数
1
解决办法
372
查看次数

字体CSS转换

我想得到一个悬停动画,同时改变font-size和font-family.在字体大小转换完成后,我无法准确地更改字体系列.这甚至可能吗?

在此输入图像描述

是)我有的:

a{
 -webkit-transition: font-size .2s ease;
 -moz-transition: font-size .2s ease;
 -o-transition: font-size .2s ease;
 -ms-transition:font-size .2s ease;
 transition: font-size .2s ease;
}

a:hover{
 font-family: "MyHoverFont";
 font-size: 3em;
}
Run Code Online (Sandbox Code Playgroud)

我尝试了什么:

a{
 ...
 -webkit-transition: font-family.2s ease;
 -moz-transition: font-family .2s ease;
 -o-transition: font-family .2s ease;
 -ms-transition: font-family .2s ease;
 transition: font-family .2s ease;
}

a:hover{
 ...
 font-family: "MyHoverFont"
}
Run Code Online (Sandbox Code Playgroud)

html javascript css sass

4
推荐指数
2
解决办法
4008
查看次数

Laravel:使用 Eloquent ORM 的 N 到 N(mysql)关系

我有4张桌子:

// Table countries
+----+------+
| Id | Name |
+----+------+
|  1 | USA  |
|  2 | GB   |
+----+------+

// Table platforms
+----+---------+
| Id |  Name   |
+----+---------+
|  1 | Windows |
|  2 | Linux   |
+----+---------+

// Table users
+----+-------+------------+-------------+
| Id | Name  | country_id | platform_id |
+----+-------+------------+-------------+
|  1 | Admin |          1 |           1 |
|  2 | Test  |          2 |           1 |
+----+-------+------------+-------------+

// Table posts
+----+-----------+------------+-------------+---------+ …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel eloquent

3
推荐指数
1
解决办法
2216
查看次数

Sequelize:具有自引用的类别模型层次结构

我的 MySQL 数据库包含一个类别表(也许“父”列在这里是错误的方法):

+----+--------------+--------+
| id |     name     | parent |
+----+--------------+--------+
|  1 | Service      | null   |
|  2 | Lifestyle    | null   |
|  3 | Food & Drink | 2      |
|  4 | Beauty       | 2      |
+----+--------------+--------+
Run Code Online (Sandbox Code Playgroud)

如何使用 Sequelize 按以下形式构造和检索数据:

[
    {
        "id": 1,
        "name": "Service",
        "children": null
    },
    {
        "id": 2,
        "name": "Lifestyle",
        "children": [
                      {
                        "id": 3,
                        "name": "Food & Drink",
                        "children": null
                      },
                      {
                        "id": 4,
                        "name": "Beauty",
                        "children": null
                      }, …
Run Code Online (Sandbox Code Playgroud)

javascript mysql node.js express sequelize.js

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

Android:NullPointerException创建新的Intent

在创建新的Intent对象时,我总是得到Nullpointer异常.我的目的是创建一个新的Activity,它以"onCreate"方法打开一个新的View.

这是我的主要活动:

public class MainActivity extends Activity {
    public void activateTimer(){
        // some code ....           
        MyTimer timer = new MyTimer(MainActivity.this);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Timer类:

public class MyTimer extends MainActivity{
    private Context contextMain;
    public MyTimer(Context context){
        this.contextMain = context;
        // some more code ...
        openMyActivity(contextMain); 
}
public void openMyActivity(Context contextMain){
    // some code ...
    // The next line throws the NPE!
Intent intent = new Intent(contextMain, MyNotificationActivity.class);
this.startActivity(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的活动应该创建:

public class MyNotificationActivity extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception

0
推荐指数
1
解决办法
2060
查看次数

CSS / SVG / Animation:将变换原点应用于 svg 中的单个元素不起作用

我想使用animejs为svg的特定部分(多边形)设置动画。

例如,我希望每个单独的多边形围绕中心旋转或从 0.1 缩放到 1.0。

我的问题是,多边形不会围绕它们自己的中心旋转,而是围绕整个 svg 的左上角旋转。

我研究了这篇关于“SVG 元素的转换”的文章,并发现“transform-origin”与我的问题有关。但即使我设置了transform-origin: 50% 50%,每个多边形的动画原点也是整个 svg 的中心,而不是单个 svg 多边形的中心。

anime({
  targets: 'polygon, polyline',
  scale: [0.1, 1],
  opacity: [0.1, 1],
  rotate: '1turn',
  duration: 7000
});
Run Code Online (Sandbox Code Playgroud)
body, html{
  margin:0
}
svg, g {
  transform-origin: unset unset;
}

polygon, polyline {
  transform-origin: 50% 50%;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.min.js"></script>
<svg version="1.1"
	 id="heroSvg" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality"
	 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"   viewBox="0 0 1920 1300"
	 style="enable-background:new 0 0 1920 1300;" xml:space="preserve">
<style type="text/css">
	.st0{fill:url(#SVGID_1_);} …
Run Code Online (Sandbox Code Playgroud)

html javascript css svg anime.js

0
推荐指数
1
解决办法
1751
查看次数