Passing data to layout in Laravel 5.2 on each request - php

Hi I am building an application using laravel 5.2 I have a messaging system and I would like to populate some sections in my layout like 'Current Unread Messages Count' and a summary of the last 5 messages.
The way i was gonna go about it is to call a method get the data that I need and then pass the data to the layout and then render the view.
I understand this can be done with a view composer but I have no idea how. Any input would be greatly appreicated. Thanks in advance

Yes, you can do that with a view composer
Let's suppose you have a my_menu.blade.php view file and you want to pass some data to it. In a service provider's boot method do:
//every time the my_menu view is rendered, this callback will be called
View::composer('my_menu', function( $view )
{
$data = //get your data here
//pass the data to the view
$view->with( 'data', $data );
} );
Now, everytime the my_menu view will be rendered, Laravel will call your callback, fetch the data and pass the data to the view. So, in your view file, you can access the data with $data

Related

Laravel: How can i pass data from controller to blade using yajra/laravel-datatables

I am using yajra/laravel-datatables. It's successfully installed. Now, how can i pass data Laravel Controller to blade??
$contacts = Contact::Where('is_deleted',0)->get();
This is the code. There are lots of data. For those reason, It takes lots of time to load in dataTable. I want to load and pass data to dataTable using yajra/laravel-datatables.
There are many option which you can use
compact method
$data = Contact::Where('is_deleted',0)->get();
return view('home',compact('data'))
with method
$data = Contact::Where('is_deleted',0)->get()
->with('data',$data);
For more information, read the documentation

Save blade templates to database rather than file

I want to save my blade templates to database, because the header and footer of each page is customizable for the user. I want to let my users create the layout themselves and then for each request from a given user, I want to serve the page, using the layout specified by that user.
The necessary variables that are passed by the controller are provided to them in the documentation.
Note: I trust my users. They are all stake-holders of the project and are programmers, so server side code execution is acceptable.
Although this is an old post but just in case someone stumbles across it like I did. I achieved similar while using the Laravel Framework, by saving the view in database such that, whenever I need to display the view, I retrieve it from DB, and load it into a file using the file_put_contents() php function and render it with the view() method. For example;
$blade = DB::table('pages')->where('name', 'index')->first();
file_put_contents('template.blade.php', $blade->view);
//Note if I also need to pass data to the view I can also pass it like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
// return view(template, $data);
return view('template');
While again in my own case for added security, I created base templates with the blade templating scheme & injected user created inputs into the template after sanitizing the generated input using HTMLPurifier and rendering the view. For example
$view = view('base.template')->render();
//similarly like the above I can load any data into the view like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
//$view = view('base.template', $data)->render();
$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
$with_purified_input = $purifier->purify($user_generated_input);
str_replace('view_variable', $with_purified_input, $view);
return $view;
I realised that I can improve security and caching if I just let them insert the static content only. The only thing I need to change is the main content, so I can just let them set a token where the content is to be placed. As is in the above answer by #huzaib-shafi , I did the following...
//In controller
$content = View::make('final',compact('data'));
$token = "<meta name='_token' content='" . csrf_token() ."'";
$scripts = View::make('final_scripts',compact('data'));
$view = str_replace_first("<%content%>", $content, $templateInDatabase);
$view = str_replace_first("<%token%>", $token, $view);
$view = str_replace_first("<%scripts%>", $scripts, $view);
return $view;
This enforces them to use bootstrap in their template, because I use bootstrap styles in my blade templates, but it is acceptable in my case.
I asked and answered a similar question some days ago. So far as I know, Blade doesn't process view content from database columns. Although you can use compileString() method of View. But you should have a look at the following questions.
Extend Blade Template from Database stored string
Let users create custom blade layouts / store in database
In Laravel 9 you can use blade Facades to render from DB:
use Illuminate\Support\Facades\Blade;
return Blade::render('Your Blade Content {{ $parameter1}}', ['parameter1' => 'Name']);

Passing data from Views laravel 4.2

I do some calculations in the controller, and pass the data to my view like this.
return View::make('fend.clist', compact('detail_'));
From this view controller "fend.clist" I'd like to open another view (blade) along with the 'detail_' variable which basically has some data that I need to show there. I tried POSTing it, but I wasn't able to recieve the data completely.
How else can I pass data from one view to another view?
By using the sessions, first put:
Session::put(['name'=>'value']);
Then get:
Session::get('name');
You must nest sub-view to view and pass data to sub-view.
return View::make('fend.clist')->nest('fend.detail', compact('detail_'));
More info in docs

How does CodeIgniter send information from a Model to a View with the Controller?

When I call a controller and it calls the model, model returns information from my database assigned to something in the controller.
But how does it "send" it to the view for rendering? How for example, when I send $data array to my_view.php. how does it get to that page so that, I am guessing, I can do things like use extract to get my individual variables.
I'm really asking at the php level, how would you send that data (so I can learn). How does that view know what I sent it?
Thanks.
You have to "send" that $data array to the view as the second parameter when you load it.
$data['user'] = array(
'name' => 'Tom Jones',
'gender' => 'male'
);
$this->load->view('blogview', $data);
Then, the contents of the array are accessed within the view by their corresponding key values
<?php echo $user['name']; ?>
Checkout out the docs for more details: http://codeigniter.com/user_guide/general/views.html
The general pattern of all php views is this:
function render_view($__filename, $__data) {
extract($__data);
include $__filename;
}
This is basically how CodeIgniter does it, but it uses a loader to find the view filename and includes output buffering options.

How to get data from controller to view?

I need to pass data from controller to view. I used loop in controller which run more than 1 minute.
for($i=0;$i<$count;$i++){
$peicedata = getdata();
$ar=explode(",",$peicedata);
$data['firstname']=$ar[0];
$data['lastname']=$ar[1];
$data['email']=$ar[2];
$data['website']=$ar[3];
}
above function getdata() take around 10 second to get data back. when data get from getdata() i want to pass that data immidiate to view.
Are you passing the data to a view?
$this->load->view('view', $data);
http://codeigniter.com/user_guide/general/views.html
It seems you use CodeIgniter. You can pass data to template like this:
$this->load->view('show', $data);
show is the template. And please read User Guide for more details.
BTW, if your script runs too slow, you should check your code.

Categories