douwu3763 2019-02-16 17:44
浏览 156
已采纳

如何在建立Eloquent关系时修复静态方法错误

I have two tables queries and query_feedback_types, the query table has one foreign key as query_feedback_type_id, I have created two models for respective tables as Query.php and QueryFeedbackType.php these are both inside my App\Admin folder.

So my problem is that when I try to make an Eloquent relationship between these two tables and returning all data from Queries table with the help of model Query and then I want to have data of query_feedback_types table also, but I am unable to access it via $row->queryFeedbackType->query, this is giving me an error as

"Cannot make static method Illuminate\Database\Eloquent\Model::query() non static in class App\Admin\QueryFeedbackType"

I have already created a similar relationship but the table name in the database was very simple for it , faqs and categories with the foreign key in faqs as category_id.

and that relationship worked perfectly

Model Query.php

namespace App\Admin;
use DB;
use Illuminate\Database\Eloquent\Model;

class Query extends Model
{
  public function queryFeedbackType()
{
    return $this->belongsTo('App\Admin\QueryFeedbackType');
}

protected $fillable= 
['name','email_id','mobile_no','query_feedback_type_id','remark'];
}

Model QueryFeedbackType

<?php

namespace App\Admin;
use DB;
use Illuminate\Database\Eloquent\Model;

class QueryFeedbackType extends Model
{
    public function query()
{
    return $this->hasMany('App\Admin\Query');
}
}

Controller QueryController.php

use DB;
use validator;
use File;
use App\Http\Controllers\Admin\Resize_Image;
use Helper;

class QueryController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $data['page'] = 'View Feedback/Query';
    $data['template'] = 'admin/query/view';
    $data['results'] = Query::orderBy('id', 'desc')->get();
    return view('admin/includes/page', compact('data'));
}
}

my coding page

<?php if ($data['results']){
          $i=1;
          foreach ($data['results'] as $row) {
            ?>

            <tr>
              {{$row->query_feedback_type}}
              <td><center><?php echo $row->name; ?></center></td>

              <td><center><?php echo $row->queryFeedbackType->id ?> //getting error in this line

Error:

"Cannot make static method Illuminate\Database\Eloquent\Model::query() non static in class App\Admin\QueryFeedbackType"

Please explain to me the mistake I am doing here.

  • 写回答

2条回答 默认 最新

  • dpxnrx11199 2019-02-16 20:10
    关注

    The model is extending \Illuminate\Database\Eloquent\Model which already contains a static query function :

    /**
    * Begin querying the model.
    *
    * @return \Illuminate\Database\Eloquent\Builder
    */
    public static function query()
    {
        return (new static)->newQuery();
    }
    

    Fo every eloquent query under the hood, ORM will try to call this query method statically even if you don't do it. It happens internally to instantiate the query builder for your model. As in your model, you are having a method with same name query which is a non-static method. This is the reason why you are getting this error.

    I think in your case query() function is nothing but a simple relation. I would suggest to rename it to something like queries as it's hasMany. It will also help you remove the overlapping of function name to default static function query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler