I need to generate a number from database and show it on input at view.
I use function on MySQL to achieve that so I use raw expression.
$nomor = DB::table('customers')-
>selectRaw('concat(year(now()),lpad(month(now()),2,0),lpad(id, 3, 0))
as nomor')
->where('id', '=', $customers->id)->get();
When I pass the variable into view,
<input type="text" class="form-control @error('nomor') is-invalid @enderror" id="nomor" placeholder="" name="nomor" value="{{ $nomor }}">
"[{"nomor":"201909001"}]"
my expected result is:
201909001 withoutquote
When you call get()
function, the function returns an array with all results of your query.
As example, if your select returns 3 lines, the $nomor
returns a collection (like an array with steroids) with 3 positions, one per line returned by the query and, in each position, the get()
function returns an object with the properties as your select, so, in this case, the correct way to access the nomor
column is:
<input type="text" class="form-control @error('nomor') is-invalid @enderror" id="nomor" placeholder="" name="nomor" value="{{ $nomor[0]->nomor }}">
Note, instead calling only $nomor
object, we access the first line of the result $nomor[0]
and get the property that corresponds to the column name of the query $nomor[0]->nomor