小编tif*_*ang的帖子

Laravel以递归方式在"app/database/migrations"文件夹上运行迁移

所以我的迁移文件夹看起来像这样,因为我有几十个表,它保持组织和清洁:

migrations/
  create_user_table.php
  relations/
  translations/
Run Code Online (Sandbox Code Playgroud)

我正在尝试刷新所有迁移和种子,但似乎我遇到了轻微的打嗝,我不知道工匠命令以递归方式运行迁移(即在relationstranslations文件夹中运行迁移).

我尝试添加,--path="app/database/migrations/*"但它吐出一个错误.有谁知道这个解决方案?

php database-migration laravel

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

Laravel:belongsToMany()不会在多对多表中获取字段

请考虑以下表格:

user
  id
  name

client
  id
  name

user_client
  user_id
  client_id
  rate
  ...
Run Code Online (Sandbox Code Playgroud)

我希望我的控制器能够获取user表中的所有字段,并且我还希望列出他们的客户端name以及rate之后.用户和客户端型号:

class User extends Eloquent {

    public function clients()
    {
        return $this->belongsToMany('Client', 'user_client');
    }

}

class Client extends Eloquent {

    public function users()
    {
        return $this->belongsToMany('User', 'user_client');
    }

}
Run Code Online (Sandbox Code Playgroud)

没有模型user_client.

我的摘录 UsersController@show

public function show($username) // foo.com/user/{$username}
{
  $user = User::where('username', '=', $username)->firstOrFail();
  $clients = User::find($user->id)->clients;

  return View::make('users.show', compact('user', 'clients'));
}
Run Code Online (Sandbox Code Playgroud)

虽然运行良好,但让我们看一下视图users/show.blade.php:

<h1>{{$user->name}}</h1>
@foreach($clients as $client)
  <p>{{$client->name}}, {{$client->rate}}</p> …
Run Code Online (Sandbox Code Playgroud)

many-to-many laravel

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

在Laravel json响应中使用访问器和mutator

所以我正在制作一个产生json响应而不是做的API View::make('foo', compact('bar')).

带刀片模板

我的控制器只返回Eloquent模型Users:

public function index()
{
    return User::all();
}
protected function getFooAttribute()
{
    return 'bar';
}
Run Code Online (Sandbox Code Playgroud)

我的视图将能够与foo属性(不是用户表中的字段)一起使用它.

@foreach($users as $user)
<p>{{$user->name}} {{$user->foo}}</p>
@endforeach
Run Code Online (Sandbox Code Playgroud)

使用Angular JS + json响应

但是,现在我没有使用刀片而是json使用Angular JS 抓取并显示它我无法做到这一点:

<p ng-repeat="user in users">{{user.name}} {{user.foo}}</p>
Run Code Online (Sandbox Code Playgroud)

有没有办法干净地打包json响应,以便我有:

[{"name": "John", "foo": "bar"}, ...]
Run Code Online (Sandbox Code Playgroud)

警告:我之前从未构建过API,去年12月我才开始编程/ web开发.这是我的尝试:

public function index()
{
  $response = User::all();
  foreach($response as $r)
  {
    $r->foo = $r->foo;
  }

  return $response;
}
Run Code Online (Sandbox Code Playgroud)

php json laravel angularjs laravel-4

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

用于递归结果的Laravel查询构建器?例如id,parent_id

所以我的数据结构如下:

id|parent_id|name
1 |null     |foo
2 |1        |bar
3 |2        |baz
Run Code Online (Sandbox Code Playgroud)

所以基本上foo->bar->baz.我很难理解如何使用laravel的查询构建器来获取子行的行,然后是它的祖先(直到parent_id == null).这可以用laravel完成吗?我已经完成了一些研究,Postgres RECURSIVE虽然MySQL没有(Postgres递归查询在遍历parent_id时更新字段的值).

我相信MySQL有类似的东西:如何在MySQL中进行递归SELECT查询?

但是我如何在Laravel中实现这个呢?

我的开始代码基本上是使用查询范围,但我只是没有做到正确:

Model::select('name')->getParent(3); //get baz and the ancestors of baz
protected function scopeGetParent($id) {
  $parent = Model::where('id', '=', $id);
  return $query->getParent($parent->parent_id);
}
Run Code Online (Sandbox Code Playgroud)

我想要的结果是:

name
baz
bar
foo
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

php mysql recursion laravel-4

6
推荐指数
3
解决办法
6407
查看次数

Laravel分块不会减少PHP内存使用量

所以我一直在Eloquent尝试laravel的分块,但我遇到了一个问题.请考虑以下代码(我的问题的更简化版本):

$data = DB::connection('mydb')->table('bigdata')
->chunk(200, function($data) {
  echo memory_get_usage();
  foreach($data as $d) {
    Model::create(
      array(
        'foo' => $d->bar,
        ...
        //etc
      ));
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,当我运行以下代码时,我的内存输出如下所示:

19039816
21490096
23898816
26267640
28670432
31038840
Run Code Online (Sandbox Code Playgroud)

所以没有跳进去php.ini改变memory_limit价值任何线索为什么它不起作用?根据文档:"如果你需要处理很多(数千)Eloquent记录,使用chunk命令将允许你不用吃掉你所有的RAM".

我试过unset($data)foreach函数,但它没有帮助.关于我如何利用chunk或者误解了它的作用的任何线索?

php memory-management chunking laravel

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

使用Laravel数据透视表生成干净格式化的json

所以我正在运行具有本地化的表,但我将字符串保存在单独的"主"表中.

假设我有一个每个实体的表:

Products
  id
  price
  ...
Run Code Online (Sandbox Code Playgroud)

翻译表

Translations
  id
  name
  description
  ...
Run Code Online (Sandbox Code Playgroud)

关系表

product_translation
  product_id
  translation_id
  lang --enum('en', 'es', 'fr',...)
Run Code Online (Sandbox Code Playgroud)

问题:这不是那么漂亮的json

所以我创建了一个BaseModel使用多对多关系的东西:

public function translations()
{
  return $this
  ->belongsToMany('Translation')
  ->where('lang', '=' App::getLocale());
}
Run Code Online (Sandbox Code Playgroud)

所以我可以Product::with('translations')->get()为我的json 做.然而...

我想要的

{
    "name": "Foo",
    "description": "Bar",
    "price": "1000",
    "stock": "10",
}
Run Code Online (Sandbox Code Playgroud)

我得到了什么

{
  "id": "1",
  "price": "1000",
  "stock": "10",
  "translations": [
    {
      "id": "1",
      "name": "Foo",
      "description": "Bar",
      "pivot": {
        "product_id": "1",
        "translation_id": "1"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,输出的包袱太多了.如何限制我想要生成所需json输出的字段?

编辑:发现https://github.com/laravel/framework/issues/745 …

json laravel eloquent

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

Laravel includesToMany只返回特定的列

所以我通过透视表在“用户”和“照片”之间有很多关系user_photo。我belongsToMany('Photo')在用户模型中使用。但是这里的麻烦是我的Photo表中有很多我不需要的列(特别是在json响应期间)。因此,一个例子是:

//Grab user #987's photos:
User::with('photos')->find(987);
//json output:
{
  id: 987,
  name: "John Appleseed",
  photos: {
    id: 5435433,
    date: ...,
    name: 'feelsgoodman.jpg',
    ....// other columns that I don't need
  }
}
Run Code Online (Sandbox Code Playgroud)

是否可以修改此方法,以使Photos模型仅返回可接受的列(例如由数组指定['name', 'date'])?

User.php

public function photos()
{
  return $this->belongsToMany('Photo');
}
Run Code Online (Sandbox Code Playgroud)

注意:仅执行一次操作时,我只想选择特定的列User->belongsToMany->Photo。当做类似的事情时Photo::all(),是的,我希望所有列都一样。

编辑:我已经尝试过在Laravel Eloquent中使用“ with()”函数来获取特定的列,但仍在选择列。也是https://github.com/laravel/laravel/issues/2306

php model pivot-table laravel

5
推荐指数
2
解决办法
4873
查看次数

以类似的方式打印树结构,存储缩进字符串(由┣,┃,┗组成)

我想为每个对象存储"节点缩进字符串",如下所示:

foo
?bar
??baz
? ?qux
?  ?quux
?  ?corge
?fizz
?buzz
Run Code Online (Sandbox Code Playgroud)

给出每个对象的数据:

objects = [
{'id':1,'parent_id':null, 'name':'foo'}
{'id':2,'parent_id':1, 'name':'bar'}
];
Run Code Online (Sandbox Code Playgroud)

请注意,我不想打印任何内容,我只想将indent每个对象的字符数组计算出来:

{'id':6,'parent_id':4, 'name':'corge', 'indent':['?',' ',' ','?']}

到目前为止,我只能用空格缩进它们但没有"管道",我很难想出一个解决方案.有帮助吗?

如果有帮助,我正在使用JS和Angular.

编辑:根据要求我到目前为止的代码.我最初没有发布这个,因为我觉得这是一个错误的基础/方法来构建.它的工作原理非常简单:对于每个对象,计算它的祖先并相应地添加它们" ".

// go through all our objects and set their indent strings
setIndents = function()
{
    for (var x in objects) {
        var o = objects[x];
        o.nodes = [];

        // push space character for amount of ancestors
        numParents = countParents(o, 0);
        for (var i = 0; …
Run Code Online (Sandbox Code Playgroud)

javascript tree

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

在带有rowspan的<tr>上重新读取?

这样一个简单的例子:

<tr ng-repeat="user in users">
  <td rowspan="2">{{user.name}}<td>
  <td>foo</td>
</tr>
<tr>
  <td>{{user.age}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

这样的东西会如何在Angular中起作用?我已经尝试了上面的内容,它只是不起作用而不会弄乱布局.我也尝试<tr>用a 包裹两者,<span>它也没有帮助.这是出于ng-repeat's功能吗?

编辑:只保留HTML,不使用JS代码.

html html-table angularjs

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