0
  $collection = DB::table('collections')
  ->join('users','users.id','collections.user_id')
  ->get([value(md5(encrypt('collections.id'))), 'collections.name', 'collections.category_id', 'users.name as username', 'collections.price', 'collections.image'])->toArray();
  return response()->json([
    'collection'    => $collection,
  ], 200);

I want to encrypt value of collections.id. It doesn't encrypt value(md5(encrypt('collections.id'))) says

Unknown column 'd0bfdf6d2c0d3a3bb3b9db20b5194e67' in 'field list'

3
  • get() function to get value of column. Please use DB select function. Commented Nov 30, 2021 at 6:48
  • 1
    Keep in mind if you use md5, you are no longer encrypting the value but hashing the value making it unpossible to restore the original value Commented Nov 30, 2021 at 6:50
  • you can encrypt after query in database Commented Nov 30, 2021 at 7:10

1 Answer 1

1

You are currently using the md5 encrypt function on the string collections.id, so you are not encrypting the result of the query but the database column name, which is why you are getting that the column doesn't exist.

By the naming of the $collection variable, I would presume that you are only expecting one result, if so you can do as below otherwise you will have to loop through each row and encrypt the column value. It's not the nicest way of doing it, it would be much nicer to use casting in a model class if you use models. You can read more about casting in the Laravel documentation https://laravel.com/docs/8.x/eloquent-mutators

Also, a note, if you are only expecting one row, use first() rather than get(), and if you are expecting several rows, use the variable name $collections as it makes the code easier to read as it tells readers that the query will return several collections rather than just one

$collection = DB::table('collections')
  ->select(['collections.id', 'collections.name', 'collections.category_id', 'users.name as username', 'collections.price', 'collections.image'])
  ->join('users','users.id','collections.user_id')
  ->first()
  ->toArray();
  
  if ($collection) {
    $collection['collections.id'] = md5(encrypt('collections.id'));
  }
  
  return response()->json([
    'collection'    => $collection,
  ], 200);
Sign up to request clarification or add additional context in comments.

1 Comment

md5 is a hashing function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.