How can I search for a record using a part of a string field in Laravel/Eloquent?

laravel query builder
laravel eloquent join
laravel collection
laravel model attributes
laravel model example
laravel subquery
simple select query in laravel
eloquent update record

I'm trying to get a student record when the user enters only part of the student's name. In other words, check if one of the current students names contains the string entered by the user, and if so, return that record.

My current code, which return the record only after the user has entered the full name:

public function getStudent($name) {
    $std = Student::where('first_name', $name)->get();
    return $std;
}

Thanks

You must to use 'like' to get part of the student's name

$std = Student::where('first_name','like', '%' . $name. '%')->get();

Eloquent: Getting Started - Laravel, Now, let's look at an example Flight model, which we will use to retrieve and So​, in this case, Eloquent will assume the Flight model stores records in the flights table. you should set the protected $keyType property on your model to string : In addition to retrieving all of the records for a given table, you may also retrieve single records using find, first, or firstWhere. Instead of returning a collection of models, these methods return a single model instance:

You should use the wildcard LIKE

$std = Student::where('first_name','like', '%'.$name.'%')->get();

Eloquent ORM - Laravel, The Eloquent ORM included with Laravel provides a beautiful, simple If you need to process a lot (thousands) of Eloquent records, using the chunk In the example above, Eloquent will look for a user_id column on the phones table. The key fields to notice here are the imageable_id and imageable_type on the photos  If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly: $email = DB::table ('users')->where ('name', 'John')->value ('email'); To retrieve a single row by its id column value, use the find method:

Use Like operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

$std = Student::where('first_name', 'like', '%' . $name . '%')->get();

You can visit here for more information

Database: Query Builder - Laravel, To retrieve a single row by its id column value, use the find method: If you need to work with thousands of database records, consider using the chunk method. Just like that, we now have records in our database. We can finally get to the fun part and show off the true power of Eloquent! Finally! Next, we will go through all the types of queries you can create with Eloquent and we will see how easy it is to query databases with one-to-one and many-to-many relationships. Using Eloquent Querying our

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

% - The percent sign represents zero, one, or multiple characters

_ - The underscore represents a single character

Finds any values that have "or" in any position

$students = Student::where('first_name', 'LIKE', '%' . $name. '%')->get();

You can do it as well - whereLike

$students = Student::whereLike('first_name',$name)->get(); 

Query Builder - Laravel, There is no need to clean strings being passed as bindings. Selects. Retrieving All Rows From A Table Using Where Null To Find Records With Unset Values. Imagine you need to provide a search for users. Using Eloquent you can perform a search like this: User::query () ->where ('name', 'LIKE', "% {$searchTerm}%") ->orWhere ('email', 'LIKE', "% {$searchTerm}%") ->get (); This will return all records that have a name or email that contains the string in $searchTerm.

Use Like Operator for SQL to Check string in record. refer:https://www.w3schools.com/sql/sql_like.asp

Or use Laravel DB or Eloquent as,

public function getStudent($name) {
    $std = Student::where('first_name','LIKE' ,'%'.$name.'%')->get();//Laravel Eloquent
    $std = DB::table('student')->where('first_name','LIKE' ,'%'.$name.'%')->get();//DB table method
    $std = DB::select("select * from student where first_name LIKE %".$name."%");//DB SELECT method
    return $std;
}

Searching models using a where like query in Laravel, Using Eloquent you can perform a search like this: This will return all records that have a name or email that contains the string in $searchTerm . Search terms are AND'ed per field and OR'ed between fields. so it will not  By default, Laravel will use the fully qualified class name to store the type of the related model. For instance, given the one-to-many example above where a Comment may belong to a Post or a Video, the default commentable_type would be either App\Post or App\Video, respectively. However, you may wish to decouple your database from your

Advanced Laravel Eloquent usage, You can find Pusher's Laravel tutorials here. What is Eloquent? The Eloquent ORM included with Laravel provides a beautiful, simple Updating an Eloquent model instance updates the database record it is mapped to, and You may create a string field and prepend your company initials to all generated  PHP web-developer with 15 years experience, 5 years with Laravel. Now leading a small team of developers, growing Laravel adminpanel generator QuickAdminPanel and publishing Laravel courses on Teachable.

Dynamic relationships in Laravel using subqueries, Dynamic relationships in Laravel using subqueries class User extends Model { public function logins() { return $this->hasMany(Login::class); } This time let's eager-load all the login records: Let's look at the database query that's actually being run: Okay, now for the part we've been building up to. The field property SearchDefault sets a field to its default value (if there is one) immediately after SearchInit PeopleCode finishes. SearchDefault overrides SearchClear. If you call SearchClear for a record, then use SearchDefault for a field, the field is set to its default value and the search key values for the rest of the record are cleared.

20 Laravel Eloquent Tips and Tricks, Eloquent ORM seems like a simple mechanism, but under the hood, $article = Article::find($article_id); $article->read_count++; is setting some field value at the moment of creating the model object. ($model) { $model->uuid = (string)​Uuid::generate(); }); }. 4. Relationship with conditions and ordering. Laravel - Update Records - We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table.

Comments
  • $std = Student::where('first_name', 'like', '%' . $name . '%')->get();
  • Use like: ->where('post_type', 'like', '%'.$string.'%')
  • Thanks for you response. For some reason it returns everything in the databse whenever i type anything. I tried $std = Student::where('first_name','like', '%' . $name. '%')->get(); and it worked perfectly fine
  • Glad to hear that :) @moepart