簡體   English   中英

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

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

我試圖讓所有元素共享一個共同的行數據,但我不斷收到這個錯誤,我不知道為什么

在 null 上調用成員 function get()

這是我使用的代碼,我已經導入了 App\User 和所有內容,但仍然沒有得到它,並且我的表中有一個具有該 role_id 的用戶

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

我的用戶表

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

但是我不斷收到下面的錯誤,我不知道為什么

use App/User;

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

嘗試下面的代碼來獲取記錄

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

如果用戶 class 擴展 Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model{}

打電話

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

總是返回一個 Illuminate\Database\Eloquent\Builder 類型的 object,因此,調用

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

總是返回一個集合(當沒有 role_id = 3 的用戶時,它可能為空)。 檢查您的用戶 class 的類型。

我認為您沒有采取一些錯過的步驟。

User不是指表名,而是指 model class 名稱。 所以首先你需要創建一個 model。

為此,您使用以下命令:

php artisan make:model User

如果你想要一個專用的模型文件夾(我總是使用一個),你可以使用

php artisan make:model Models/User

到那時,您應該已經創建了 model,但您需要對其進行設置。

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

現在,通過在文件頂部包含 model,您可以使用 Model class。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM