How to call controller function in view page in Laravel 5 - laravel

public function getPendingOrder($id)
{
$pendingOrder = DB::table('erp_purchase_order_details')->where('product_category_id','=',$id)->where('received','=','0')->count();
return $pendingOrder;
}

To access controller methods in your laravel project we can create the object for that controller then we can call method of controller using created object
Suppose i have a controller Product and one method getPendingOrders() method inside it:
class Product extends BaseController
{
public static function getPendingOrders($id)
{
$pendingOrder = DB::table('erp_purchase_order_details')- >where('product_category_id','=',$id)->where('received','=','0')->count();
return $pendingOrder;
}
}
So in your view you can call this method using ::
For example
{{ Product::getPendingOrders(10); }}
Another method is to create object of that class and then call the method if it is not static.

if you want to extra functionally to your laravel project use laravel widget package its work like wordpress shortcodes and you can call them as laravel blade tags
Please read the document carefully
http://sky.pingpong-labs.com/docs/2.0/widget

Related

Pass object in all methods of all controllers in Laravel

I have this code in the HomeController#index:
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
Is there any way I can tell Laravel to execute that lines of code before the end of methods and controllers I define without me copying and pasting those lines of code in every method?
You don't need to do that, you can just share this data with all views by using the view()->share() method in a service provider:
view()->share('towns', Town::all());
You can also use a view composer for that:
public function compose(View $view)
{
$view->with('towns', Town::all());
}
You can extend all controllers from your basic controller. Use Controller.php on app/Http/Controllers/controller.php or create new one.
Add myThreeLines to base controller.
controller.php:
function myThreeLines(){
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
}
class TestController extend Controller{
function index(){
return $this->myThreeLines();
}
}

Create route for every controller of index method in Laravel

I'm developing a school management system in laravel. I have many controllers like
controller staff in method index
class controllerstaff extends controller {
public function index{
//here process of staff data
}
}
//this controller have `Route::get('/', 'controllerstaff#index');
and other controller
class controllerstudent extends controller {
public function index{
//here process of student data
}
}
//this controller have Route::get('/', 'controllerstudent#index');
As above does not work properly.
Any one can tell me how to create route for every controller of index method. If we crate many route file then how operate it and how access in controller and form action
You cannot create same urls for each route. For each route you need to have different url, for example:
Route::get('/staff', 'controllerstaff#index');
Route::get('/students', 'controllerstudent#index');
You should also name your controllers rather StudentController and not controllerstudent. You might also consider looking at Routing documentation before creating code - I believe it might be the right way ;)

How to Call a controller function in another Controller in Laravel 5

im using laravel 5.
I need to call a controller function but this should be done in another controller.
I dont know how to do this
public function examplefunction(){
//stuff
}
And i have a Route for this function, so at
public function otherfunctioninothercontroller(){
// I need examplefunction here
}
how Can i do this?
1) First way
use App\Http\Controllers\OtherController;
class TestController extends Controller
{
public function index()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
2) Second way
app('App\Http\Controllers\OtherController')->method();
Both way you can get another controller function.
If they are not in the same folder, place use namespace\to\ExampleClass; on top of your file, then you are able to instantiate your controller.
You can simply instantiate the controller and call the desired method as follows
FirstController.php:
namespace App\Http\Controllers;
class FirstController extends Controller {
public function examplefunction() {
// TODO: implement functionality
}
}
SecondController.php:
namespace App\Http\Controllers;
class SecondController extends Controller {
public function test() {
$object = new FirstController();
$object->examplefunction();
}
}
Now, after i've answered the question, i would like to add the following comment:
Controllers are classes, all rules that applies to normal classes can be applied to them
However, instantiating a controller directly inside another controller to call a desired method signifies a problem in your design for the following 2 reasons:
A controller cannot obtain an instance of another controller directly
Controller should contain as little business logic as possible, and if possible none
The closest possible solution to what you want (WITHOUT BREAKING MVC) is to make an HTTP request to the route that points to the desired method (using cURL, for example) and read the response as the returned data
But this still doesn't make much sense in this scenario because after all you're making an HTTP request from a method in a controller in your project on your server to a method in a controller in your project on your server, seems like unnecessary overhead, right ?
As i said earlier, a controller should contain as little business logic as possible because the logic should stay inside specialized classes (commonly known as Service Classes), and when a processing is requested the controller simply delegates the job of processing to the appropriate service class which does the processing and returns the results to the controller which in turn sends it back as a response
Now imagine if you've the following scenario:
We've got an application that consists of 3 functionalities:
A user can register an account from web application
There's a mobile application that talks to an API to register a user
There's an admin panel, which he can use to add new user
Obviously you need to create 3 controllers, but those controllers contains repeated logic, would you copy/paste the code everywhere ?
Why not encapsulate this logic inside a service class and call it from the controller when needed ?
Let's say I have Controller1 and Controller2. I want to call a function of Controller1 from inside a function placed in Controller2.
// Controller1.php
class Controller1 {
public static function f1()
{
}
}
And on the other controller:
// Controller2.php
use App\Http\Controllers\Controller1;
class Controller2 {
public function f2()
{
return Controller1::f1();
}
}
Points to be noted:
f1() is declared static
A call to a controller from inside another controller is a bad idea. There is no sense of meaning of controllers then. You should just redirect to web.php to save safe whole architecture like this:
class MyController {
public function aSwitchCaseFunction(Request $requestPrm){
...
//getting path string from request here
...
switch($myCase){
case CASE_1:
return redirect()->route('/a/route/path');
....
}
}
}

Laravel Use a method from BaseController in a view

As I need to get for every page a site configurations variables from a table of my database called 'site_configuration', I use a method in my baseController :
In the __constructor i have
$this->config = SiteParameter::first();
and I have a public method to retrieve a variable :
public function getSiteParameter($variable)
{
return $this->config->$variable;
}
If I do $this->getSiteParameter('sitename') it works. I'd like to do the same thing in a view. but without passing values to the view. I'd be happy if it was automatic.
Use the controller __construct to share the config data with the view. E.g.:
public function __construct() {
View::share('config', $this->config);
}
Just use the Config class, as explained in the configuration documentation. To put it short, assuming you created a customer configuration file app/config/site.php:
<h1><?= Config::get('site.sitename') ?></h1>

Call a controller from a method

Can we call a controller and parsing any values or data to the controller from a method?
let's say that i have this method,
function loader(){
//some operations to call another controller
}
and from that method i want to call a controller named welcome.php wich is located in /application/controller
i'v tried this but it doesn't work
function loader(){
$open = new Welcome();
}
it says that Class Welcome not found
Sorry for my bad english
At first You have to include the file
include('welcome.php');
Then, create the object.
function loader(){
$open = new welcome();
//if you want to call a method in an object
$open->MyWelcomeMethod();
}
Makesure that your loader controller was extended to welcome controller.
Suppose controller welcome,my_controller are two controller and loader function in B then
class Welcome extends CI_Controller {
function my_fun() {}
}
then you can call my_fun() when you are entended from my_controller like
class My_Controller extends Welcome {
$open = $this->my_fun();
}

Resources