laravel passing data from controller to view - laravel

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

Related

Sharing create and update form in Laravel

I'm learning Laravel and have created a single form that is shared by the create and edit controller.
The create controller just returns the view.
public function create()
{
return view('hotels.create');
}
However i've had to put my edit controller and return it in an array
return view('hotels.edit', [
'hotel' => Hotel::with('hotelFacilities')->where('id', $id)->get()
]);
Now in my view I have to pass
$hotel[0]->hotelFacilities->fitness_centre
instead of
$hotel->hotelFacilities->fitness_centre
So now my create view is looking for $hotel where it is now $hotel[0] in the shared view. How can I change this so it's looking at the same reference to the $hotel variable?
If you are using the same view for create and edit, you have to pass the same object for views. change code as below.
return view('hotels.create', [
'hotel' => new Hotel
]);
return view('hotels.edit', [
'hotel' => Hotel::with('hotelFacilities')->where('id', $id)->first()
]);

Second page of paginate won't display the filtered data

I'm want to use paginate in laravel but didn't work perfectly. When i paginate the filtered data it worked fine, but only for the first page.The problem is when i moved to the second page, it will display the second page of all data not the filtered data. i want it so the second page will display the second page of filtered data
in blade.php I'm using links to call the paginate(for moving page)
<div class="paginate">
{{ $data->links() }}
</div>
Here's the filter controller
function filter(Request $request){
$data = profiles::when($request->has('pNamaLengkap'), function($query) use ($request){
$query->where('pNamaLengkap','like','%'.$request->pNamaLengkap.'%');
});
if($request->pJobDescription != 'All'){
$data = $data->where('pJobDescription','like','%'.$request->pJobDescription.'%');
}
if($request->pUnitKerja != 'All'){
$data = $data->where('pUnitKerja','like','%'.$request->pUnitKerja.'%');
}
if($request->pDirectorate != 'All'){
$data = $data->where('pDirectorate','like','%'.$request->pDirectorate.'%');
}
if($request->pRank != 'All'){
$data = $data->where('pRank','like','%'.$request->pRank.'%');
}
return view('export', [
'data' => $data->paginate(5),
'jobdescription' => jobdes::all(),
'unitkerja' => unitkerja::all(),
'direktorat' => direktorat::all(),
'rank' => rank::all(),
'oldjob' => $request->pJobDescription,
'oldunit' => $request->pUnitKerja,
'olddir' => $request->pDirectorate,
'oldrank' => $request->pRank,
'oldname' => $request->pNamaLengkap
]);
// return redirect('export')->with([
// 'data' => $data,
// 'jobdescription' => jobdes::all(),
// 'unitkerja' => unitkerja::all(),
// 'direktorat' => direktorat::all()
// ]);
}
I'll make an example from a picture
Filtered JobDescription Page 1
Page 2 that reset the filter and display all data
in the URL parameter it change it from
http://127.0.0.1:8000/filterexport?pNamaLengkap=&pJobDescription=Full+Stack+Developer&pUnitKerja=All&pDirectorate=All&pRank=All&export=Filter
into
http://127.0.0.1:8000/filterexport?page=2
for extra note, the paginate is working but just didn't work when you want to view the second page because somehow it reset the input filter
Thank you for reading this page, i really need help in this one
$data->paginate(15)->withQueryString();
use withQueryStrings() method with paginated data.
another solution is replacing
{{ $data->links() }}
to the
{{ $data->appends(Request::except('page'))->links() }}
For lower versions of Laravel, you can use this:
{!! str_replace('/?', '?', $data->appends(Input::except('page'))->render()) !!}

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.

Is it possible to send the html output from a blade template through a JSON?

In an application I am loading the pages through AJAX calls (instead of the regular browser loading) so I'm wondering if it's possible to add the output from a blade template in a JSON return?
I designed my application around a JSON looking like this:
return Response::json(array('id' => "1",'urlString' => "admin/posts", 'html' => "<p>test</p>"),200);
I need the HTML index in the JSON to be the output from a blade template, is this possible?
Yes, that should be possible by calling the render() method of the view :
$view = View::make('some.view');
$view->somevariable = 'some value';
$html = $view->render();
return Response::json(array('id' => "1",'urlString' => "admin/posts", 'html' => $html);

where to add- class="required" (jquery validation) in form_dropdown?

I have a dropdown list which is being populated from database. It is working fine but in the form_dropdown in the view file, I want to add class="required" for validating the dropdown using Jquery. I have tried to make it work but as it turned out it won't work. Would you please kindly help me where exactly to put the class="required" - and make the jquery validation work?
Thanks in Advance
I have this in my controller
// To get the batch name
$this->load->model('dropdown_batchlist');
$data['dropdown_batchlist']= $this->dropdown_batchlist->dropdown_batchlist();
this in my model-
function dropdown_batchlist() {
$this->db->select('batchname, batchid');
$records=$this->db->get('batch');
$data=array();
// add it here as the first item in the array,
// assuming you don't have a $row->batchid of 0 in your results.
$data[0] = 'SELECT';
foreach ($records->result() as $row)
{
$data[$row->batchid] = $row->batchname;
}
return ($data);
}
And this in my view file
<?php echo form_dropdown('batchid', $dropdown_batchlist,'', 'class="required"' ); ?>
The Problem is Solved
I have figured out the problem. The view file was okay, all I had to do is replace $data[0] = 'SELECT'; with
$data[' '] = 'SELECT';
Thanks
Try setting the attributes using an associative array:
$attributes = array(
'name' => 'batchid',
'class' => 'required',
'options' => $dropdown_batchlist
);
echo form_dropdown($attributes);

Resources