小编Gab*_*bor的帖子

在Laravel 4.1中使用Eloquent ORM查询同一表中的关系

我刚刚发现了Laravel,并进入了Eloquent ORM.但我在以下一个小问题上遇到了磕磕绊绊.

我有三个表,其中包含以下结构和数据:

words

id | language_id | parent_id | word
-------------------------------------------
1  | 1           | 0         | Welcome
-------------------------------------------
2  | 2           | 1         | Bienvenue
-------------------------------------------

documents

id | title
---------------------
1  | Hello World
---------------------

documents_words

document_id | word_id
--------------------------
1           | 1
--------------------------
Run Code Online (Sandbox Code Playgroud)

如您所见,我们在单词表中有父/子关系.

文档模型定义如下

class Documents extends Eloquent {

protected $table = 'documents';

public function words()
{
    return $this->belongsToMany('Word', 'documents_words', 'document_id');
}

}
Run Code Online (Sandbox Code Playgroud)

和模型:

class Word extends Eloquent {

protected $table = 'words';

public function …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-4

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

Laravel Eloquent ORM在哪里和哪里有foreach

我想做下面的工作,这可能是不好的做法,我对Laravel来说很新,所以请让我知道!

我有一个名为的模型Files,它们Keywords通过数据透视表链接.

我正在运行以下代码,但我得到Undefined variable: keyword_id了错误.

$keyword_ids = array(148, 4);
$files    = new Files;

foreach($keyword_ids as $keyword_id)
{
    $files = $files->whereHas('keywords', function($query)
    {
        $query->where('id', '=', $keyword_id);
    });
}
Run Code Online (Sandbox Code Playgroud)

非常感谢 !

php

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

标签 统计

php ×2

eloquent ×1

laravel ×1

laravel-4 ×1