首页 文章

如何在Laravel 5中调用控制器内的模型函数

提问于
浏览
3

我一直面临着在新的laravel框架版本5中无法在控制器内部使用模型的问题 . 我使用artisan命令 "php artisan make:model Authentication" 创建了模型,并在app文件夹中成功创建了模型,之后我创建了一个其中的小函数测试,我的模型代码看起来像这样 .

<?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Authentication extends Model {

   protected $table="canteens";

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

}

现在我不知道,如何将模型的函数test()调用到我的控制器,任何帮助将不胜感激,提前谢谢 .

7 回答

  • 0

    运行该函数并查看输出的快速而脏的方法是编辑 app\Http\routes.php 并添加:

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

    然后访问您的网站并转到此路径: /authentication/test

    Route :: get()的第一个参数设置路径,第二个参数说明调用该路径时要执行的操作 .

    如果你想进一步,我建议创建一个控制器并用控制器上的方法替换该匿名函数 . 在这种情况下,您可以通过改为添加以下内容来更改 app\Http\Routes.php

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

    然后使用artisan制作一个名为 AuthenticationController 的控制器或创建 app\Http\Controllers\AuthenticationController.php 并进行编辑,如下所示:

    <?php namespace App\Http\Controllers;
    
    use App\Authentication;
    
    class AuthenticationController extends Controller {
        public function test()
        {
            $auth = new Authentication();
            return $auth->test();
        }
    }
    

    再次,您可以通过Laravel网站上的 /authentication/test 查看结果 .

  • 2
    <?php namespace App;
    
     use Illuminate\Database\Eloquent\Model;
    
     class Authentication extends Model {
    
       protected $table="canteens";
    
       public function scopeTest(){
        echo "This is a test function";
       }
    
    }
    

    只需在 test() 前加上 scope . 这将成为 scopeTest() .

    现在你可以从 Authentication::Test() 之类的任何地方调用它 .

  • 2

    您可以在控制器中调用模型功能

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

    方法名称前使用范围

    <?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();
      }
    }
    

    上面的代码我试图访问某些目的来调用左侧菜单的数据库

    我们可以轻松地在Controller中调用它

    <?php 
    
     Mainmenu::Leftmenu();
    
  • 8

    1)首先,确保您的模型位于模型文件夹中

    2)然后假设您有一个名为Property的模型,其中有一个名为returnCountries的方法 .

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

    当然,在你的情况下,用你的Model名称替换Property,如果你的函数名字是returnCountries,那就是Test

    在模型中,您编写了请求国家/地区的功能

    所以在你的模型中,放置一个:

    <?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";
       }
    
    }
    

    这就是你的控制器将获得的

  • 2

    对我来说,修复是将函数设置为静态:

    public static function test() {..}
    

    然后直接在控制器中调用它:

    Authentication::test()
    
  • 0

    您应该在控制器功能中创建模型的对象,然后您可以在控制器内对函数进行建模:

    在模型中:

    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.
       }
    
    }
    

    在控制器中:

    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();
     }
    
    }
    

    输出:

    This is a test function
    

相关问题