Laravel custom string as field

laravel model attributes
laravel validation
laravel datetime
laravel eloquent
laravel migration
laravel datetime format
laravel collection
laravel-custom casts

How would I convert the following statement into laravel's eloquent or Query builder:

Select *, 'admin' as type from users.

its the 'admin' as type is where im having problem in converting it on laravel. Thanks

I would rather figure out why MSSQL is handling it that way, or the adapter but since that raw SELECT directly on the connection works, you can do that with Eloquent as well:

App\User::fromQuery("select *, 'admin' as type from users");

You will have a Collection of models hydrated from that result.

php - Laravel custom string as field, I would rather figure out why MSSQL is handling it that way, or the adapter but since that raw SELECT directly on the connection works, you can do that with  Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0. This will stop it from thinking it is an auto increment field. Laravel Docs

Using query builder your can try it like this

$users = DB::table('users')->selectRaw("*, 'admin' AS type")->get();

And if you have User model then

$users = User::selectRaw("*, 'admin' AS type")->get();

Eloquent: Mutators - Laravel, Custom Casts; Array & JSON Casting; Date Casting; Query Time Casting Eloquent can also automatically cast date fields to Carbon instances or even cast text fields to JSON. @return string */ public function getFullNameAttribute() { return  # Defining Fields. Each Nova resource contains a fields method. This method returns an array of fields, which generally extend the Laravel\Nova\Fields\Field class. Nova ships with a variety of fields out of the box, including fields for text inputs, booleans, dates, file uploads, Markdown, and more.

As suggested by lagbox. This query returns what I need. Thank you all.

DB::select("select *, 'admin' as type from users")

Validation - Laravel, By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull In this example, we are specifying that the publish_at field may be either null or a valid Form requests are custom request classes that contain validation logic. Laravel 7 brings custom casts to the table, and flexible content fields are the perfect use case for them. The field stores its values as a single JSON string, meaning this string needs to be parsed before it can be used in your application. This can be done trivially by using the FlexibleCast class in this package:

Schema Builder - Laravel, Schema::table('users', function($table) { $table->string('email'); });. The table builder contains a variety of column types that you may use when building your  The field under validation must be greater than or equal to the given field. The two fields must be of the same type. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule.

Database: Query Builder - Laravel, There is no need to clean strings being passed as bindings. PDO does not support You may also specify a custom key column for the returned Collection: For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model. In addition to custom accessors and mutators, Eloquent can also automatically cast date fields to Carbon instances or even cast text fields to JSON.

Making Laravel Nova custom fields more developer-friendly, Making Laravel Nova custom fields more developer-friendly If your field treats a value, doesn't output anything or stores a JSON string on the model, you  Helpers. Introduction; Available Methods; Introduction. Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

Comments
  • try this $users = DB::table('users')->selectRaw("*, 'admin' as type")->get();
  • You can use this also. Users::where('type','admin')->get();
  • error on result. Invalid column name '"admin"'
  • What is the name of your column.
  • does DB::select("select *, 'admin' as type from users") return anything?
  • Thanks for your help. But both this shows this error: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name '"admin"'
  • are you using SqlServer ?
  • does the sql working directly in your mssql client ?
  • Yes. it displayed results if I run that query directly on my database.
  • ive added a picture of the native query and result for your reference
  • I posted another answer that was dependent upon the question i had asked