Reputation: 43
I'm using Laravel 5, MySQL.
I'm trying to get the data from my database to a table in my view.
I have done this before on the users table and listed all users from that database in a table on my html view.blade.
I can see by looking at both my user version and my new version that with in the blade tempting see below, there are spaces in the column names i.e. Cost Code need to be CostCode or Cost_Code:
<td>{{ $simstotals->Voice Numbers}}</td>
<td>{{ $simstotals->Cost Code }}</td>
<td>{{ $simstotals->Status }}</td>
To this, with out changing column names in database:
<td>{{ $simstotals->VoiceNumbers}}</td>
<td>{{ $simstotals->CostCode }}</td>
<td>{{ $simstotals->Status }}</td>
now I can't change the database column names so that nice easy solution is out the window.
So how do i get this to work, please help!!
Upvotes: 1
Views: 5912
Reputation: 21
I ran into this as well, I rewrote the SQL query that was pulling the data to the array to rename the columns (this is mssql)
$varName = DB::select(
"SELECT [index]
,[Row]
,[Supplier]
,[Plant]
,[Buyer]
,[PO Created] as POCreated
,[Ord Typ] as OrdTyp
FROM [dbo].[PullDaily]
WHERE XXXX = XXXX");
Upvotes: 0
Reputation: 1214
Laravel Collection implements ArrayAccess, so it can be always referred as an array.
E.g. you can try doing the following:
$simstotals['Voice Numbers']
;
Another "hacky" way is to implement custom __get
and __set
methods. In this case you'll be able to access these fields with something like $simstotals->Voice__Numbers
(double underscore).
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
abstract class HackedModel extends Model
{
public function __get($attr)
{
if (strpos($attr, '__') !== false) {
$attr = str_replace('__', ' ', $attr);
}
return parent::__get($attr);
}
public function __set($attr, $value)
{
if (strpos($attr, '__') !== false) {
$attr = str_replace('__', ' ', $attr);
}
parent::__set($attr, $value);
}
}
Then extend this model.
Of course, this is a dirty "hack" and I don't think it's a good idea :)
Good luck!
Upvotes: 0