on my models to load data is
public function getgl()
{
$this->db->select('trID id, purpose title, date start');
$this->db->from('trroom');
return $this->db->get()->result();
}
and my controller is
$data=$this->Reservationmodel->getgl();
echo json_encode($data);
with this code is no problem for me but the problem come when i want to add not only the date but i want to input a date time but from my database date and time is a diffrent column, can any one help me for this problem?
Just concatenate the two fields date and time. If you have MariaDB/MySQL it would be something like this in your model:
public function getgl()
{
$this->db->select('trID id, purpose title, date start, CONCAT(date," ",timecolumn) AS datetimecolumn');
$this->db->from('trroom');
return $this->db->get()->result();
}
You can use the MySQL function DATE_FORMAT() if you want more diplay options: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
Related
This issue is happening across the entire application...
I have datatables & charts:
Datatables don't sort by dateTime correctly, aka Started Column.
Charts always starts with the latest date...not by order:
I am using Carbon for datatables like this:
->editColumn('startDateTime', function ($report) {
return Carbon::parse($report->startDateTime)->format('d M, Y');
})
For the charts, returning data as json then format the date:
$data = TrafficViolation::select('id', 'violationDateTime')
->orderBy('violationDateTime')
->get()
->groupBy(function($data) {
return Carbon::parse($data['violationDateTime'])->format('M');
});
The column type of these dates are DateTime in the database.
What's frustrating is that there is a datatable called Audit Log that came with the theme (Metronic 8) and it's sorting the date correctly here (created at):
And looking to its controller:
->editColumn('created_at', function (Activity $model) {
return $model->created_at->format('d M, Y H:i:s');
})
Looking at the Model there isn't anything related to Carbon or date functions there, noting that the data type of created_at is timestamp.
I tried:
Changing data type to timestamp instead of datetime.
Copying the same code of audit log, no need for Carbon, I get an error format() unknown.
To me it looks like you are overcomplicating things, why don't you just sortByDesc if you need the newest results first I do not understand. You can do this like so:
First in you TrafficValidation model add a casts to datetime
protected $casts = [
'violationDateTime' => 'datetime',
];
Then where you return the query just do
$data = TrafficViolation::select('id', 'violationDateTime')
->orderByDesc('violationDateTime')
->get()
->groupBy(function($data) {
return Carbon::parse($data->violationDateTime)->format('M');
});
And the reason audit log works for sorting is because it by default doesnt look at all at your query sorting, it takes the data and sorts it by itself
Using laravel 7 and pivot tables i have something like this:
Model: show
public function genres()
{
return $this->belongsToMany('App\ShowGenre');
}
Model: ShowGenre
public function shows()
{
return $this->belongsToMany('App\Show', 'show_show_genre')->orderBy('name', 'asc');
}
In mysql database i have show_genres where i put my show_id and genre_id. So i want to create something like suggestion based.
At bottom my page i want to display 5 random tv shows that have same genre. Some tv shows have multiple genres so how can i display that and limit it with suma 5.
show_show_genre should have show_id and show_genre_id because show_show_genre is pivot table. show_genres is not pivot table since it is genre's table as described in code. Also, you don't need to set show_show_genre in return statement since Laravel will automatically recognize it's pivot table.
Try something like this:
$show = Show::findOrFail($id);
$featuredShows = Show::whereHas('genres', function ($query) use ($show) {
$query->whereIn('show_genres.id', $show->genres()->pluck('id')->toArray());
})->where('id', '!=' , $show->id)->with(['genres'])->inRandomOrder()->limit(5)->get();
I am using the following method to get the column names of the database tables of my Laravel (v5.6.24) project. I am using mysql and it was working fine as expected. But from this week the columns names are showing as ordered by name. Previously it was showing names as in order as the actual table.
How can i get the column names in same order as the table?
/*
* Get Table Column Names
*/
public function getTableColumns()
{
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}
Might help someone: this is a slightly modified version of #Jonas Staudenmeir answer - but tweaked to be reusable. Just feed it the table name and it will spit out an array of the table fields.
private function getTableColumns($table_name)
{
return DB::select(
(new \Illuminate\Database\Schema\Grammars\MySqlGrammar)->compileColumnListing()
.' order by ordinal_position',
[env('DB_DATABASE'), $table_name]
);
}
You'll have to order the columns by ordinal_position:
public function getTableColumns()
{
return $this->getConnection()->select(
(new \Illuminate\Database\Schema\Grammars\MySqlGrammar)->compileColumnListing()
.' order by ordinal_position',
[$this->getConnection()->getDatabaseName(), $this->getTable()]
);
}
I have an order table with a primary key id and another table trips. I trip can have many orders. I have defined the relationship in this way
public function orders_currency() {
return $this->hasMany('App\Order', 'trip_id', 'id')->select('id', 'converted_currency_code');
}
Although I know that, I have to select id of the order table in order to get the result, that's why I am mentioning to select the id of the order table.
Despite this, I am getting the orders_currency index empty. Which angle am I missing in this case?
Any clue will be appreciated.
Did you set up the model correctly?
You'd do something like this in your model script I suppose
class Trips extends Model
{
public function orders() {
return $this->hasMany('Order');
}
I got the solution by doing this way
public function orders_currency() {
return $this->hasOne('App\Order', 'trip_id', 'id')
->select('trip_id','converted_currency_code');
}
I had to select referencing key in this case.
I'm sure this is very simple - I just need two columns from a country database to populate a <select>
Looking at this URL I came up with the code below, but this seems to return an object for each record
http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_all
class countries extends Eloquent
{
}
public static function getCountrySelect()
{
return countries::all(array("name","iso_3166_2"));
}
If you want to populate custom field from database use this:->
countries::lists('name','iso_3166_2');
i.e
countries::lists('coulmnname1','coulmnname2');
and if you want to get suppose first 10 entries for that model use this:
countries::take(10)->get();