My Laravel code is something like this...
Cheque::distinct()->get(['cheque_no'])->paginate('25');
If possible I want to add sortByDesc('updated_at') also.
Please help me.
First off, I think you want
distinct('cheque_no')->get()
instead of
distinct()->get(['cheque_no'])
Then, either you use get or paginate
And you define the sorting at the end
So, the altogether ends up like this:
Cheque::distinct('cheque_no')->paginate(25)->sortByDesc('updated_at');
Related
I want to use transactions in my controller, but when I typed in DB::transaction(), its not recognized by it, also the autocomplete doesn't show transaction() as a function inside DB. I'm new to laravel, any help would be appreciated.
Try using like this..
\DB::transaction();
I know this is a really simple thing that I really should know but I'm trying to learn cakephp without having done much php before. I've been told thats a stupid idea, but I'm doing it for fun and so I'm doing it.
I want to pass an array from one controller action to another controllers action and then pass it to the view. I have:
sponges_controller.php
$info = $this->data;
$this->redirect(array('controller'=>'baths', 'action'=>'dashboard', $info));
baths_controller.php
function dashboard($info) {
$this->set('info', $info);
}
and then
<?php echo debug($info); ?>
in the view for dashboard.
I've tried various ways but can't make it work. All it does is print out Array()
Plz help me! :) Julia
You can't pass data that way from one controller to the other as far as I know, at most you can concat a string to the action, like an ID for view or editing.
If you want to pass the info you could try setting it in the SESSION variable in the following way:
$this->Session->write('Info', $info);
And in your other controller you can check for it:
$this->Session->read('Info');
It looks like cake will not let you pass an array into a controller action. I set up a simple example and I got an 'array to string conversion error'. Is there a specific reason why you aren't just posting the data to baths/dashboard? I can think of a workaround for your problem, but it is quite messy.
8vius's solution above will definitely work.
Here is another way, but using sessions is probably a lot better
$str = http_build_query($info);
$this->redirect('/baths/dashboard?'.$str);
So then in your baths/dashboard action, you will have access to your data using the php $_GET array.
So if you originally had this->data['name'] you can access it with $_GET['name']
I'm not sure about the passing data in different controllers but within the same controller we can do it just like a function call by writing something like this.
$this->function_name($info);
This will perfectly work as intended. I've not tried this type of data passing in different controllers function.
How can I create something like: $this->layout()->sidebar, I'm trying for about 4 hours...but it doesn't work for me!Can you give me an example?
I'm still not certain what you are trying to do - but maybe this code that I'm using would help.
// setup the info column into the placeholder
$this->placeholder('rightcol')
->set($this->render('index/view_infoCol.phtml'));
// later in the layout template
echo $this->placeholder('rightcol');
use setResponseSegment('sidebar') in you controller to make $this->layout()->sidebar work...
You could use this:
In your controller:
$this->_response->insert('sidebar', $this->view->render('sidebar.phtml'));
In your layout:
<?=$this->layout()->sidebar;?>
When I use CakePHP Paging I get an url like this:
http://example.com/php/page:2
What do I have to change in the controller, the view and the routes.php to create a working url like this:
http://example.com/php/2
Oh yes, now I see your question. Well you could do something like:
function index($page){
$this->paginate = array('page'=>$page);
$this->set('stuff', $this->paginate('YourControllerName'));
}
See here for more details:
http://bakery.cakephp.org/articles/view/basic-pagination-overview-3
Also, of course you should do some validation that the page is an actual number and that the page would even exist but that is the basics of it i think.
About the routes and views, I have never tried but have a look at these posts on the cake groups, I think they have a problem similar to yours.
http://www.mail-archive.com/cake-php#googlegroups.com/msg45878.html
Try this link:
http://www.sakic.net/blog/changing-cakephp-pagination-urls/
My guess is that this won't be easy to automate, you'll definitely need to do some tweaking.
For starters, you'll probably have to create your own paginator helper and inherit the default one. By the looks of the code, you'll need to override the link-generating code in PaginatorHelper::__pagingLink(), but probably numbers() and prev() etc.. since they all create links with the page param.
Maybe a better way would be to override your AppHelper::url(), check for the "page" param there and modify the url to accomodate your needs.
But, I haven't tried all this, so no guarantees..
I'm total newbie on Yii. Professor basically asks us to make school project having shown us three things to do in Yii.
Let's observe two classes I have, their models being: StudProg and NivoStudija.
What I want is to pass attribute 'naziv' from nivoStudija/admin to studProg/admin, because when I click on a particular item nivoStudija/admin, studProg/admin is shown and I want to use this variable there. So I pass argument like this in one of my CGridView widget's items:
CHtml::link($data->naziv, array("studProg/admin", "nivo_naziv" => $data->naziv))
It opens up studProg/admin and I see URL like this:
http://localhost/pmf/index.php?r=studProg/admin&nivo_naziv=Osnovne+studije
My problem is: How do I get this nivo_naziv thing to use it in studProg/admin ?
Thanks in advance.
For Yii1 you need the equivalent code
$my_nivo_naziv = Yii::app()->request->getQuery('nivo_naziv);
I'm assuming that you are using Yii2.
Then you can get the URL parameter with:
Yii::$app->getRequest()->getQueryParam('nivo_naziv');
Try this for Yii1:
Yii::app()->getRequest()->getParam('nivo_naziv');
In Yii if you want to access get and post parameter, you can use getParam function like this.
Yii::app()->request->getParam('nivo_naziv);
http://www.yiiframework.com/doc/api/1.1/CHttpRequest