Closed laziestcoder closed 6 years ago
Simple
protected function FacultyGrid($value) {
// catching the value as $value
return User::grid(function (Grid $grid) use ($value) {
$grid->model()->where('userrole', '=', $value); // want to use the value here as $value
});
}
What about if I want to call a controller function after $grid->model();
return User::grid(function (Grid $grid) use ($value) {
$grid->model()->where('userrole', '=', $value); // want to use the value here as $value
$grid->jobid(trans('Varsity ID'))->sortable();
$grid->image(trans('admin.avatar'))->display(function ($s) {
$file = $this->imageValidate($this->jobid);
// 'imageValidate' is defined in the same controller
// it returns a bool value
if($file){
echo "worked";
}else{
echo "failed";
}
}
});
But $this->functionName() is not working. It shows me that there is no function defined. :(
BadMethodCallException In Builder.php line 2816 : Method Illuminate\Database\Query\Builder::imageValidate does not exist.
Thank you very much @krsmga
If I understood correctly, you created a method called imageValidate in the same controller.
User: grid(function is a closure, it's like it's another environment, and it does not see anything from outside, nor does it return anything out.
If imageValidate is in the same class, put $this together with $value.
use ($value, $this)
It did not work. See my code. This is my controller code.
class StudentController extends Controller
{
public function index(Content $content)
{
return $content
->header(trans('Users'))
->description(trans('Student List'))
->body($this->grid(1)->render());
}
protected function grid($value)
{
return User::grid(function (Grid $grid) use ($value) {
$grid->model()->where('userrole', '=', $value);
$grid->id('ID')->sortable();
$grid->jobid(trans('Varsity ID'))->sortable();;
$grid->image(trans('admin.avatar'))->display(function ($s) {
//here I wan to use the 'imageValidate()' function
$url = "http://asdasdasd.xx/blabla/" . $this->jobid . ".jpg";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Get URL content
$lines_string = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// close handle to release resources
curl_close($ch);
//output, you can also save it locally on the server
$file2 = $lines_string;
$file = $retcode;
if ($file == 200 && $file2[0] != '<') {
return "<img style='max-width:100px;max-height:100px' class='img img-thumbnail' src='http://asdasdasd.xx/blabla/" . $this->jobid . ".jpg' alt='" . $this->name . "'/>";
} else {
return "<img style='max-width:100px;max-height:100px' class='img img-thumbnail' src='/storage/image/user/" . $this->image . "' alt='" . $this->name . "'/>";
}
});
$grid->name(trans('Name'));
$grid->email(trans('Email'));
$grid->gender(trans('Gender'))->display(function ($s) {
return $s ? 'Female' : 'Male';
});
$grid->confirmed(trans('Activated'))->display(function ($s) {
return $s ? 'Yes' : 'No';
})->label();
$grid->confirmation(trans('Verified'))->display(function ($s) {
return $s ? 'Yes' : 'No';
})->label();
$grid->created_at(trans('Member Since'));
$grid->updated_at(trans('Last Updated'));
$grid->actions(function (Grid\Displayers\Actions $actions) {
if ($actions->getKey() == 1) {
$actions->disableDelete();
}
// $actions->disableEdit();
$actions->disableview();
});
$grid->tools(function (Grid\Tools $tools) {
$tools->batch(function (Grid\Tools\BatchActions $actions) {
$actions->disableDelete();
});
});
$grid->filter(function ($filter) {
// Sets the range query for the created_at field
$filter->between('jobid', 'Search by Varsity ID');
});
$grid->disableCreateButton();
$grid->perPages([10, 15, 20, 25, 30, 35, 40, 45, 50, 100]);
});
}
protected function imageValidate($pic){
$url = "http://asdasdasd.xx/blabla/" . $pic . ".jpg";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$lines_string = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$file2 = $lines_string;
$file = $retcode;
if ($file == 200 && $file2[0] != '<') {
return true;
} else {
return false;
}
}
}
In theory, the use of $this should work as you can see below in a code that I did.
public function edit($id) {
return Admin::content(function (Content $content) use ($id) {
$this->numIdEdit = $id; <<------------------------------------------------
$qryQtdSum = DB::table('lotofacil_apostas')
->select('apo_valor')
->where('id', '=', $this->numIdEdit);
$this->numQtdApo = $qryQtdSum->count();
$this->numValApo = $qryQtdSum->sum('apo_valor');
unset($qryQtdSum);
$this->setLastResults(); <<-----------------------------------------------
...
});
}
protected function setLastResults() { <<----------------------------
$this->qryLastResults = DB::table('lotofacil_results')
->select('fac_numero', 'fac_data', 'fac_pre15val', 'fac_pre15qtd', 'fac_data_prox', 'fac_valor_prox', 'fac_acumulado')
->orderBy('fac_numero', 'desc')
->limit(1)
->get()
->toArray();
....
}
But I think in your case, the closure is referring to the Grid, and maybe for some reason the $this variable ends up being the closure grid itself. Therefore it does not find the imageValidate method. Then try, encapsulate $this into another variable and use it as in the example below.
protected function grid() ....
$self = $this; <<<--------------
return User::grid(function (Grid $grid) use ($value, $self) { <<<-----------------
$file = $self->imageValidate($self->jobid); <<<------------
});
It didn't work. Look into my code very carefully.
protected function grid($value)
{
$self = $this; // '$this' here it means the controller
return User::grid(function (Grid $grid) use ( $value, $self) { // $self is controller '$this'
$grid->model()->where('userrole', '=', $value);
$grid->jobid(trans('Varsity ID'))->sortable();
// ' $this->jobid ' here $this means the value, where it equals to '$grid->jobid'
$file= $self->imageValidate($this->jobid); // I want to use this $file value
$grid->image(trans('admin.avatar'))->display(function ($s) {
if($this->file){ // here I am using the $file value . But it does not work here.
echo "true";
} else {
echo "false";
}
});
Thank you very much for trying to help me.
$grid->image(trans('admin.avatar'))->display(function ($s) use ($file) { <<<<<-------------------
if($file){ <<<<-----------------------
echo "true";
} else {
echo "false";
}
});
Thank You very much :D
I have used this grid function
$content->body($this->FacultyGrid(1)->render()); // passing value 1
How can I do it?