How to use 'LIKE' in Laravel? [duplicate] - php

This question already has answers here:
Laravel 'like' query with MongoDB connection
(1 answer)
Laravel-5 'LIKE' equivalent (Eloquent)
(7 answers)
How to query MongoDB with "like"
(45 answers)
Closed 3 years ago.
I'm trying to use 'LIKE' in Laravel. But in the output whole data is displayed. Is my method correct? If not then correct me.
$this->uses('Data');
$a ='nan';
$options = ['sort' => ['city' => 1]];
$results = $this->Part->find([],['city','LIKE',"%$a%"],$options);
return view('testEnv')->with('results',$results);
}

Query Builder
$data = DB::table('table_name')->where('col_name','LIKE','%'.$variable.'%')->get();
"%$a%" is a string ....

You can use where like
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();
Read documentation here

Related

Laravel query builder combine pluck() with keyBy() [duplicate]

This question already has answers here:
Laravel Eloquent Pluck without losing the key
(7 answers)
Closed 1 year ago.
I would like Laravel query-builder to return an array of key => value.
For example, for users table which has columns username, email return something like:
Array
(
[email1] => username1
[email2] => username2
)
Now, I can achieve each one of the with either pluck('username') or with keyBy('email').
I can use php array functions to manipulate result returned by keyBy in order to achieve what I want:
$tmpRes = DB::table('users')
->select(['username', 'email'])
->get()->keyBy('email')->toArray();
$res = array_map(function ($el) {
return $el->username;
}, $tmpRes);
I wonder whether there is a direct way to do so.
Pluck actually allows a second parameter as a key
public static function pluck($column, $key = null)
So all you have to do is pass that in
$tmpRes = DB::table('users')
->pluck('username', 'email')
->toArray();

how to join three table in laravel [duplicate]

This question already has answers here:
Laravel join with 3 Tables
(5 answers)
Closed 1 year ago.
i have problem when joining my tables the error says Undefined variable: spps
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
return view('admin.data_siswa',compact('siswas','kelass'));
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps'));
}
i dont know how to defining the spp table, any one please suggest me idea
the problem is not in the joining , the problem is with two return statement!
the exception won't reach the last return, you should simply united them.
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps','kelass'));
pass only one parameter in DB::table($tableName);
$spps = DB::table('spp')->get();

Select specific column only from Laravel 5.2 Relation query not working [duplicate]

This question already has answers here:
Get Specific Columns Using “With()” Function in Laravel Eloquent
(19 answers)
Closed 2 years ago.
So I have a query that I only want to get specific columns in a relation but is not working. I'm using Laravel 5.2 by the way. Here's what I have:
$job = Job::query()->whereId($job_id)
->with([
'jobType' => function (Relation $query) {
$query->select(['name']);
},
])
->first();
If I do that, the jobType relationship returns null as seen below:
And if I'll remove the $query->select(['name']);, it has the data from job_type table. How can I just successfully get specific column from a table?
Maybe This can.. To get the specific column you need a specific jobtype
$job = Job::query()->whereId($job_id)
->with(['jobType' => function ($q) use($jobType) {
$q->where(// check the condition on jobtype table);
$q->select(['name']);
},
])
->first();

"IN" and "NOT IN" in CakePHP3 [duplicate]

This question already has answers here:
Access variables from parent scope in anonymous PHP function
(2 answers)
Closed 5 years ago.
There is an example in the Cookbook:
$query = $cities->find()
->where(function ($exp, $q) {
return $exp->notIn('country_id', ['AFG', 'USA', 'EST']);
});
In SQL this should be aequivalent to:
WHERE country_id NOT IN ('AFG', 'USA', 'EST')
Now, I'm trying to use a variable here. Sadly, this won't work:
$query = $cities->find()
->where(function ($exp, $q, $variable) {
return $exp->notIn('country_id', $variable);
});
Any ideas?
I always find the easiest way to use IN and NOT IN is as follows in CakePHP
$query = $cities->find()
->where(['country_id IN' => $variable])
More information on auto generating in clauses is at the following link in the book. You can also use it to cast values to the type of the column automatically. To me it is more readable as well.
https://book.cakephp.org/3.0/en/orm/query-builder.html#automatically-creating-in-clauses
It works with "use", see: PHP: Anonymous Functions
$query = $cities->find()
->where(function ($exp, $q) use ($variable) {
return $exp->notIn('country_id', $variable);
});

CakePhp - how to access variables inside find->innerJoinWith? [duplicate]

This question already has answers here:
PHP variables in anonymous functions
(2 answers)
Closed 7 years ago.
I am trying to query for users that are assigned to a certain project in CakePHP. How could I basically achieve this:
$projectId = //Project ID query result.
$users = $this->Tickets->Users
->find('list', ['limit' => 200])
->innerJoinWith(
'ProjectsUsers', function($q){
return $q->where(['ProjectsUsers.project_id' => $projectId]);
}
);
This code works when not using variables (eg. replacing $projectId with 8) but when I try to use variables I get: Undefined variable: projectId
How can I pass variables into innerJoinWith?
If you mean how to inherit a variable from the parent scope, you'd do it like this.
$users = $this->Tickets->Users
->find('list', ['limit' => 200])
->innerJoinWith(
'ProjectsUsers', function($q) use($variableToPass) {
return $q->where(['ProjectsUsers.project_id' => $variableToPass]);
}
);

Categories