来自数据透视表的数据通过连接laravel

时间:2014-10-23 23:01:59

标签: php laravel eloquent blade

您好我试图通过名为tagprojects的数据透视表显示项目名称。不幸的是,我无法实现它。我会告诉你我尝试过的东西。首先,我将向您展示名为ProjectController.php的控制器:

public function get_results($keyword){

    $projects=Project::search2($keyword);
    //die(print_r($tagprojects));

    return View::make('user.projects.results')->with('project', 'Offertes - Zoek resultaten')
    ->with('projects', $projects);
}

search2()方法在我的模型中定义,名为Project.php:

public static function search2($keyword){
    //return static::DB::table('tagprojects')
    $keyword='ta';
    $result=DB::table('tagprojects')
        ->join('projects', 'tagprojects.id_project', '=', 'projects.id')
        ->join('tags', 'tagprojects.id_tag', '=', 'tags.id')
        ->where('tags.tag_name', 'LIKE', '%'.$keyword.'%')->get();

    /*
    $result=DB::table('tagprojects')
        ->join('projects', 'tagprojects.id_project', '=', 'projects.id')
        ->join('tags', 'tagprojects.id_tag', '=', 'tags.id')
        ->select( 'projects.project_name')
        ->where('projects.project_name', 'LIKE', '%'.$keyword.'%')
        ->where('tags.tag_name', 'LIKE', '%'.$keyword.'%');
     */ 
        //->paginate(3);
        //->get();
    return $result;

}

这是我的观点,名为results.blade.php:

@foreach ($projects as $tp)
    <li>
    {{$tp->id_tag}}
    {{--    {{$tp->project['project_name']}} --}}
    </li>
@endforeach

好{{$ tp-&gt; id_tag}}有效,但实际上我想查看项目名称。正如您在我的代码中所看到的,我已经尝试过{{$ tp-&gt; project [&#39; project_name&#39;]}}。不幸的是,这没有用,因为我得到以下错误:

Undefined property: stdClass::$project (View: C:\xampp\htdocs\offerteTool\app\views\user\projects\results.blade.php)

我也尝试了{{$ tp-&gt; project-&gt; project_name}}但是我也收到了同样的错误。但后来我尝试了{{die(print_r($ projects))}},输出结果为:

Array ( [0] => stdClass Object ( [id] => 6 [id_tag] => 6 [id_project] => 3 [project_name] => Velma [project_description] => Dormouse! Turn that Dormouse out of sight; and an Eaglet, and several other curious creatures. Alice led the way, was the first day,' said the Caterpillar. Alice folded her hands, and began:-- 'You. [hour] => 96 [created_at] => 2014-10-16 08:52:08 [updated_at] => 2014-10-16 08:52:08 [tag_name] => accountancy ) ) 1

如您所见,输出显示了列project_name,但出于某种原因,我无法显示project_name列。有谁可以帮助我吗?很高兴我在等你的回复。无论如何,谢谢你的回答。

2 个答案:

答案 0 :(得分:0)

您可以选择以下相关项目:

$projects = Project::with('tags')->whereHas('tags', function($q) use ($keyword) {
    $q->where('tag_name', 'LIKE', '%'.$keyword.'%');
});

您可以循环浏览项目并正常获取标记。我认为会像这样做

foreach($projects as $project) {
    $tags = $project->tags;
}

此外,如果您只是想要加载您匹配的标记:

$projects = Project::with(['tags' => function($q) {
    $q->where('tag_name', 'LIKE', '%'.$keyword.'%');
}])->whereHas('tags', function($q) use ($keyword) {
    $q->where('tag_name', 'LIKE', '%'.$keyword.'%');
});

可能有一种更好的方法来完成最后一部分,但我从来没有遇到像以前那样的逻辑需求。

答案 1 :(得分:0)

我已经修好了。我不再使用get_results()方法了。因为现在我只在控制器中使用post_search()方法:

public function post_search()
{

    $keyword=Input::get('keyword');

    if(empty($keyword))
        return Redirect::route('user.projects.index')->with('message', 'No keyword entered, please try again');

    $projects=Project::search3($keyword);

    foreach( $projects as &$project){

        //we initialize projecttask
        $project->projecttask = Projecttask::where('id_project', '=', $project->id)->get();

    };

    //die(print_r($projects));

    return View::make('user.projects.index', compact('projects'));

}

现在我使用一个名为search3()的方法,该方法在我的模型中定义,名为Project.php(我仍然没有针对SQL注入转义此查询):

public static function search3($keyword){

    $result = DB::Select(DB::raw('SELECT projects.*, tagx.tags_all 
        FROM projects LEFT JOIN (SELECT GROUP_CONCAT(tags.tag_name SEPARATOR ",") 
            AS tags_all, tagprojects.id_project FROM tags JOIN tagprojects 
            on tagprojects.id_tag = tags.id GROUP BY tagprojects.id_project) as tagx 
            ON tagx.id_project = projects.id WHERE projects.project_name 
            LIKE "%'.$keyword.'%" OR tags_all LIKE "%'.$keyword.'%"'));

    return $result;


}