douwu3763 2019-02-16 09: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 12: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部