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

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.

Related

Pass data from controller to View in 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]
)
);

Passing variable from DOMPDF controller to view

I want create a DOMPDF with laravel, and I must passing my variable to view. I've been try passing variable like below, but it still not working yet.
here my Laravel Controller
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$data = [$salesorder, $detailemployee, $detailservice];
$pdf = PDF::loadView('summary.invoice', $data);
return $pdf->download('invoice.pdf');
}
the error on my view is :
Undefined variable: salesorder
How to passing some variable from Laravel controller to DOMPDF ?
PS : dd($data) result is correctly
You have to pass the data as below
$data = [
'salesorder' => $salesorder,
'detailemployee' => $detailemployee,
'detailservice' => $detailservice
];
or try using compact
$data = compact('salesorder', 'detailemployee', 'detailservice');
You may try this following
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$pdf = PDF::loadView('summary.invoice', array('salesorder' => $salesorder,'detailemployee'=>$detailemployee,'detailservice'=>$detailservice));
return $pdf->download('invoice.pdf');
}
You can use library function to do that easily.
$pdf_text = "It will be the text you want to load";
PDF::loadHTML($pdf_text)->save('public/you-file-name.pdf');
You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)
PDF::loadHTML($pdf_text)->setPaper('a4', 'landscape')->setWarnings(false)->save('public/you-file-name.pdf')
You may try the following.
$html = view('summary.invoice', ['salesorder' => $salesorder, 'detailemployee' => $detailemployee, 'detailservice' => $detailservice])->render();
$pdf = App::make('dompdf.wrapper');
$invPDF = $pdf->loadHTML($html);
return $pdf->download('invoice.pdf');
I know this is old but this may help others. you need to use array key in your view.
Controller:
$data = [
'foo' => $bar,
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('doc.pdf');
View:
<p>{{$foo}}</p>

laravel passing data from controller to view

my controller having the below format. how can i transfer to view in laravel
i tried ->with($inboxMessage) . The browser not even completely loaded. It's loading for a lifetime.
$inboxMessage[] = [
'messageId' => $message_id,
'messageSnippet' => $snippet,
'messageSubject' => $message_subject,
'messageDate' => $message_date,
'messageSender' => $message_sender
];
you can return your view in controller with data like below
return view('viewname', ['inboxMessage' => $inboxMessage]);
now $inboxMessage will available in view.
return view("path to your view", compact('inboxMessage'));
return view("view_name",['data'=>$inboxMessage]);
and in view you can access using foreach loop or use print_r($data)
in view if you are using blade template then
#foreach($data as $val)
{{$val->messageId}}
#endforeach
or else use normal foreach loop

Is there a shorter way than this to check values when using same Add form for Edit

I have the same code for an Add and an Edit form. Therefore in the controller I need a check to check if a) POST vars submitted (for saving), and if not then b) the original values (for editing) and if not then no value (blank for Adding). I put them in a $data array to pass to the view. Then in the form I can put:
value="<?php echo $member_id;?>"
So my question is, in Codeigniter is there a shorter way than the following to check if POST, then if not check if the original data exists, and if not then its nothing.
$data = array(
'member_id' => ( isset($_POST['member_id']) ? $_POST['member_id'] : (isset($member->member_id ) ? $member->member_id : '') )
);
I know about set_value() but looks like that wont add in the current data when editing a form, so have not used that.
You can allways make function for it.
function get_value_or_default($array, $key, $default) {
return isset($array[$key] ? $array[$key] :
isset($default) ? $default : '';
}
Or even better:
function update_from_post($object) {
$data = array();
foreach ($object as $prop_name => value) {
$value = get_value_or_default($_POST, $prop_name, $object->{$prop_name});
$data[$prop_name] = $value;
}
Assuming you have different methods in the controller for create vs edit: (you can use the same view in different methods by specifying it in $this->load->view()):
Your create method would assume it was new, and always read the $_POST variables (if $_POST)
Your edit method would first load the object from the database, and then overwrite with $_POST variables if present.
Finally, CodeIgniter has the input helper:
$this->input->post('field_name');
returns false if that field is not in $_POST.
To use your code above:
create
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : '')
);
edit
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : $member->member_id )
);

Why does my data not pass into my view correctly?

I have a model, view and controller not interacting correctly, and I do not know where the error lies.
First, the controller. According to the Code Igniter documentation, I'm passing variables correctly here.
function view() {
$html_head = array( 'title' => 'Estimate Management' );
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('html_head', $html_head);
$this->load->view('estimates/view', $estimates);
$this->load->view('html_foot');
}
The model (short and sweet):
function get_estimates() {
$query = $this->db->get('estimates')->result();
return $query;
}
And finally the view, just to print the data for initial development purposes:
<? print_r($estimates); ?>
Now it's undefined when I navigate to this page. However, I know that $query is defined, because it works when I run the model code directly in the view.
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('estimates/view', $estimates);
You're loading the return of $this->Estimatemodel->get_estimates() as the array of view variables. In other words, all the children of $estimates (assuming it can be treated as an array) are available in your view. But not the parent element.
The key here is when loading a view the second parameter needs to be an array of values, not just a single value.
$this->load->view('estimates/view', array('estimates' => $estimates));
That should get the result you're looking for, in fact, you're already doing that for the html header view. Even though that view only has one variable, it's passed as the single element of an array:
$html_head = array( 'title' => 'Estimate Management' );
$this->load->view('html_head', $html_head);
The documentation shows that the object you pass to the view must be an associative array.
$data = array(
'estimates' => $estimates
);
$this->load->view('estimates/view', $data);
Docs here

Resources