Hi I'm trying to display the project names through my pivot table called 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:
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:
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:
@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. And as you can see in my code I already have tried {{$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. But then I had tried {{die(print_r($projects))}} and the output was:
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. 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. Because now I only use the post_search() method in my controller:
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):
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.