I have 2 separate partials views. I want to get the HTML with the given data from those partials views and have to send a Json response from controller.
Here is my code snippet.
public function myControllerFunction()
{
$response['products'] = view('search._partials.product_box')->with('data', $data['products]);
$response['filters'] = view('search._partials.facet_filters')->with('data', $data['filters]);
return $response;
}
I want to achieve some thing like it. This is possible with plain php code but is there any way to achieve it with this framework.
Just use render() function to generate html from view in controller
Your function should look like this
public function myControllerFunction()
{
$response['products'] = view('search._partials.product_box')->with('data', $data['products])->render();
$response['filters'] = view('search._partials.facet_filters')->with('data', $data['filters])->render();
return response()->json($response);
}
Related
I have model called Page and view called view.blade.php
//this is a model
public function Test()
{
return 'test';
}
//this is the template
<h1>{{$Test}}</h1>
how can I do this? please help me?
As I understood, you want to call model function inside your view? Do it like this:
{{Page::Test()}}
Edit:
If you need to use $this in your function to pass some data for your function (if that was what you were asking in comments below), you can do something like this. First define your static function:
public static function getPages()
{
return [
//some logic (get all pages)
];
}
Now, let's say this function will return multiple pages. If you want to filter them, and to display only one page on your view, you can pass the id of that view as a parameter to a next function which you will then pass to your view:
public function getSinglePage()
{
return self::getPages()[$this->id];
}
Lastly, in order to display the output of that function, use the same method as above, with new function name:
{{Page::getSinglePage()}}
how can i pass a value from my query to the same view in CI. I am having a situation of like this:
public function index() {
$myinfo = $this->Myprofile_model->mydata();
$data['myinfo'] = $myinfo
$this->load->view('header');
$this->load->view('myprofile',$data);
$this->load->view('footer');
}
public function getResults($id) {
$fetchdata = $this->Myprofile_model->fetchresult($id);
$data['userselect'] = $fetchdata;
$this->load->view('myprofile',$data);
}
i already load my wholepage in index. and now i want to make another function to my button which is the getResults($id) function in my controller, and i want to display my another data from database to the same page. When i do the top code, my view is duplicating, because i loaded the $this->load->view('myprofile',$data); twice.
1: Create helper function and use that function on view page.
OR
If you want records for single id then merge both function in one.
If there is many ids and you want to fetch records on click for each id then you can use ajax.
$( document ).ready(function() {
$('.class_name').click(function(){
// place your code here to fetch records.
// on success you can display the result on same page
})
});
I am Not getting clearly but here is the controller to load multiple data on view:
public function index($id) {
$myinfo = $this->Myprofile_model->mydata();
$fetchdata = $this->Myprofile_model->fetchresult($id);
$this->load->view('header');
$this->load->view('myprofile',['myinfo'=>$myinfo,'fetchdata'=>$fetchdata]);
$this->load->view('footer');
}
You can use
$this->load->view('myprofile',['myinfo'=>$myinfo,'fetchdata'=>$fetchdata,'third_data'=>$third_data.. as many as you like]);
Here in your code you are passing an id to the getResults function so if you would like to load the data form this function you can do the same with this function. or pass the id to the same index function to get the result.
I have a controller that outputs data from the database in raw JSON format.
I want this to function as an API and allow anyone to make views with any technology that can consume the JSON i.e. Angular, Jquery/Ajax.
However I also want to make a view in Laravel.
So what's the best practice for creating a view from Laravel that uses data from a controller while still allowing the controller to output raw JSON?
The options I'm thinking of are to call the controller from the view(not good?) or to create extra routes.
Route::get('sample', function ()
{
$data = getDataFromSomewhere();
if (Request::ajax())
{
return Response::json($data);
}
return View::make('some.view', compact('data))
});
I would separate API controllers, but you could do something like this if you want one controller to handle all response formats:
URL:
mySite.com/getData?output=json
Controller:
public function index()
{
$data = MyModel::all();
switch(Request::query('output')){
case 'json':
return Response::json($data, $this->responseCode, $this->accessControl);
case 'xml':
return Response::make($data, '200')->header('Content-Type', 'text/xml');
default:
return View::make('data.myData', compact($data));
}
}
I'm migrating a non-MVC application to Laravel 4.2 and I'm unsure of the best way to accomplish this task. I have several reports created on routes like this:
/reports/this_report
/reports/that_report
/reports/another_report
These actions query the database, run a bunch of calculations, and generate some html tables and forms.
What I need to add now is a page like this:
/reports/dashboard
This dashboard page should display the output of all 3 reports in a condensed format, each with a "click to view details" link that takes the user to the main report page.
Is there a way for the dashboard action to call each of the report actions, and use their output as data in the dashboard view?
Here's a little code of how you would do this. I'm not exactly sure how you have everything structured, so you might have to adapt this a little.
Lets say you have a route for the dashboard like this.
Route::get('/reports/dashboard', DashboardController#showDashboard');
This route should call a controller method that will do your processing.
class DashboardController extends BaseController
{
public function showDashboard()
{
return View::make('dashboard')->with(array(
'report1_data' => $this->getReport1Data(),
'report2_data' => $this->getReport2Data(),
'report3_data' => $this->getreport3Data()
));
}
public function getReport1Data() { //calculations, return array of results }
public function getReport2Data() { //calculations, return array of results }
public function getReport3Data() { //calculations, return array of results }
public function showThisReport()
{
$data = $this->getReport1Data();
return View::make('report')->with(array('data' => $data));
}
public function showThatReport()
{
$data = $this->getReport2Data();
return View::make('report')->with(array('data' => $data));
}
public function showAnotherReport()
{
$data = $this->getReport3Data();
return View::make('report')->with(array('data' => $data));
}
}
So, this dashboard method will call the other methods (that you will also include in this controller) that will query the database and calculate the reports.
Then it returns a View with all of the data. The view will format the data and display it to the user.
Now, to make it so you can see the detailed view of each report, I suggest adding a couple more methods and routes to show detailed views.
Route::get('/reports/this_report', 'DashboardController#showThisReport');
Route::get('/reports/that_report', 'DashboardController#showThatReport');
Route::get('/reports/another_report', 'DashboardController#showAnotherReport');
I hope this helps! Good luck.
I'm trying to generate ajax specific responses from my controllers by using the Request::ajax() method, which is working just fine. The only problem is that the way I have it set up right now isn't really a nice looking solution.
My controller:
class HomeController extends BaseController {
protected $layout = 'layouts/main';
public function __construct()
{
$this->beforeFilter('auth');
}
public function getIndex()
{
$view = View::make('content.home.index');
if(Request::ajax()) return $view; //For ajax calls we only want to return the content to be placed inside our container, without the layout
$this->layout->menu = 'content.menu';
$this->layout->content = $view;
}
}
So right now, for every method I define within my controllers I need to add the code snippet that checks for an AJAX request and returns a single view if the statement returns true.
This leads to my question that is probably more PHP related than it is to the framework;
Is there a way of executing my AJAX check on every method call, without actually placing it inside the method? Or is there some other solution to keep my code DRY?
Thanks in advance!
PS: This is my first post on stackoverflow, so feel free to correct me if I made any mistakes
Create a new barebone layout named 'layouts/ajax' (or any name you like).
<?php echo $content ?>
In your Base controller, override this setupLayout() function.
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$layout = Request::ajax() ? 'layouts/ajax' : $this->layout;
$this->layout = View::make($layout);
}
}
Change your getIndex() function to this.
public function getIndex()
{
$view = View::make('content.home.index');
$this->layout->menu = 'content.menu';
$this->layout->content = $view;
}
Now non-ajax requests will be rendered using layout set in the controller, where as ajax requests will receive whatever set to $this->layout->content.
Note : Controller will neglect the layout setup in setupLayout(), if the called method returns truthy value. So this method will not work for functions like below.
public function getIndex()
{
return View::make('content.home.index');
}
You could just change the layout property, in the constructor, if it's an ajax request:
public function __construct()
{
$this->beforeFilter('auth');
if(Request::ajax()) {
$this->layout = '';
}
}
If it doesn't work try setting it to NULL instead.
Why would you return a VIEW via ajax? Are you using it to create a SPA? If so there are better ways. I'm generally against returning HTML via AJAX.
The route I'd go in your position is probably opposite of how you're doing it. Render the view no matter what, if the request is ajax, pass the extra data back and have JS render the data on the page. That's essentially how most Javascript MVC frameworks function.
Sorry if I am totally missing the point here, just going on an assumption of your end goal with the info you provided.