Laravel 4 where a similar article

public function getIndex() { // Get all the blog posts /*$posts = Post::with(array( 'author' => function($query) { $query->withTrashed(); }, ))->orderBy('created_at', 'DESC')->paginate(10);*/ $posts =Post::with(array('search' => function($query) { $query->where('title', 'like', '%Lorem ipsum%')->withTrashed(); }))->orderBy('created_at', 'DESC')->paginate(10); // Show the page return View::make('frontend/blog/index', compact('posts')); } 

This is my code in the controller. I am using the starter kit available on GitHub.

I created this model for this controller

 public function search() { return $this->belongsTo('Post', 'user_id'); } 

The problem is not that the results in which the title contains "Lorem ipsum" are not accepted. It just prints all the values ​​from the table.

How can I implement this to get only those values ​​that contain my tag / keyword. I am doing this to add a search parameter to the laravel site

+4
source share
3 answers

Why not create an area for this? Have you read scopes docs ? Here is an example of how I would achieve this:

Scope:

 public function scopeTagged($query, $tag) { return $query->where('title', 'LIKE', '%' . $tag . '%'); } 

And a modification of your action:

 public function getIndex() { $posts = Post::tagged($lorem_ipsum)->withTrashed()->orderBy('created_at', 'DESC')->paginate(10); // Show the page return View::make('frontend/blog/index', compact('posts')); } 
+9
source

Try it...

  $posts =Post::with(array('search' => function($query) { $query->raw_where("title LIKE '%Lorem ipsum%")->withTrashed(); }))->orderBy('created_at', 'DESC')->paginate(10); 

Or something like that...

$ search is your input

  Post::raw_where("match (`title`) against (?)", array($search)) ->orderBy('created_at', 'DESC')->paginate(10); 

EDIT

How about this?

  Post::where(DB::raw('MATCH(`title`)'),'AGAINST', DB::raw('("+'.implode(' +',$search).'" IN BOOLEAN MODE)->orderBy('created_at', 'DESC')->paginate(10); 
+1
source

I think this was a problem with Laravel Eager Load Constraints implementations:

For ref: -

 https://github.com/laravel/laravel/pull/1069 https://github.com/laravel/laravel/pull/946 
0
source

Source: https://habr.com/ru/post/1489326/


All Articles