【问题标题】:How to use 'get' method with the where eloquent in laravel如何在 laravel 中使用 'get' 方法和 where eloquent
【发布时间】:2019-10-30 12:14:50
【问题描述】:

我试图让所有元素共享一个共同的行数据 但是我一直收到这个错误我不知道为什么

在 null 上调用成员函数 get()

这是我使用的代码,我已经导入了 App\User 和所有内容,但仍然没有得到它,并且我的表中有一个具有该 role_id 的用户

我的控制器

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

我的用户表

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

但我不断收到下面的错误,我不知道为什么

【问题讨论】:

  • 我的数据库没有问题
  • 请输入您的控制器功能代码。
  • 您确定在您的命名空间中定义了User 吗?你的语法是正确的。
  • @EyadJaabo - 如果没有role_id,它会给出一个SQLException

标签: php laravel


【解决方案1】:
use App/User;

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

【讨论】:

    【解决方案2】:

    试试下面的代码获取记录

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

    【讨论】:

    • 我已经整理好了
    • 很好,你能解释一下如何整理吗?
    【解决方案3】:

    如果用户类扩展模型

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class User extends Model{}
    

    打电话

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

    总是返回 Illuminate\Database\Eloquent\Builder 类型的对象,因此,调用

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

    总是返回一个集合(如果没有 role_id = 3 的用户,该集合可能为空)。检查您的用户类的类型。

    【讨论】:

    • 但对我来说,我有具有该 rolr_id 的用户
    • 您发布为“我的用户表”的代码是迁移。它用于在数据库中创建表。您需要创建一个模型:laravel.com/docs/5.8/eloquent#defining-models
    • 我也有模特
    【解决方案4】:

    我认为你没有采取一些遗漏的步骤。

    User 不是指表名,而是指模型类名。所以首先你需要创建一个模型。

    为此,您使用以下命令:

    php artisan make:model User

    如果你想要一个专用的模型文件夹(我总是使用一个),你可以使用

    php artisan make:model Models/User

    到那时您应该已经创建了一个模型,但您需要对其进行设置。

    进入你的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 = [
            //
        ];
    }
    

    现在通过在文件顶部包含您的模型,您可以使用您的模型类。

    【讨论】:

      猜你喜欢
      • 2015-10-02
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多