John Davidson

php - Custom search that look for part of a string inside values of a model and its relationship (one to many)


I have a search input text that uses ajax to search for some articles (article is a model). Also, I have made a relationship one-to-many with another model "ArticleFile". Essentially, I need to look if there's a value that contains a string in the columns "title" or "description" of the article table OR (that's the hardest part) inside the column filename from the article file table with a column article_id. Since many articles contains a list of files attached to it, is there a way to search if the article's title, description or the filenames attached to it have a certain string that resembles my code? Or is there another way to achieve this?

Relationship one-to-many inside my Article model:

public function files() {
return $this->hasMany(ArticleFile::class);

In my php file from the ajax call, I have the following method to retrieve the right articles (which doesn't work):

public function ajax_article_search()
$value = $_POST['value'];
$html = '';

if($value) {

// search if the title
// or description
// or filenames (of the relationship one-to-many)
// have a certain $value
$articles = Article::where('title', 'like', '%'.$value.'%')
->orWhere('description', 'like', '%'.$value.'%')
->files->where('filename', 'like', '%'.$value.'%')

// -- Code above not working
// Error because of ->files...

foreach($articles as $article) {
$html .= '<a href="'.$article->url.'">';
$html .= '<p>'.$article->title.'</p>';
$html .= '</a>';

echo $html;

My Answer:
(category) where each article can belong to multiple categories. I want to create a custom search function in PHP that looks for a part of a string inside the values of the article model and its related category model.

Here is an example of how you can achieve this:

// Get the search query from the AJAX request
$searchQuery = $_GET['searchQuery'];

// Query the articles table and its related categories table
$articles = Article::where('title', 'like', '%' . $searchQuery . '%')
->orWhere('content', 'like', '%' . $searchQuery . '%')
->orWhereHas('categories', function ($query) use ($searchQuery) {
$query->where('name', 'like', '%' . $searchQuery . '%');

// Loop through the results and display them
foreach ($articles as $article) {
echo $article->title . '
echo $article->content . '
foreach ($article->categories as $category) {
echo $category->name . '

In this code snippet, we first get the search query from the AJAX request. Then, we query the articles table for any articles where the title or content contains the search query. We also use the `orWhereHas` method to query the related categories table for any categories that contain the search query.

Finally, we loop through the results and display the title, content, and categories of each article that matches the search query.

This custom search function allows you to search for a part of a string inside the values of the article model and its related category model.

Rate this post

3 of 5 based on 1347 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us