I have a index view in which I have a field name sub-division. The field displays the id. But I want to display it's name instead of ID.
What I have done?
I have placed a function in my Model
public function getSubdivision()
{
return $this->hasOne(SurveyHescoSubdivision::className(), ['id' => 'sub_division']);
}
In my search model I have done filtering like
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'created_by' => $this->created_by,
'updated_by' => $this->updated_by,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'store_id' => $this->store_id,
'sub_division'=> $this->sub_division,
'status'=>'Inventory Stored',
]);
And in my index view
[
'label' => 'Sub-Division',
'value' => function ($d) {
return $d->subdivision->name;
},
// 'filter' => Html::activeDropDownList($searchModel, 'sub_division', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
The table has the name column against the id.
When I goto my index page I am getting the below exception
Trying to get property of non-object
I have done this thing in other views but I don't know why this is showing
Any help would be highly appreciated.
Your subdivision id is empty, Use :
[
'label' => 'Sub-Division',
'value' => 'subdivision.name',
],
OR
[
'label' => 'Sub-Division',
'value' => function ($d) {
return !empty($d->sub_division) ? $d->subdivision->name : null;
},
],
Related
i'm got stuck for 2 days, i got confused how to render column usind yajra/laravel-datatable
i'm using postgre as database, and yajra/laravel-datatables as package.
i have query builder like this
$data = DB::select("SELECT * FROM get_list_count_amount_transaction('chat', 'done', '2019-03-01', '2021-12-31' )")[0];
which generates a value of object : (i use var_dump() to see the value)
{ ["list_data"]=> string(2171)
"[{"id":"44649ccd-9195-4964-b48c-ed2098077dc5","kd_join":1,"status":"done","booking_date":"2021-04-18","transaction_type":"chat","price":4000.00,"date_create":"2021-04-18T19:56:57"},
{"id":"e500d2c1-99ae-4436-8ecc-8073f4f05bba","kd_join":1,"status":"done","booking_date":"2021-03-20","transaction_type":"chat","price":10000.00,"date_create":"2021-03-19T21:41:41"}
]"
["count_transaction"]=> int(12)
["total_amount_transaction"]=> string(9) "160500.00"
}
i'm confused, how to render list_data into a table using datatble
this is my html builder function :
public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '200px'])
->addColumnBefore([
'defaultContent' => '',
'data' => 'DT_Row_Index',
'name' => 'DT_Row_Index',
'title' => 'No',
'render' => function () {
return 'function(data,type,fullData,meta){return meta.settings._iDisplayStart+meta.row+1;}';
},
'orderable' => false,
'searchable' => false,
'exportable' => false,
'printable' => true,
'footer' => '',
]);
}
protected function getColumns()
{
return [
['data' => 'transaction_type', 'name' => 'transaction_type', 'title' => 'Tipe Transaksi', 'orderable' => false],
['data' => 'status', 'name' => 'status', 'title' => 'Status', 'orderable' => false],
['data' => 'booking_date', 'name' => 'booking_date', 'title' => 'Tanggal Booking', 'orderable' => false],
['data' => 'price', 'name' => 'price', 'title' => 'Jumlah', 'orderable' => false],
];
}
}
Just grab the list_data, use json_decode and laravel collection to make your needed structure. Try to use map, filter, mapWithKeys or whatever suitable.
For Instance:
collect(json_decode($listData))->mapWithKeys(function($item)){
//
}
I have install the miloschuman\highcharts\Highcharts, website : https://www.yiiframework.com/extension/yii2-highcharts-widget. I have a database with the table columns lme_price and lme_name which I want to display the price of the copper in the highcharts. I'm using PHP.
Below is my code which I have done. This is the code in my models. I create a static function with the query inside it to find the data that I want from the database.
public static function getCopperPrice()
{
$getCopperPrices = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->all();
return $getCopperPrices;
}
Here is my code in the view. I have a table show in the view which show every data from the database.
<div class="lme_index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'searchModel'=> $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=> 'DATE',
'attribute' => 'lme_title',
],
[
'label'=> 'MATERIAL',
'attribute'=> 'lme_name',
],
[
'label'=> 'PRICE (US$)',
'attribute'=> 'lme_price',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Here is the code for the highcharts graph.
<div class = "graph">
<?= Highcharts::widget([
'options' => [
'title' => ['text' => 'Copper Price'],
'xAxis' => [
'categories' => []
],
'yAxis' => [
'title' => ['text' => 'Price $US']
],
'plotOption' => [
'series' => [
['name' => 'Copper',
'data' => [lme::getCopperPrices()]],
]
]
]
]);?>
I want to display only 1 material price in the highcharts. I call the function from the model inside the series but the graph shows nothing on it. The coding there didn't show any error to me.
Can someone help me or tell me how to solve this? Thanks.
Chart require integer values to display
public static function getCopperPrice()
{
$cooperPrices = [];
$copperPriceList = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->column();
foreach($copperPriceList as $price) {
$cooperPrices[] = (int) price;
}
return $cooperPrices;
}
I am trying to create a table using database migrations, here is my code
public function change()
{
$table = $this->table('tags');
$table->addColumn('title', 'string', ['default' => null,'limit' => 255,'null' => false,]);
$table->addColumn('created', 'datetime', ['default' => null,'null' => false,]);
$table->addColumn('modified', 'datetime', ['default' => null,'null' => false,]);
$table->addUniqueKey('title'); // Giving error **Fatal error: Call to undefined method Phinx\Db\Table::addUniqueKey()**
$table->create();
}
I want to set title column unique, but when I try to do it then it gives the error:
Fatal error: Call to undefined method Phinx\Db\Table::addUniqueKey()
Add primary key refer http://book.cakephp.org/3.0/en/orm/schema-system.html
public function change() {
$t = new Table('posts');
$t->addColumn('id', 'integer')
->addColumn('author_id', 'integer')
->addColumn('title', 'string')
->addColumn('slug', 'string');
// Add a primary key.
$t->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
// Add a unique key
$t->addConstraint('slug_idx', [
'columns' => ['slug'],
'type' => 'unique',
]);
// Add index
$t->addIndex('slug_title', [
'columns' => ['slug', 'title'],
'type' => 'index'
]);
// Add a foreign key
$t->addConstraint('author_id_idx', [
'columns' => ['author_id'],
'type' => 'foreign',
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade'
]);
}
This should be the solution
$table->addColumn('title', 'string',
[
'default' => null,
'limit' => 255,
'null' => false,
]);
$table->addIndex(['title'], ['unique' => true]); // < this is what you looking for
I am surprised CakePHP documentation doesn't mention it properly. After long searching, I found the solution in this article
I am stuck with a problem that don't know how to get the value from url. I am new in backpack. please help me with this.....
I want my details on a view based on the id that is passed from another view, how to implement this?? this is my code
Adcontroller.php
public $crud = array(
"model" => "App\Larapen\Models\Ad",
"entity_name" => "ad",
"entity_name_plural" => "ads",
"route" => "admin/ad",
"reorder" => false,
"add_permission" => false,
"columns" => [
[
'name' => 'reviewed',
'label' => "Reviewed",
'type' => "model_function",
'function_name' => 'getReviewedHtml',
],
],
"fields" =>[
[ // Hidden
'name' => 'id',
'label' => "id",
'type' => 'hidden'
],
],
);
Ad.php(model)
public function getReviewedHtml()
{
if ($this->reviewed == 1) {
return '<i class="fa fa-check-square-o" aria-hidden="true"></i><span> Reviews</span>';
} else {
return '<i class="fa fa-square-o" aria-hidden="true"></i>';
}
}
route.php
CRUD::resource('ad', 'AdController');
CRUD::resource('reviews', 'AdReviewController');
AdReviewController.php
public $crud = array(
"model" => "App\Larapen\Models\AdReviews",
"entity_name" => "Reviews",
"entity_name_plural" => "Reviews",
"route" => "admin/reviews",
"reorder" => false,
"add_permission" => false,
// *****
// COLUMNS
// *****
"columns" => [
[
'name' => 'created_at',
'label' => "Date",
],
[
'name' => 'user',
'label' => "User",
'type' => "model_function",
'function_name' => 'getuser',
],
[
'name' => 'review',
'label' => "Review",
],
],
"fields" => [
[
'name' => 'review',
'label' => "Review",
],
],
);
public function __construct()
{
//$this->crud['update_fields'][1]['options'] = $this->adType();
// $this->crud['update_fields'][2]['options'] = $this->categories();
parent::__construct();
}
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
}
"I want to display the reviews based on each Ads now it display all the reviews from the review table, where should i write the condition???"
Waiting for a response...............
I presume you have one-to-one or one-to-many relationships between AdReview and Review, defined both in your Review model and your AdReview model.
If so, you can add:
a "select" column in your table view;
a "select" / "select2" field in your create/update views;
That will show the entity it's "connected" with and allow the user to edit it.
Is this what you were trying to achieve?
I need to set custom data-attribute to the ListView elements. As example I try to get current DataProvider model id. But I still view error "htmlspecialchars() expects parameter 1 to be string, object given". Please check my code and halp me - how I should get this ID?
<?= ListView::widget([
'dataProvider' => $photoProvider,
'id' => 'photo-list',
'itemView' => '_photoListItem',
'viewParams' => [
'fullView' => true,
],
'options' => [
'tag' => 'ul',
'class' => 'list-view'
],
'itemOptions' => [
'tag' => 'li',
'class' => 'item',
'data' =>[
'test' => function ($model, $key, $index, $widget) {
return Html::encode($model->id);
}
]
],
'pager' => [
'class' => ScrollPager::className(),
'container' => '#photo-list',
'item' => '.item',
'triggerText' => '<div class="btn more">Load More</div>',
'noneLeftText' => '',
'triggerOffset' => 2,
'negativeMargin' => 200
],
'layout' => "{items}\n{pager}",
]);
?>
This is not possible in the ListView, according to the doc:
'data' => [
'test' => function ($model, $key, $index, $widget) {
return Html::encode($model->id);
}
]
You cannot use a function here. Only static values are allowed.
Alternatively, you can provide those attributes in tags of your _photoListItem view file.