Maatwebsite/Laravel-Excel exports blank excel when using Input::get() in model - laravel

I developed a Laravel application that connects to a external MySQL database for reports. I'm using Maatwebsite/Laravel-Excel 3.1 to be able to export to excel.
In this app, I have a method in my model that uses Input::get() for 3 variables ($from, $to, $paymentType). This method is called whenever the user chooses date range and payment type as filter for their reports. Everything works as it should since data is displayed in my view.
Now this same method is called when the user chooses to export the file to excel. Again, all that works except the files is blank.
The curious thing is that if I replace all Input::get() for static values such as '2018-11-14' for the dates and 'n' for payment type, then the file exports with data.
I've been struggling with this for a couple of days so I hope someone can help me.
Thanks,
Ernesto

First, towards the top of the controller file, add:
use Illuminate\Http\Request;
Then, you can access the fields by:
$request->input('from');
$request->input('to');
Assumption: from and to are correct field names.
You can also use request()->input('from') etc. directly (note the absence of $).
Please see this for more info: https://laravel.com/docs/5.6/requests

If the parameters are obtained from the request you should use $request->input() instead of Input::get().
If used in a function where $request is not available you could use request('variable') to get the variable value from the request.

Related

CRUD with localization in laravel 5?

I want to create, read, update and destroy data with localization in laravel 5. please help me some suggestions thanks!
Localization comes build in, check out the official docs:
https://laravel.com/docs/5.3/localization
Honestly, for inserting and updating data, I don't think you can choose to localise your data: you have to insert ALL contents of ALL languages because you are saying you support these languages, right?
But for reading and displaying data, you can
1) Define columns with suffix. For example in post table you have columns called body_en, body_cn, body_jp etc.
2) When users choose to change languages, use a controller to respond to that action, save a language suffix like '_en' into a cookie, maybe called 'cookie_language'.
3) When users navigate in your site, for every request, use a middleware check whether there is a language cookie, get its value, save it into session, and call App::setLocale() accordingly.
4) When you retrieve data from post table, get the language value out of session, use it as suffix, like Post::select(['body'.$suffix])->get(). Localised data got!
5) Because you have set locale, now you can use trans() in all your views. View translated!
Hope this helps!

Laravel - Model binding not working when I use accessors

I want to populate my select box from the Laravel IoC Container.
App\Http\Utilities\BillingHelper.php
views/billing/create.blade.php
views/billing/edit.blade.php
Create the table:
Now, instead of the value, i want to display some flags and currency symbols.
Should i use mutators?
Problem
If i use mutators, when i open the edit page, i see always the first value selected, from the BillingHelper, instead of the choosen one.
Any help? Thanks.
I know it should be a comment, but I have no reputation.
What if, on you edit page, you replace null on Form::select with $client->language and $client->currency.
I know that you area binding the values with Form::Model. But worth a try.
When you use mutators the matching won't occur anymore. You'll have to use a matching static array according to the values you'll return. (the flags)
You can make it work if you make a mutator for saving the data also and simplify again to the ['it', 'en', 'lv'], otherwise your saved data will differ and the initial mutator won't work the second time. You can still make a one-time-only test.
This is why:
Your form binding is using $bill->language to retrieve the actual stored data, and compare it with the values in your $bill::lang static array. If no match found, than the first value will be always selected.
Can you provide the the currency and language fields definition in the migration for the bill?
Also retrive your values from your bills DB and paste them here for language and currency. They must be in the defined static sets.
Laravel has a way of skipping the accessor/mutator by using
$model->getOriginal('data_field').
The "getOriginal()" gets the model's original attribute values.

Adding attribute to a user when register in Laravel 5.1

When a user Register, I want to add location data for the user.
I get those with GeoIP.
So, each time a user is created, I would like somewhere to add Country, City, etc.
I was thinking about setting Hidden fields in view, but I think it a hugly way to do it, and I'm sure there is a better way to do...
Any Idea???
Any time I create a record that needs extra data, involves inserting additional records into additional tables, etc, I create a service class. Something like "UserCreator" then I pass it the input, do any additional operations, wrap multiple database calls in a transaction and so on.
That said there are so many ways to do what you want. You could Input::merge(...) then save, you could separate the process of creating a user from your controller / route function, etc.
If you are just getting started with Laravel and/or your project is rather simple, then you probably want to look at Input::merge
I solved it using Request Contructor as said here in laracast
In the Form Request's constructor I inject \Illuminate\Http\Request. I can then add my value to the request instance as such:
public function __construct(\Illuminate\Http\Request $request)
{
$request->request->add(['date_of_birth' => implode('-', $request->only('year', 'month', 'day'))]);
}

How to pass route values to controllers in Laravel 4?

I am struggling to understand something that I am sure one of you will be able to easily explain. I am somewhat new to MVC so please bear with me.
I have created a controller that handles all of the work involved with connecting to the Twitter API and processing the returned JSON into HTML.
Route::get('/about', 'TwitterController#getTweets');
I then use:
return View::make('templates.about', array('twitter_html' => $twitter_html ))
Within my controller to pass the generated HTML to my view and everything works well.
My issue is that I have multiple pages that I use to display a different Twitter user's tweets on each page. What I would like to do is pass my controller an array of values (twitter handles) which it would then use in the API call. What I do not want to have to do is have a different Controller for each user group. If I set $twitter_user_ids within my Controller I can use that array to pull the tweets, but I want to set the array and pass it into the Controller somehow. I would think there would be something like
Route::get('/about', 'TwitterController#getTweets('twitter_id')');
But that last doesn't work.
I believe that my issue is related to variable scope somehow, but I could be way off.
Am I going down the wrong track here? How do I pass my Controllers different sets of data to produce different results?
EDIT - More Info
Markus suggested using Route Parameters, but I'm not sure that will work with what I am going for. Here is my specific use case.
I have an about page that will pull my tweets from Twitters API and display them on the page.
I also have a "Tweets" page that will pull the most recent tweets from several developers accounts and display them.
In both cases I have $twitter_user_ids = array() with different values in the array.
The controller that I have built takes that array of usernames and accesses the API and generates HTML which is passed to my view.
Because I am working with an array (the second of which is a large array), I don't think that Route Parameters will work.
Thanks again for the help. I couldn't do it without you all!
First of all, here's a quick tip:
Instead of
return View::make('templates.about', array('twitter_html' => $twitter_html ))
...use
return View::make('templates.about', compact('twitter_html'))
This creates the $twitter_html automatically for you. Check it out in the PHP Manual.
 
Now to your problem:
You did the route part wrong. Try:
Route::get('/about/{twitter_id}', 'TwitterController#getTweets');
This passes the twitter_id param to your getTweets function.
Check out the Laravel Docs: http://laravel.com/docs/routing#route-parameters

way to pass variable through url in codeigniter

I got a big search module in my Codeigniter project. Well simply I am passing variable to a view like
<a href=<?php echo site_url('controller/view/1'); ?>>View List</a>
And fetching its data in controller like
$id=$this->uri->segment(3);
For pagination
http://wwww.site.com/controller/view/<filter id>/<page from>
This is working perfectly in the case of simple query.
Now I got some more filter quires like
Country
State
City
Customer type
etc etc
then the url should be
http://wwww.site.com/controller/view/1/id2/id3/i4/id5
Is this the correct way to do the process ? If not please give a little advice...
I am new to codeigniter
The problem you are facing i have recently found a solution for this.
When you are first sending parameters through url use POST instead.
When you get the parameters you can pass them to session in a variable
type. Next time when you paginate get the type value from session and
put it in your query to get the desired result.
If you have more than 1 parameters you can put them in sessions and
unset them on certain conditions so that they are not called in every query.
I think the best approach here is to create another method in the controller something like filtered_view that accepts a filter_id and a page number, and that methode will fetch the data from the database with the provided filter and you'll use your pagination class as usual.
Hope this help.

Resources