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

如何在建立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条)

相关推荐 更多相似问题