How to call a controller from another controller in codeigniter? - codeigniter

I want to load a controller function from another controller function using codeigniter. What is the suitable way to do this so when call it url should be changed also.

No You cant do it.
What you have to do it is create that function in model and call it through your controllers. So it will work fine.
Ex
In Model
function get_id()
{
//some argument
}
In controller 1
$this->Model_name->get_id()
In controller 2
$this->Model_name->get_id()

yes you can (for version 2)
load like this below inside your controller
$this->load->library('../controllers/whathever');
and call the following method:
$this->whathever->functioname();

Related

laravel 5.8 edit function with model instance

public function edit(EduLevel $eduLevel)
{
dd($eduLevel->name);
return view('adm.edulevel.edit',compact('eduLevel'));
}
Route::resource('edulevel','EduLevelController'); //web.php
with resource route
how to get eduLevel to view with model instance laravel. in previous i call with parme parameter id and use find() method to get data..
from this sample - https://itsolutionstuff.com/post/laravel-58-crud-create-read-update-delete-tutorial-for-beginnersexample.html
I don't understand the question but I will just guess that you have a route that accepts a parameter that you you expect it to be the model inside your function.
You need to create a route like this one:
Route::get('/edit/{eduLevel}', 'SomeController#edit');
Notice the same name for the variable, this is important otherwise you will get only the id, slug or whatever.
Make sure your path name also have the same name for route segment name.
so your route path should be like this.
Route::get('/edit/{variablename}', 'ControllerName#edit');
your controller function logic should be like this.
public function edit(EduLevel $variablename)
{
return view('adm.edulevel.edit',compact('variablename'));
}
So make sure your variable name in route and in controller function
should be same.
For more information, you can read Route Model Binding in laravel
I am having the same problem (almost).
I wanted to call a controller method in the view. So I should pass the model from controller to view.
How to pass model from controller to view?
I found this [Laravel 5 call a model function in a blade view but using ->withModel($model); to pass the model from controller to view and {{$model->someFunction()}} to call the method in the view is not working.
Any advice please?

Creating a route to custom function in controller in Laravel

I still can't understand why I can't point my blade to the custom function I made in my controller. I create a route like this,
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
and in my controller I have this,
public function storeInitialItems()
{
return view('orders.storeInitialItems');
}
but when I run the page, storeInitialItems.blade.php, the error seems calling the show() function of my controller.
Why is that happening?
update
Complete routes for ORDERS
Route::get('/orders','OrdersController#index')->name('orders.index');
Route::get('/orders/create', 'OrdersController#create')->name('orders.create');
Route::post('/orders', 'OrdersController#store')->name('orders.store');
Route::get('/orders/{order}/edit', 'OrdersController#edit')->name('orders.edit');
Route::post('/orders/{order}', 'OrdersController#update')->name('orders.update');
Route::delete('/orders/{order}', 'OrdersController#destroy')->name('orders.delete');
Route::resource('orders', 'OrdersController');
Route::put('orders/{order}/pub', 'OrdersController#publish')->name('orders.publish');
Route::put('orders/{order}/cancel', 'OrdersController#cancel')->name('orders.cancel');
Route::put('orders/{order}/delivered', 'OrdersController#delivered')->name('orders.delivered');
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
Route::get('/orders/{order}/delivery', 'OrdersController#viewdeliveryItems')->name('orders.delivery');
Route::get('/orders/acceptDelivery', 'OrdersController#acceptDelivery')->name('orders.acceptDelivery');
Add your orders.storeInitialItems route
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
before,
Route::resource('orders', 'OrdersController');
or add some extra path with your storeInitialItems
Route::get('/orders/storeInitialItems/add-some-extra-path', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');

how to call a function in codeigniter email library

i am using codeigniter framework. i am calling a function from controller to custome library. i want to call another function in that function.i am calling below calendarupdate() function in emailcom library.Please help me. I am trying like below.
function calendarupdate($info, $users)
{
$this->subject="Calendar Schedule Update";
$this->to='test#gmail.com';
$this->CI->email->from('support#gmail.com','test');
$this->CI->email->to($this->to);
$this->CI->email->subject($this->subject);
$this->CI->email->message('Hi');
$this->CI->email->set_mailtype("html");
if($this->CI->email->send())
{
$this->CI->email->clear(TRUE);
$this->newadmin($info,$newarr,$adminusers);
}}
I need to call newadmin() funtion in Emailcom library only.
If the second method you need to call is also in the emailcomm class, simply use $this->newadmin()

how can i call laravel model method like modelname->save() inside the laravel view

hi i have a project in laravel 5.4 for some function i have calling the laravel model methods inside the view for my convenient so it is showing me error save() method does not exist so anyone help me is that possible to call the laravel model method inside the view or how to achieve this below is my code
below is my blade code
$pi_amount=new App\PI_Amount;
$pi_amount->invoiceNumber=$fd->invoiceNumber;
$pi_amount->total_goods=$total_goods;
$pi_amount->total_cst=$total_tax;
$pi_amount->total_security=$security_amount;
$pi_amount->freight=$freight;
$pi_amount->total_value=$total_value;
$pi_amount->save();
Make sure:
1. That is the name of the model PI_Amount
2. That it is the model on App\PI_Amount
You can import the model it using at the beginning of the php file:
use App\PI_Amount;
Hi i have create a new static function inside the Controller and call the that static method inside the view and define the process code inside the controller function so my problem resolve.
Below is my controller function
public static function processsAmount($insert_data){
$pi_amount=new PI_Amount;
$pi_amount->invoiceNumber=$insert_data['invoiceNumber'];
$pi_amount->total_goods=$insert_data['total_goods'];
$pi_amount->total_cst=$insert_data['total_cst'];
$pi_amount->total_security=$insert_data['total_security'];
$pi_amount->freight=$insert_data['freight'];
$pi_amount->total_value=$insert_data['total_value'];
$pi_amount->save();
}
and below i have make a call of that static function inside the view like
$security_amount=0;
$insert_data=array('invoiceNumber'=>$fd->invoiceNumber,'total_goods'=>$total_goods,'total_cst'=>$total_tax,'freight'=>$freight,'total_value'=>$total_value,'total_security'=>$security_amount);
echo App\Http\Controllers\PiController::processsAmount($insert_data);
so by using the above concept my problem is resolved and we can use this for further.

How to have a dynamic controller in Laravel

I'm trying to call dynamic popup views in which I need to pass the data through the controller, I want the controller to be dynamic which will access the particular function and make the view accordingly. Basically i'm looking for something like this:
Route::post('/popup/{id}', 'PopupController#{$id}');
So basically suppose when it is called like this: mydomain.com/popup/id1, it should call PopupController#id1.
Help me out with this.
You need a method that will fire the appropriate function
Route::post('/popup/{id}', 'PopupController#dispatch');
In PopupController
public function dispatch($id)
{
return $this->$id()
}
then if your $id is someFunction you need to make sure your controller has function someFunction() method
I suggest instead of writing a dynamic route or controller use switch case in controller action.
e.g.
Route::post('/popup/{id}', 'PopupController#action');
In Controller
public function action($id)
{
switch($id)
{
case 1: ...
case 2: ...
}
}

Resources