简体   繁体   English

从数据透视表通过联接laravel

[英]Data from pivot table through joins laravel

Hi I'm trying to display the project names through my pivot table called tagprojects. 嗨,我正在尝试通过名为tagprojects的数据透视表显示项目名称。 Unfortunately I can't realize it. 不幸的是我没意识到。 I will show you what I have tried. 我会告诉你我尝试过的。 First I will show you my controller called ProjectController.php: 首先,我将向您展示我的名为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);
}

The search2() method is defined in my model called Project.php: 在我的模型Project.php中定义了search2()方法:

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;

}

And here is my view called results.blade.php: 这是我的名为results.blade.php的视图:

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

Well {{$tp->id_tag}} works but I actually want to see the project names. {{$ tp-> id_tag}}很好,但是我实际上想查看项目名称。 And as you can see in my code I already have tried {{$tp->project['project_name']}}. 正如您在我的代码中看到的那样,我已经尝试过{{$ tp-> project ['project_name']}}。 Unfortunately that didn't work, because then I get the following error: 不幸的是,这没有用,因为然后我得到以下错误:

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

I also have tried {{$tp->project->project_name}} but then I also receive the same error. 我也尝试过{{$ tp-> project-> project_name}},但随后我也收到相同的错误。 But then I had tried {{die(print_r($projects))}} and the output was: 但是后来我尝试了{{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

As you can see the output shows the column project_name, but for some reason I can't display project_name column. 如您所见,输出显示列project_name,但是由于某些原因,我无法显示project_name列。 Can someone help me, please? 有人能帮助我吗? Gladly I'm waiting for your response. 很高兴我在等待您的回复。 Anyway thanks for your answer. 无论如何,谢谢您的回答。

You can select your relevant projects like this: 您可以这样选择相关项目:

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

You can loop through the projects and fetch your tags as normal. 您可以遍历项目并照常获取标签。 I assume that would be done like this 我认为那样会做

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

Additionally if you just want to eager load the tags that you are matching: 此外,如果您只想加载匹配的标签,请执行以下操作:

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

There might be a better method of doing that last part, but I have never come across the need for logic like that before. 可能有更好的方法来完成最后一部分,但是我之前从未遇到过像这样的逻辑需求。

I have fixed this. 我已经解决了。 I don't use the get_results() method anymore. 我不再使用get_results()方法了。 Because now I only use the post_search() method in my controller: 因为现在我只在控制器中使用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'));

}

And now I use a method called search3(), which is defined in my model called Project.php (I still haven't escaped this query yet against SQL injection): 现在,我使用一种称为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;


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM