How I can take event_id column value?
Thanks
Change
I'm using laravel-admin and I have to show header with event name, but when the event do not have participant get error:
Trying to get property 'event' of non-object (View: /usr/home/gernikaracing/Apps/timing/vendor/encore/laravel-admin/resources/views/grid/table.blade.php)
Code
protected function grid()
{
$grid = new Grid(new Participant);
$eventId = request()->route('event');
$grid->model()
->where('event_id', $eventId)
->orderBy('number', 'asc');
$grid->header(function ($query) {
$event = $query->first()->event->name;
return "<div style='background-color:#f7f7f7; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;'>$event</div>";
});
$grid->authorized(__('Authorized'))->editable()->sortable();
$grid->number(__('Number'))->sortable();
$grid->driver()->full_name(__('Driver'))->sortable();
$grid->admin_user()->name(__('Author'))->sortable();
$grid->updated_at(__('Updated at'))->sortable();
$grid->disableCreateButton();
$grid->disableFilter();
$grid->disableActions();
$grid->disableExport();
$grid->disableRowSelector();
return $grid;
}
$builder->get()->first()->event_id;
Related
I want to filter the data with array, but return null if one of element in array is false.
for example my existing cars color is ['black', 'white', 'red']
I already try with whereIn but in the docs say given containing value within the given array thats why it's still return the data.
In my controller:
// the request param is /cars?color=black,white,yellow
$cars = Car::latest()->filter(request(['color', ...etc]))->get();
// $cars should empty
// the request param is /cars?color=black,white,red
$cars = Car::latest()->filter(request(['color', ...etc]))->get();
// $cars shouldn't empty
In my model:
public function colors() {
return $this->hasMany(Color::class);
}
public function scopeFilter($query, array $filters) {
$query->when($filters['color'] ?? false, function ($query, $colors) {
$colors = explode(',', $color);
$query->whereHas('colors', function ($q) use ($color) {
$q->whereIn('color_name', $colors);
});
});
}
It sounds like you want a where condition for each selected colour:
$cars = Car::whereHas('colors', function($q) use ($filter) {
foreach ($filter as $color) {
$q->where('color_name', $color);
}
})->get();
Laravel has the function whereIn Documentation here
$filter = ['black', 'white', 'yellow'];
$cars = Car::whereIn('color_name', $filter)->get();
I am trying to figure out how to use Chartisan and my controllers in Laravel. Having spend a couple of days on this, I have to admit that I am missing some fundamental because I understand the error, I just can't fix it..
What I have done so far is followed this https://charts.erik.cat/guide/installation.html#publish-the-configuration-file and reading multiple other guides online on how to solve it. If I stick to the guide, with the basic example then it works fine, but I want to create multiple charts, based on id/user variables which require I get the information from my database..
My problem is: "Call to undefined method App\Charts\SampleChart::labels()"
Are there anyone who has experience with this issue and tell me how to fix?
SampleChart.php (location: app/Charts/SampleChart.php)
declare(strict_types = 1);
namespace App\Charts;
use Chartisan\PHP\Chartisan;
use ConsoleTVs\Charts\BaseChart;
use Illuminate\Http\Request;
class SampleChart extends BaseChart
{
/**
* Handles the HTTP request for the given chart.
* It must always return an instance of Chartisan
* and never a string or an array.
*/
public ?string $name = 'my_chart';
public ?string $routeName = 'my_chart';
public function handler(Request $request): Chartisan
{
return Chartisan::build();
}
}
My Controller is:
namespace App\Http\Controllers;
use App\Charts\SampleChart;
use App\Charts\ExerciseInsight;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ExerciseInsightChartController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request): Chartisan
{
$samplechart = new Samplechart;
$exercise = 16;
$created_at = [];
$exercise_name = [];
$exercise_weight = [];
$exercise_rep = [];
$records = DB::table('dump_all_records')->where('exercise_id',"=", $exercise)->get();
foreach ($records as $record)
{
array_push($created_at, $record->created_at);
array_push($exercise_name, $record->exercise_name);
array_push($exercise_weight, $record->exercise_unit_value);
array_push($exercise_rep, $record->exercise_round_value);
}
// dd($samplechart);
$samplechart->labels($created_at);
$samplechart->dataset(['Weight','line', $exercise_weight]);
// $samplechart->dataset('Reps','line', $exercise_rep);
return view('insight.exercise_insight', compact('samplechart'));
}
}
my view is:
<!-- Charting library -->
<script src="https://unpkg.com/echarts/dist/echarts.min.js"></script>
<!-- Chartisan -->
<script src="https://unpkg.com/#chartisan/echarts/dist/chartisan_echarts.js"></script>
<!-- Chart's container -->
<div id="chart" style="height: 300px;"></div>
<script>
const chart = new Chartisan({
el: '#chart',
url: "#chart('my_chart')",
hooks: new ChartisanHooks()
.colors(['#4299E1','#FE0045','#C07EF1','#67C560','#ECC94B'])
// .datasets([{ type: 'line', fill: false }, 'bar'])
.datasets(
[
{
type: 'line',
fill: true ,fillColor : 'rgba(38,198,218,1)',
strokeColor : 'rgba(38,198,218,0)',
pointColor : '#26c6da',
pointStrokeColor : 'rgba(38,198,218,0)',
pointHighlightFill : '#fff',
pointHighlightStroke: 'rgba(38,198,218,1)',
},
{
type: 'line',
fill: true
}
]
)
.axis(true)
.tooltip()
});
</script>
Since the documentation has wroten, you can pass the data manually using data : {...} property.
So, the first step is call the Chartisan class, but don't forget to call the ServerData Class first, because the Chartisan Class constructor need parameter a ServerData Class.
In YourController.php
use Chartisan\PHP\Chartisan;
use Chartisan\PHP\ServerData;
In your method,
public function index () {
$serverdata = new ServerData;
$chart = new Chartisan($serverdata);
$chart->labels(
['First', 'Second', 'Third', 'Four', 'Five',
'Six', 'Seven', 'Eight', 'Nine', 'Ten']);
/**
* This your query will be placed,
* just for example :
*/
for ($i = 1; $i < mt_rand(5,9); $i++) {
$chart->dataset('Attribute '. $i, [
mt_rand(3,50), mt_rand(3,50), mt_rand(3,50), mt_rand(3,50), mt_rand(3,50),
mt_rand(3,50), mt_rand(3,50), mt_rand(3,50), mt_rand(3,50), mt_rand(3,50)
]);
}
/**Please remember on this chartisan version,
* Chartisan class will return an Object
* But the frontend loader just read a JSON format only.
* so it's easily to call Chartisan toJSON method.
* */
$chart = $chart->toJSON();
return view('your.view', [
'chart' => $chart,
]);
}
Next, in your blade view, this must be same scheme to load the chart according the documentation. But don't forget to escape $chart variable at blade syntax.
<script>
const chart = new Chartisan({
el: '#chart',
data: {!! $chart !!},
hooks: new ChartisanHooks()
.title({
textAlign: 'center',
left: '50%',
text: 'Example Chart Title',
})
.colors()
.datasets('line')
.axis(true)
.tooltip()
});
</script>
And the html part,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<!-- here your chart-->
<div id="chart" style="height: 300px;"></div>
</div>
</body>
</html>
CMIIW. Hope its help.
Thank you.
Laravel 7: I'm building a report view using Maathwebsite.
My function looks like this.
public function view(): View
{
return view('dash._exportSales' ['datas' => Sales::where('store_id', 30)->get()]);
}
This will output an excel file with all the information, as you can see I'm using a basic settings, so far so good, the problem that I have is that I have no idea how to give a parameter, the number 30 is the ID but how can I retrieve that number?
my route looks like this
Route::get('sales/export_sales', 'SaleController#export')->name('export.sales');
if I use: Route::get('lots/export_sales/{id}', 'SaleController#export')->name('export.sales');
doesn't do anything still can't get the ID in to my ExportController which looks like this:
class SaleExport implements FromView
{
public function view(): View
{
return view('dash._exportSales' ['datas' => Sales::where('store_id', 30)->get()]);
}
}
If add a declare a private, const, public or protected variable it does nothing nor can set or retrieve a value...
So how can I retrieve that ID #?
Export is on my SalesController.php, the above class is on my SalesExport.php
SalesExport looks like this:
public function export($id)
{
$name = 'store_' . $id . '.xlsx';
return Excel::download(new SaleExport(), $name);
}
My routes looks like this.
Route::get('sales/export_sales/{id}', SaleController#export')->name('export.sales');
Route::get('sales', 'SaleController#sales')->name('sales');
you can do this by following way
Route::get('sales/export_sales/{id}', 'SaleController#export')->name('export.sales');
and your method should be like this
public function view($id)
{
return view('dash._exportSales' ['datas' => Sales::where('store_id', $id)->get()]);
}
The whole problem was that I never use __construct() on my controller.
So, here is the full flow
Laravel 7+
maatwebsite/excel
First the view:
<table>
<thead>
<tr>
<th colspan="4">Report {{ date('Y-m-d H:i:s') }} | Store: {{ $datas->store_name }}</th>
</tr>
<tr>
<th style="font-weight: bold; text-align: center; vertical-align: middle;">Sale #</th>
<th style="font-weight: bold; text-align: center; vertical-align: middle;">Amount</th>
<th style="font-weight: bold; text-align: center; vertical-align: middle;">Date</th>
<th style="font-weight: bold; text-align: center; vertical-align: middle;"></th>
</tr>
</thead>
<tbody>
#foreach($datas->items as $data)
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->amount }}</td>
<td>{{ $data->date }}</td>
<th></th>
</tr>
#endforeach
</tbody>
</table>
It has more columns but lets keep it simple.
in app create a new folder app/Exports, inside SaleExport.php
Inside that file use it like this:
namespace App\Exports;
use App\Sale;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
//use Illuminate\Support\Facades\DB;
class SaleExport implements FromView
{
protected $id;
public function __construct($id)
{
$this->id = $id;
}
public function view(): View
{
return view('dash._exportsales', ['datas' => Sales::where('store_id', $this->id)->get()]);
}
}
On this file was the problem there was no __contruct(); but I didn't notice because it was been called from another controller with __construct(); so my "view controller looks like this.
namespace App\Http\Controllers\Dash;
use App\Order;
use App\Sale;
//use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use App\Exports\SaleExport;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class ReportController extends Controller
{
public function index(Request $resquest)
{
// This is just a basic index nothing fancy
// I could avoid this 'where' but client only want this status...
$query = Order::where('status', 1);
// a few IF's to build query with orWhere();, limit();... etc
// here is where I could add the 'status' and set it as filter...
$data = $query->paginate($limit);
return view('dash.order_list', compact('data');
}
// Here is where we call our Export
public function export($id)
{
$name = 'sales_' . $id . '.xlsx'; // Set the same for the file
return Excel::download(new SaleExport($id), $name);
}
}
Then you can just called in your router Route::get('sales', 'SaleController#export')->name('sales'); and thats it!, you get a full report using a blade view in to your excel software... problem was is that I wasn't using __construct(); on my FromView controller
Reference about this method can be found here: docs.laravel-excel, problem on my part was that the construct, without a construct can't use parameters, now that is been set I can use parameters and do more filters for a better report.
Thanks to #lagbox who reminded me of that.
I'm trying to use a color value specified in a map as the $color argument for darken(), like so:
border: 1px solid darken(color(lowlight), 10);
This relies on a couple of simple functions and maps I've written (extraneous values removed):
#function gray($color) {
#return map-get($grays, $color);
}
#function color($color) {
#return map-get($colors, $color);
}
$grays: (
x-light: #f8f8f8
);
$colors: (
lowlight: gray(x-light)
);
I'm getting this error:
Argument $color of darken($color, $amount) must be a color
It works if I supply it with a variable, but not with a function value. Is there a solution to this?
I need to pass a variable from the controller to the view, to use it in the script and configure the Highstock graph. I have a problem with the date conversion, and the use of arrays. Unfortunately, the data are not included in the chart.
I receive the data correctly in the view, but I think we need to "format" it via json_encode or whatever.
Can you tell me why and how can I solve the problem?
statistiche.blade.php
#section('content')
<div id="container" style="height: 400px; min-width: 310px"></div>
#stop
#section('css')#stop#section('js')
<script>
var data = [#php echo $data #endphp];
// Create the chart
Highcharts.stockChart('container', {
rangeSelector: {
selected: 1
},
title: {
text: 'Richieste ricevute'
},
series: [{
name: 'Richieste ricevute',
data: data,
tooltip: {
valueDecimals: 2
}
}]
});
</script>
#stop
statisticheController.php
public function index(){
/* calcolo il totale delle richieste ricevute */
$richieste = Richiesta::groupBy(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'))
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as data'), DB::raw('count(*) as richieste_totali'))
->get();
foreach($richieste as $richiesta) {
$data[] = [$richiesta->data, $richiesta->richieste_totali];
}
return view('layouts.statistiche', compact( 'data'));
}
Instead of
var data = [#php echo $data #endphp]
you can just have
var data = #json($data);
Also, instead of running #php echo $stuff; #endphp you can also echo stuff like {{$stuff}}
Check this docs https://laravel.com/docs/5.6/blade