Srinivasulu Rao
Srinivasulu Rao

Reputation: 309

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command "php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.

 <?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Authentication extends Model {

   protected $table="canteens";

   public function test(){
    echo "This is a test function";
   }

}

Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.

Upvotes: 16

Views: 127341

Answers (8)

Jood jindy
Jood jindy

Reputation: 480

simply you can make it static public static function test(){ .... } then you can call it like that Authentication::test();

Upvotes: 2

Gufran Hasan
Gufran Hasan

Reputation: 9373

You should create an object of the model in your controller function then you can model functions inside your controller as:

In Model:

namespace App;
use Illuminate\Database\Eloquent\Model;
 class Authentication extends Model {
   protected $table="canteens";

   public function test(){
    return "This is a test function"; // you should return response of model function not echo on function calling.
   }

}

In Controller:

namespace App\Http\Controllers;
class TestController extends Controller
{
    // this variable is used to store authenticationModel object
    protected $authenticationModel;
public function __construct(Request $request)
  {
     parent::__construct($request);     
     $this->authenticationModel= new \App\Authentication();
  }
 public function demo(){
  echo $this->authenticationModel->test();
 }

}

Output:

This is a test function

Upvotes: 0

Boris
Boris

Reputation: 105

For me the fix was to set the function as static:

public static function test() {..}

And then call it in the controller directly:

Authentication::test()

Upvotes: 3

IMANULLAH
IMANULLAH

Reputation: 579

Use scope before method name

<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;

use Illuminate\Database\Eloquent\Model;

class Mainmenu extends Model
{

  public function scopeLeftmenu() {

    return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
  }
}

above code i tried to access certain purpose to call databse of left menu

than we can easy call it in Controller

<?php 

 Mainmenu::Leftmenu();

Upvotes: 9

atul_systematix
atul_systematix

Reputation: 253

You can call your model function in controller like

$value = Authentication::test();
var_dump($value);

Upvotes: 2

Khuram
Khuram

Reputation: 1850

<?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Authentication extends Model {

   protected $table="canteens";

   public function scopeTest(){
    echo "This is a test function";
   }

}

Just prefix test() with scope. This will become scopeTest().

Now you can call it from anywhere like Authentication::Test().

Upvotes: 6

patricio
patricio

Reputation: 350

1) First, make sure your Model is inside a Models Folder

2) Then supposing you have a model called Property inside which you have a method called returnCountries.

public function returnCountries(){
$countries = Property::returnCountries(); 
}

of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test

and in the Model you write that function requesting the countries

so in your Model, place a:

 <?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Authentication extends Model {

   protected $table="canteens";

   public function test(){
    return $test = "This is a test function";
   }

}

and this is what your Controller will be getting

Upvotes: 0

morgaN Star
morgaN Star

Reputation: 356

A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:

use App\Authentication;

Route::get('authentication/test', function(){
    $auth = new Authentication();
    return $auth->test();
});

Then visit your site and go to this path: /authentication/test

The first argument to Route::get() sets the path and the second argument says what to do when that path is called.

If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:

Route::get('authentication/test', 'AuthenticationController@test');

And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:

<?php namespace App\Http\Controllers;

use App\Authentication;

class AuthenticationController extends Controller {
    public function test()
    {
        $auth = new Authentication();
        return $auth->test();
    }
}

Again, you can see the results by going to /authentication/test on your Laravel site.

Upvotes: 24

Related Questions