小编M K*_*M K的帖子

Laravel Schema onDelete设置为null

无法弄清楚如何在Laravel中的表上设置正确的onDelete约束.(我和SqLite合作)

$table->...->onDelete('cascade'); // works
$table->...->onDelete('null || set null'); // neither of them work
Run Code Online (Sandbox Code Playgroud)

我有3次迁移,创建了gallery表:

Schema::create('galleries', function($table)
{
    $table->increments('id');
    $table->string('name')->unique();
    $table->text('path')->unique();
    $table->text('description')->nullable();
    $table->timestamps();
    $table->engine = 'InnoDB';
});
Run Code Online (Sandbox Code Playgroud)

创建图片表:

Schema::create('pictures', function($table)
{
    $table->increments('id');
    $table->text('path');
    $table->string('title')->nullable();
    $table->text('description')->nullable();
    $table->integer('gallery_id')->unsigned();
    $table->foreign('gallery_id')
        ->references('id')->on('galleries')
        ->onDelete('cascade');
    $table->timestamps();
    $table->engine = 'InnoDB';
});
Run Code Online (Sandbox Code Playgroud)

将图库表链接到图片:

Schema::table('galleries', function($table)
{
    // id of a picture that is used as cover for a gallery
    $table->integer('picture_id')->after('description')
        ->unsigned()->nullable();
    $table->foreign('picture_id')
        ->references('id')->on('pictures')
        ->onDelete('cascade || set null || null'); // neither of them works
});
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误.此外,即使"级联"选项也不起作用(仅在图库表上).删除图库会删除所有图片.但删除封面图片,不会删除图库(用于测试目的).

由于即使"级联"未被触发,我"设置空"也不是问题.

编辑(解决方法):

阅读完这篇 …

php laravel laravel-migrations

92
推荐指数
4
解决办法
7万
查看次数

更改node_modules位置

有没有办法更改node_modules文件夹位置?

例如:

- dir1
- dir2
- node_modules
Run Code Online (Sandbox Code Playgroud)

至:

- dir1
- dir2
    - node_modules
Run Code Online (Sandbox Code Playgroud)

node.js npm

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

Laravel默认订单

是否有一种干净的方法可以默认情况下某些模型由某个属性进行排序?它可以通过扩展laravel的QueryBuilder来工作,但是为了这样做,你将不得不重新连接它的一些核心功能 - 糟糕的做法.

原因

这样做的要点是 - 我的一个模型被其他许多模型重复使用,现在你不得不一遍又一遍地使用它.即使使用封闭器 - 你仍然需要调用它.能够应用默认排序会更好,因此使用此模型并且不提供自定义排序选项的每个人都将接收按默认选项排序的记录.在这里使用存储库不是一个选项,因为它急于加载.

扩展基础模型:

protected $orderBy;
protected $orderDirection = 'ASC';

public function scopeOrdered($query)
{
    if ($this->orderBy)
    {
        return $query->orderBy($this->orderBy, $this->orderDirection);
    }

    return $query;
}

public function scopeGetOrdered($query)
{
    return $this->scopeOrdered($query)->get();
}
Run Code Online (Sandbox Code Playgroud)

在你的模型中:

protected $orderBy = 'property';
protected $orderDirection = 'DESC';

// ordering eager loaded relation
public function anotherModel()
{
    return $this->belongsToMany('SomeModel', 'some_table')->ordered();
}
Run Code Online (Sandbox Code Playgroud)

在你的控制器中:

MyModel::with('anotherModel')->getOrdered();
// or
MyModel::with('anotherModel')->ordered()->first();
Run Code Online (Sandbox Code Playgroud)

php laravel

36
推荐指数
6
解决办法
4万
查看次数

Laravel Redirect :: withInput()

正如标题所说 - 我正在尝试使用输入数据重定向回上一页,如下所示:

return Redirect::back()->withInput();
Run Code Online (Sandbox Code Playgroud)

它适用于常规输入,但不适用于文件!这有解决方法吗?这样在重定向之后,再次选择前一个文件.

php redirect file laravel

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

Gulpjs连接和文件名

我正在寻找一种方法将文件名作为注释插入流中的每个文件.因此,在连接的目标文件中,您将拥有文件路径的注释行.

它现在做了什么:

pseudocode: concat(file1, file2) 
# output: 
# contents of file 1
# contents of file 2
Run Code Online (Sandbox Code Playgroud)

我想要实现的目标:

pseudocode: concat(add_comments(file1, file2))
# output: 
# // file1
# contents of file 1
# // file2
# contents of file 2
Run Code Online (Sandbox Code Playgroud)

javascript gulp

6
推荐指数
1
解决办法
2303
查看次数

Coffeescript类共享属性

我在coffeescript中发现了一个非常奇怪的行为.

class Foo
    list: []
    add: (val)->
        @list.push(val)


x = new Foo()
x.add(1)
console.log(x.list.length) // 1

y = new Foo()
y.add(1)
console.log(y.list.length) // 2
Run Code Online (Sandbox Code Playgroud)

所以当你看到@list属性以一种奇怪的方式在两个类实例之间共享时.在coffeescript中,我以前从未遇到类似的问题.

coffeescript

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

从html中提取css

我正在寻找一种干净的方法来抓取并删除<style></style>标签之间的所有CSS .例如:

<style>
foo
</style>

content

<style>
bar
</style>

here
Run Code Online (Sandbox Code Playgroud)

在进程结束时,我应该有一个字符串content\nhere(样式标记之间的所有内容)和样式标记之间的匹配数组['foo', 'bar']或类似的东西.我尝试了很多不同的正则表达式方法,似乎没有一个对我有用.(虽然我不是正则表达式.)

php regex

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

将变量注入闭包

在没有将它绑定到窗口对象的情况下,实现这样的东西的正确方法是什么?

x = function(fn)
{
    foo = 'bar';
    fn();
}

x(function()
{
    console.log(foo) // error happens here
});
Run Code Online (Sandbox Code Playgroud)

在PHP中,您可以通过"use"指令.

$foo = 'bar';
$fn = function() use($foo)
{
    echo $foo; // bar
}
Run Code Online (Sandbox Code Playgroud)

编辑:我改变了一段代码.我想要实现的是,在闭包中使一个声明的变量可用,而不需要在那里传递它.

foo = 'bar';
console.log(foo) // bar
console.log(window.foo) // bar
console.log(this.foo) // bar
Run Code Online (Sandbox Code Playgroud)

我希望实现相同的效果,另一个对象被绑定为"this".

javascript php closures

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

正则表达式 - 逃避'

我正在尝试解析一个字符串并提取一些值.

突然,我在这里坚持使用符号:

这是一些代码,用于描述我的情况:

// example string
$string = "@set('var', 'value')";

// regex that I'm using to extract the values
$regex = '@set\((.+),(.+)\)/'

// result that I'm getting
array("'var'", "'value'")

// desired result
array("var", "value")
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

php regex

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