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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32806287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:12:20  来源:igfitidea点击:

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

phplaravel-5

提问by Srinivasulu Rao

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.

我一直面临无法在新的 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";
   }

}

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.

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

回答by morgaN Star

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

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

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

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

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

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

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.phpby instead adding:

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

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

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

然后使用 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();
    }
}

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

同样,您可以通过访问/authentication/testLaravel 站点来查看结果。

回答by Imanullah

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

回答by MKJ

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

只是前缀test()scope。这将成为scopeTest().

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

现在你可以从任何地方调用它,比如Authentication::Test().

回答by atul_systematix

You can call your model function in controller like

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

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

回答by Jood jindy

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

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

回答by Boris

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

回答by Gufran Hasan

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

回答by patricio

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

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

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

2) 然后假设您有一个名为 Property 的模型,其中有一个名为 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

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

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

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