-1

I need to use a Model function in a controller but get the above error:

Non-static method App\Models\Employee::getEmployeeName() should not be called statically, assuming $this from incompatible context

My Model:

 <?php namespace App\Models;


  use Illuminate\Database\Eloquent\Model;



class Employee extends Model 
{



protected $table = ‘BLABLA’;


    public function getEmployeeName()

        {
            if ($this->EmployeeName){

                return "{$this->EmployeeName}";
                }

        return null;

        }

}

My controller:

use Auth;

use DB;

use App\Models\Bookings;

use App\Models\User;

use App\Models\Employee;

use Illuminate\Http\Request;

class BookingsController extends Controller {

    public function postBooking(Request $request){

     $employee=Employee::getEmployeeName()->get();

     dd($employee);

           }



     }
Screwtape007
  • 185
  • 1
  • 2
  • 16
  • 1
    Possible duplicate of [Error message Strict standards: Non-static method should not be called statically in php](https://stackoverflow.com/questions/4684454/error-message-strict-standards-non-static-method-should-not-be-called-staticall) – Sven Hakvoort Mar 05 '19 at 19:25
  • 1
    An employee has a name. The employee **class** does not. You'd access it something like `$employee->getEmployeeName()`. Your static function doesn't have a `->get()` function at all, so you'll have to nix that. Chances are `name` will be a parameter from the database, so you shouldn't need *any* of this code - `$employee->name` should likely suffice just fine. You should re-read the Laravel docs on Eloquent. – ceejayoz Mar 05 '19 at 19:27
  • 1
    You con do it for example `$employee=Employee::first()->getEmployeeName();` – Davit Zeynalyan Mar 05 '19 at 19:30
  • Thank you very much Davit, worked like a charm! – Screwtape007 Mar 05 '19 at 19:36
  • 1
    @Screwtape007 Please do not use `first()`. It will only get the very first employee in the database. Please see the answers below and also take @ceejayoz 's advice and read the Laravel docs on Eloquent. – Matt K Mar 05 '19 at 19:43

4 Answers4

1

getEmployeeName is a method of an Employee object. It must be called on an instance of an Employee. In your case, you would have to get an instance of an Employee before calling that method. Perhaps by an employee_id passed in with the $request.

public function postBooking(Request $request) {

     $employee = Employee::findOrFail($request->input('employee_id'));

     dd($employee);

}

Furthermore, getEmployeeName is not needed at this point since you already have the Employee object. To get the name all that's required is to call the attribute:

$employee->name OR $employee->EmployeeName (whatever you named it)

Matt K
  • 6,620
  • 3
  • 38
  • 60
0

try this : in your model

 public static function getEmployeeName()

        {
            if ($this->EmployeeName){

                return "{$this->EmployeeName}";
                }

        return null;

        }

in your controller :

 $employee=Employee::getEmployeeName();

but if EmployeeName is an attribute better do this :

public function getEmployeeNameAttribute()
{
    if($this->EmployeeName){return $this->EmployeeName;}
}

and in your controller :

$employees= Employee::find(x);
$employees->employee_name ;

$this have value when your model contain a records set in both solution

i hope help you

Mohammad
  • 652
  • 1
  • 7
  • 18
0

This worked

  $employee=Employee::first()->getEmployeeName();
Screwtape007
  • 185
  • 1
  • 2
  • 16
  • This is not works for the question. This will works with only the first record. For the rest of the records it will not give us a relevant data. – Mano Mahe Feb 06 '23 at 06:43
0

It means define function statically, add static after public , below an example

Like This

public static function postBooking(Request $request){

 $employee=Employee::getEmployeeName()->get();

 dd($employee);

       }
Siraj Ali
  • 526
  • 6
  • 13