简体   繁体   English

如何在 laravel 中的 where eloquent 中使用“get”方法

[英]How to use 'get' method with the where eloquent in laravel

I was trying to get all the elements sharing a common row data but I keep getting this error I do not know why我试图让所有元素共享一个共同的行数据,但我不断收到这个错误,我不知道为什么

Call to a member function get() on null在 null 上调用成员 function get()

Here is the code I used, I have imported the App\User and everything but still not getting it and I have a user in my table with that role_id这是我使用的代码,我已经导入了 App\User 和所有内容,但仍然没有得到它,并且我的表中有一个具有该 role_id 的用户

My controller我的 controller

<?php

namespace App\Http\Controllers;

use App\Animal;
use App\Clinic;
use App\Role;
use App\Slaughter;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class ClinicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()

    {

        $farms = User::where('role_id', 3);
        $user = Auth::user();
        $animal = Animal::all();
        return view('clinic.index', compact('user', 'animal', 'farms'));
    }

My user table我的用户表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->unsignedBigInteger('address_id')->index();
            $table->unsignedInteger('role_id');
            $table->string('description')->nullable();
            $table->timestamps();

            $table->foreign('address_id')->references('id')->on('addresses');
        });
    }

But I am keep getting that error bellow I do not know the why但是我不断收到下面的错误,我不知道为什么

use App/User;

$farm = User::where('role_id', 3)->get();

Try below code for getting the record尝试下面的代码来获取记录

$farm = User::select(['id','name'])->where('role_id', 3)->get();

In case the User class extends Model如果用户 class 扩展 Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model{}

calling打电话

$builder = User::where('role_id', 3);

always returns an object of type Illuminate\Database\Eloquent\Builder, therefore, calling总是返回一个 Illuminate\Database\Eloquent\Builder 类型的 object,因此,调用

$user= User::where('role_id', 3)->get();

always returns a collection (which might be empty when there is no user with role_id = 3).总是返回一个集合(当没有 role_id = 3 的用户时,它可能为空)。 Check the type of your User class.检查您的用户 class 的类型。

I think there are some missed steps that you didn't take.我认为您没有采取一些错过的步骤。

The User does not refer to the table name but to the model class name. User不是指表名,而是指 model class 名称。 So first things first you need to create a model.所以首先你需要创建一个 model。

In order to do that you use the command:为此,您使用以下命令:

php artisan make:model User

If you want a dedicated Models folder(i always use one) you use如果你想要一个专用的模型文件夹(我总是使用一个),你可以使用

php artisan make:model Models/User

By that time you should have a model created but you need to set it up.到那时,您应该已经创建了 model,但您需要对其进行设置。

Going inside your User.php file you should have something like this:进入你的User.php文件你应该有这样的东西:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
 /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are NOT mass assignable.
     *
     * @var array
     */
    protected $guarded = [
        'created_at', 'updated_at', 'password,
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        //
    ];
}

Now by including your model at the top of your file you can use your Model class.现在,通过在文件顶部包含 model,您可以使用 Model class。

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

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