I have a db table called CardNumbers, the data has been encrypted using Crypt::encrypt().
I have a search box that i want to be able to search CardNumbers, but the data is encrypted. How do i decrypt it in my query in mysql.
This is what i am currently using.
$from = INPUT::get('from');
$to = INPUT::get('to');
$email = INPUT::get('searchall');
$appid = INPUT::get('searchall');
$cardS = INPUT::get('searchall');
$data = DB::table('customer')
->when($from && $to, function($query) use ($from, $to) {
return $query->whereBetween('AppDate', array($from, $to));
})
->when($email, function($query) use ($email){
return $query->orWhere('Email', $email);
})
->when($appid, function($query) use ($appid){
return $query->orWhere('AppID', $appid);
})
->when($cardS, function($query) use ($cardS){
return $query->orWhere('CardNumbers', $cardS);
})
->get();
return view('customer', compact('data'));
I want to be able to use $cardS
I also tried this to get values of the card details.
$getCard = DB::table('customer')->get();
foreach($getCard as $key){
$cardG = Crypt::decrypt($key->CardNumber);
if (strpos($cardG,$cardS) !== false) {
$cardS = Crypt::encrypt($cardG) ;
}else{$cardS = 0;}
}
Related
I have this controller with many functions and cache keys depending on the username....
I need when updating the information and delete all cache keys for a specific user? is there any way to do that?
my controller to get data from caches:
class HomeController extends Controller
{
private $cache_duration = 60*60*72;
function home($uname) {
$info = Cache::remember('get-all-info-home'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->about : null;
});
return view('home', compact('info'));
}
function info($uname) {
$info = Cache::remember('get-all-info'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->about : null;
});
return view('info', compact(['info']));
}
function skills($uname) {
$info = Cache::remember('get-all-user-skills'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->about : null;
});
$skills = Cache::remember('get-all-skills'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->skills : null;
});
return view('skills', compact(['skills', 'info']));
}
I have tried this way, but still not working:
$uname = $info->user->name;
$keys = ['get-all-info-home', 'get-all-info', 'get-all-user-skills',
'get-all-skills', 'get-all-user-users-education-experiences', 'get-all-education',
'get-all-experiences', 'get-all-user-users-user-achievements', 'get-all-user-users-achievements',
'get-all-user-users-user-services', 'get-all-user-users-services-unique',
'get-all-user-users-user-services'];
foreach($keys as $key) {
$key = $key.$uname;
Cache::forget($key);
}
Similar to what lagbox mentioned, change to memcache or redis driver that supports tags, and add
Cache::
to
Cache::tags('user.....
in your controller method:
function home($uname) {
$info = Cache::tags('user'.$uname)
->remember('get-all-info-home'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->about : null;
});
return view('home', compact('info'));
}
function info($uname) {
$info = Cache::tags('user'.$uname)
->remember('get-all-info'.$uname, $this->cache_duration, function() use($uname){
$user = User::where('name', '=', $uname)->firstOrFail();
return $user !=null ? $user->about : null;
});
return view('info', compact(['info']));
}
then for the cache tag clear you would do:
Cache::tags('user'.$info->user->name)->flush()
if adding tagging is not not feasible for any reason, try checking and making sure that the $key.uname is actually returning the data you are expecting
foreach($keys as $key) {
$key = $key.$uname; // <- maybe this is not returning the right data
dd($key); // echo this out just to see if it is
Cache::forget($key);
}
I am having issue while keeping the same slug if we don't change title while updating, slug take the value of title. I have made a function to create slug. But when i update the same function automatically changes slug because it already exists in DB.
public function createSlug($title, $id = 0)
{
$slug = str_slug($title);
$allSlugs = $this->getRelatedSlugs($slug, $id);
if (! $allSlugs->contains('slug', $slug)){
return $slug;
}
$i = 1;
$is_contain = true;
do {
$newSlug = $slug . '-' . $i;
if (!$allSlugs->contains('slug', $newSlug)) {
$is_contain = false;
return $newSlug;
}
$i++;
} while ($is_contain);
}
// slug function
protected function getRelatedSlugs($slug, $id = 0)
{
return Post::select('slug')
->where('slug', 'like', $slug.'%')
->where('id', '<>', $id)
->get();
}
First of all, you don't need to create a function for that. Just validation will be enough.
use Illuminate\Support\Str;
$validator = $request->validate([
'slug' => ['required''unique:post'],
]);
if ($validator->fails()) {
$generate_extension = Str::random(3);;
}
$newSlug = str_slug($request->title).'-'.$generate_extension;
Then assign the slug.
$post->slug = $newSlug;
In order to keep the same slug you can check if title is changed;
if($post->slug != str_slug($request->title)){
$post->slug = str_slug($request->title);
}
or
if($post->title != $request->title){
$post->slug = str_slug($request->title);
}
I run the below query, if cause true then where cause is apply like below, but when i run below query it gives me "Out of memory (allocated 503316480) (tried to allocate 492834816 bytes)" error, So another easy way to check the where cause above if condition.
Here below is my code.
$user_id = "";
$doctor_id = "";
$start_date = "";
$end_date = "";
if(isset($request->user_id)) {
$user_id = $request->user_id;
}
if(isset($request->doctor_id)) {
$doctor_id = $request->doctor_id;
}
if(isset($request->start_date)) {
$start_date = $request->start_date;
}
if(isset($request->end_date)) {
$end_date = $request->end_date;
}
$query = DB::table('request_approves')
->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
->select('doctors.*','city_masters.*','request_approves.*');
if($user_id !== "") {
$query->where('request_approves.user_id', 2);
}
if($doctor_id !== "") {
$query->where('request_approves.doctor_id',3);
}
if($start_date !== "" & $end_date !==""){
$query->whereBetween('request_approves.approve_time', array($start_date,$end_date));
}
$query->get();
Not sure what it is happening, but your code seems wrong to me .
On the if statements you do something like this $query->where('request_approves.user_id', 2); but where this returned values go ? To continue the same query you should do it like this:
$query = DB::table('request_approves')
->join('city_masters', 'request_approves.city_id', '=', 'city_masters.id')
->join('doctors', 'request_approves.doctor_id', '=', 'doctors.id')
->select('doctors.*','city_masters.*','request_approves.*');
if($user_id !== "") {
$query = $query->where('request_approves.user_id', 2);
}
if($doctor_id !== "") {
$query = $query->where('request_approves.doctor_id',3);
}
if($start_date !== "" & $end_date !==""){
$query = $query->whereBetween('request_approves.approve_time', array($start_date,$end_date));
}
$query = $query->get();
PHP uses memory to perform queries as well. So performing queries from a PHP application will indeed use more memory than performing them straight from MySQL.
I did a simple calculation and 503316480 bytes comes out to 503MB. So I would suggest modifying your php.ini settings. This should let you output data, and from there you can tune your SQL.
In your php.ini
memory_limit=503M
I have the following code that displays different data depending on if you are the authenticated user looking at your own data or if you are looking at data from another user. The code works but in my opinion is very nasty.
public function showUserDetailed(Request $request, $username)
{
$key = strtolower($username).'-data-detailed';
if(Auth::user()->usenrame === $username) {
$key = $key .'-own';
}
if(Cache::has($key)) {
if($request->wantsJson()) {
return request()->json(Cache::get($key));
} else {
return view('user/detailed', ['user' => Cache::get($key)]);
}
} else {
if(Auth::user()->username === $username) {
$u = User::where('username', $username)->first();
$user = new \stdClass();
$user->username = $u->username;
$user->email = $u->email;
$user->address = $u->address;
$user->city = $u->city;
$user->state = $u->state;
$user->zip = $u->zip;
$user->phone = $u->phone;
$user->follows = $u->follows;
$user->ratings = $u->ratings;
$user->location = $u->location;
} else {
$u = User::where('username', $username)->first(['username', 'city', 'state']);
$user = new \stdClass();
$user->username = $u->username;
$user->city = $u->city;
$user->state = $u->state;
$user->follows = $u->follows;
$user->ratings = $u->ratings;
}
Cache::put($key, $user);
if($request->wantsJson()) {
return response()->json($user);
} else {
return view('user/detailed', ['user' => $user]);
}
}
}
I have tried to use something similar to the following in the model calls.
User::where('username', $username)->with('follows', 'ratings', 'location')->find(['username', 'city', 'state');
However when I specify the columns to get from the user table it negates the relation data so it comes back as empty arrays. Is there a different way I can do this which would be cleaner? I despise having to create a stdClass to be a data container in this situation and feel I may be missing something.
// this works
User::where('username', $username)->with('follows', 'ratings', 'location')->first();
// this also works
User::where('username', $username)->first(['username', 'city', 'state']);
// this does not
User::where('username', $username)->with('follows', 'ratings', 'location')->first(['username', 'city', 'state']);
When you specify columns and want to get the related models as well, you need to include id (or whatever foreign key you're using) in the selected columns.
The relationships execute another query like this:
SELECT 'stuff' FROM 'table' WHERE 'foreign_key' = ($parentsId)
So it needs to know the id of the parent (original model). Same logic applies to all relationships in a bit different forms.
I am calling URL with search params which are dynamic. How could I form proper Eloquent query?
In theory:
query
query where(someParam1)
query where(someParam2)
query orderby(someParam3)
query get
I need this kind of structure so I can use where clause if param exists.
If there is some other way in Laravel, please let me know.
It's easy with Laravel. Just do something like this:
$query = User::query();
if ($this == $that) {
$query = $query->where('this', 'that');
}
if ($this == $another_thing) {
$query = $query->where('this', 'another_thing');
}
if ($this == $yet_another_thing) {
$query = $query->orderBy('this');
}
$results = $query->get();
You can just use the where statement.
For ex: on users table or User model, you want dynamic search on name, id. You can do this
$where = [];
$firstName = $request->get('first_name');
if ($firstName) $where[] = ['first_name', 'like'. '%' . $firstName . '%'];
$id = $request->get('id');
if ($id) $where[] = ['id', $id];
$users = User::where($where)->get();
By default, it will return all the users, if anything exists in $where array, it will apply the where condition on that.
You can use like this
$validateUserDetail = User::query();
if (!empty($userDetails['email'])) {
$validateUserDetail->whereemail($userDetails['email']);
}if (!empty($userDetails['cellphone'])) {
$validateUserDetail->wherecellphone($userDetails['cellphone']);
}
$validateUserDetail->select('username');
$validateUserDetail->get()
You can pass dynamic value by below example
$user_auctions = $this->with('userAuctions')
->where('users.id', '=', $id)
->get();
I came here from Google. If you are going to be iterating over more then 5 if statements, its more effective to use a switch statement
if(empty($request->except('_token')))
return 'false';
$models = Vehicle::query();
$request_query = $request->all();
$year_switch = false;
foreach ($request_query as $key => $field_value){
if($field_value != 'any'){
switch($field_value){
case 'X':
case 'Y':
$year_switch = true;
break;
case'Z':
//Dynamic
$models->where($key,'LIKE', $field_value);
break;
}
}
}
You can pass a callback to the where function.
So, you can do something like this:
class TestService {
TestRepository $testeRepository;
public function __construct(TesteRepository $teste) {
$this->testeRepository = $teste;
}
public function getAll(array $filters)
{
$where = function (Builder $query) use ($filters) {
collect($filters)
->each(function ($value, $param) use ($query) {
if ($param === 'test') {
$query->where($param, '=', $value);
} else if ($param === 'test2') {
$query->orWhere($param, '>', $value);
}
});
};
return $this->testRepository->gelAll($where);
}
class TestRepository
{
public function getAll(\Closure $where)
{
$query = TestModel::query();
$query->where($where);
//and put more stuff here, like:
//$query->limit(15)->offset(30)
...
return $query->get();
}
}
And in your controller you pass the filters:
class TestControler ...
{
public function $index()
{
$filters = request()->query();
return $this->testService->getAll($filters);
}
}