I am getting an md5 string value from the URL and want to check is record present in the database table with that md5 value, but I am not able to use md5('column_name') in where condition with Eloquent model.
My code should be something like this.. Suppose I have test_records named in my table and I am getting md5 string value of id from URL:
// sample for model `class TestRecord extends Eloquent { // code of model
}`
// in test function of repository I have written below statements.
$record = TestRecord::where('md5("id")','=', $md5_valued_string_get_from_url);
I am getting error: as unknown column name md5("id")
from table test_records.
This will be resolved by using DB::raw() function where I need writed entire query as we written in core.
I want to use this md5 function of mysql with Eloquent model is this possible? Thanks for help in advanced.
Honestly I have no idea at all what you are trying to achieve. First of all you are enclosing md5("id")
in single quote so it is just considered as string so it will try to find $md5_valued_string_get_from_url
in the md5("id")
column. I don't really get what you are trying to achieve here so I can't give you a real answer unless you give me some more informations :)
I don't understand either... Why would you MD5 the column name?
I suppose that what you want to do, is check if the MD5 hash in URL matches some ID (which is non hashed) in your database. This can not (i suppose) be done easily without a loop (because you can not unhash MD5) of all your records.
I suggest you to save another column named "hash" (MD5 or another) and query records by this from URL (not by hashed ID)
Hey @zaalbarxx and @TorchSK I am applogies for my question. Now I will try to give you brief what I want,
consider the below query as an example
SELECT *
FROM table_name
WHERE md5( category_id
) = 'c4ca4238a0b923820dcc509a6f75849b'
I want to create this query with Eloquent model.
I hope this time I am pretty much clear with my question :)
Still don't get the idea, but if you want to build query like this one use:
$record = TestRecord::where(DB::raw('md5("id")') , $md5_valued_string_get_from_url);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community