Pass data from controller to View in Laravel - laravel

I have a query that I am not able to pass to the view.
$dias_usados = calendario::where('id_funcionario', '=', $userid)
->groupBy('id_funcionario')
->sum('contaferias');
dd outputs the correct expected value.
I tried to pass to the View as follows:
return view(
'ausencia',
compact('tabela'),
['itens' => $ferias],
['dias_usados' => $dias_usados]
);
I'm having problems with the last one dias_usados. The first two work normally.
<h3>{{$dias_usados}}</h3>
Causes the following error:
Undefined variable: "dias_usados"
I also leave the path I have on the route, but I don't see anything wrong
Route::get('Ausencia', [AusenciaController::class, 'index'])->name('ausencia.index');

This is the the definition of the view helper
function view($view = null, $data = [], $mergeData = []) { }
You are misusing the function by giving it three separate arrays expecting it to get them as $data.
Fixes
return view('ausencia', [
'tabela' => $tabela,
'itens' => $ferias,
'dias_usados' => $dias_usados,
]);
return view('ausencia')
->with(compact('tabela'))
->with(['itens' => $ferias])
->with(['dias_usados' => $dias_usados]);
return view(
'ausencia',
array_merge(
compact('tabela'),
['itens' => $ferias],
['dias_usados' => $dias_usados]
)
);

Related

Yii2 ActiveRecord

I'm new to Yii and struggling to get my head around a few things.
I have model with this function:
public static function findNonGeo()
{
$query = new CallerIdentityQuery(get_called_class());
$query->select([
'cidref', 'caller_id', 'expiry', 'conf_call',
'type', 'redirect', 'destination', 'status', 'start_date',
'statusDesc' => new \yii\db\Expression("CASE status
WHEN 0 THEN 'Deactivated'
WHEN 1 THEN 'Active'
WHEN 2 THEN 'Inactive'
WHEN 3 THEN 'Unregistered'
ELSE 'Permanently Deleted' END")])
->where(['status' => '1'])
->andWhere(['type' => 'N'])
->andWhere(['custref' => Yii::$app->user->identity->typedIdentity->getId()]);
return $query;
}
And in my controller I call this method like so:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->one();
but when the data is returned its like its just ignoring the
->andWhere(['type' => 'N'])
because I can view records that aren't of type 'N'.
so I'm having to do in my controller, maybe I'm doing something wrong or just not understanding it but I don't get it:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->andWhere(['type'=>'N'])->one();
another thing when using findOne($id):
$model = CallerIdentity::findOne($id);
null is returned.
Would appreciate any form of explanation/info.
When using where you're setting the where part of the query, not adding to it. So in the findNonGeo function you're building the required where parts. But with CallerIdentity::findNonGeo()->where(['cidref' => $id]) you're removing the already added where parts.
Try like this:
CallerIdentity::findNonGeo()->andWhere(['cidref' => $id])->one();
by using andWhere you'll keep the other parts and just add another one to it.

Passing data controller to view getting undefined variable, in Laravel 5.1

I want to passing data from controller to view
$participantView = view('section.participant', ['data' => $result['data']])->render();
the $result['data'] is array data(not empty)
I do foreach in view, and got:
undefined variable: data
I also do with compact and 'with', but also getting undefined variable
What is wrong in my code?
You must return the view, Try this :
return view('section.participant', ['data' => $result['data']]);
try using
$data = $result['data'];
return view('section.participant', compact('data'));
It will do work.
Try this:
$participantView = view('section.participant')->with([
'data' => $result['data']
])->render();
and in view you will have $data variable available.
I want to passing the data to view, then I will return $data. Which is:
$data=['data_array' => ['view' => $participantView ]];
in another function, the passing data was fine.

How can I pass an object to the sheet method in Laravel Excel?

I'm trying to pass an object into the Laravel Excel sheet method but I'm not sure how. I get an undefined variable error - which I realize is because I'm inside a function that doesn't have scope to reach the object.
Ultimately I'm loading up regtypes from one table - attendees from another and creating a sheet for each regtype that contains all attendees of that type.
My code:
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$regtypes = Regtype::all();
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$excel->sheet('Attendees', function($sheet) {
$date = date('Y_m_d');
$attendees = new Attendee;
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export', array('atts' => $atts))->with('curdate',$date)->with('regtype_name',$regtype->name);
});
} //endif
} //endforeach
$excel->export('xls');
});
}
As I was writing this up I figured it out, pretty simple I should have known as it's simply a method of a Laravel Facade, so you pass the object as an array like so:
$excel->sheet('Attendees', array('regtype' => $regtype), function($sheet) {

One-shot laravel validator

I have a form where someone searches for something. Based on this form, I validate if the input is correct:
$validator = Validator::make(Input::all() , array(
'address' =>'required',
));
if($validator->fails()) {
return Redirect::to('/')->withErrors($validator);
}
After this, I want to validate something else (that a result object isn't empty), which is completely unrelated to the search. In other words, it's NOT input from a form.
1) Do I create another validator to validate this? Or
2) Is there a better way to simply check this value and spawn an object that can be returned with "withErrors"?
UPDATE
This isn't working for me:
$validator = Validator::make(
array(
'searches' => sizeof($search)
) ,
array(
'searches' => 'required|min:1'
)
);
if($validator->fails()) {
return Redirect::to('/')->withErrors($validator);
}
It's not working because for some reason it's picking up that the "searches" item should only be validated "sometimes"
you have two ways. one is custom validator
or there is a simpler way,
suppose,
private function foo()
{
$data = ''; //retrieved the data error here with whatever call you want to make
return !empty($data) ? true : false;
}
in the controller,
public function bar()
{
if(!$this->foo())
{
$messages = new \Illuminate\Support\MessageBag;
// you should use interface here. i directly made the object call for the sake of simplicity.
$messages->add('custom', 'custom error');
return Redirect::back()->withErrors($messages)->withInput();
}
}
in the view:
#if($errors->has('custom'))
<p>custom error output.</p>
#endif
it is just the outline to give you the idea.

Problems using batch_update in codeigniter

I read similar batch_update Qs but I still have issues. Thanks in advance for help! I receive error message ("One or more rows submitted for batch updating is missing the specified index.") for the following code:
Controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$link_title = $this->input->post('page_link_title', TRUE);
$link = $this->input->post('page_link_sub_title', TRUE);
$this->content_model->update_links($id, $link_title, $link);
$this->index();
}
Model:
public function update_links($id, $link_title, $link){
$data = array(
array(
'page_id' => $id,
'link_title' => $link_title,
'link' => $link
)
);
$this->db->update_batch('content_links', $data, $id);
}
Check the CI documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
so the third parameter should be a column not data.

Resources