繁体   English   中英

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

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

我一直面临无法在新的 Laravel 框架版本 5 中使用控制器内部模型的问题。我使用工匠命令“php artisan make:model Authentication”创建了模型,并在应用程序文件夹中成功创建了模型,之后我在其中创建了一个小功能测试,我的模型代码如下所示。

 <?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() 调用到我的控制器,任何帮助将不胜感激,在此先感谢。

运行该函数并查看输出的一种快速而肮脏的方法是编辑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来查看结果。

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

在方法名称之前使用范围

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

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

比我们可以在控制器中轻松调用它

<?php 

 Mainmenu::Leftmenu();

您可以在控制器中调用模型函数,例如

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

简单地你可以把它设为 static public static function test(){ .... } 然后你可以像这样调用它 Authentication::test();

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

public static function test() {..}

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

Authentication::test()

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

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

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

当然,在您的情况下,将 Property 替换为您的模型名称,如果您的函数(即 Test)则使用名称 returnCountries

并在模型中编写请求国家/地区的函数

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

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

}

这就是您的控制器将得到的

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

在模型中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM