Skip to content

[9.x] Added rawValue to Database Query Builder (and Eloquent as wrapper) - #44631

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
eusonlito:9.x
Oct 19, 2022
Merged

taylorotwell merged 1 commit into
laravel:9.xfrom
eusonlito:9.x

Conversation

@eusonlito

Copy link
Copy Markdown
Contributor

Currently there are a method to get a existing column value from database called value, this PR adds the method rawValue to get a value from a SQL expression.

For example, get first and last year from trip table using the Trip Model.

$first = TripModel::orderBy('date_at', 'ASC')->rawValue('YEAR(`date_at`)');
$last = TripModel::orderBy('date_at', 'DESC')->rawValue('YEAR(`date_at`)');

Or full name from the user table:

$fullname = UserModel::where('id', $id)->rawValue('CONCAT(`first_name`, " ", `last_name`)');

I think that there are a lot of usage cases for this method :)

Regards!
Lito.

@eusonlito eusonlito changed the title Added rawValue to Database Query Builder (and Eloquent as wrapper) [9.x] Added rawValue to Database Query Builder (and Eloquent as wrapper) Oct 18, 2022
@taylorotwell
taylorotwell merged commit ae4b96a into laravel:9.x Oct 19, 2022
@eusonlito

Copy link
Copy Markdown
Contributor Author

Thanks @taylorotwell 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants