【问题标题】:BelongsTo relationship in laravelLaravel 中的 BelongsTo 关系
【发布时间】:2019-05-26 03:49:43
【问题描述】:

我正在尝试建立一对多关系,每个客户可以分配到多个条目,这是我的迁移表

客户表:

Schema::create('customers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('idtype');
        $table->string('idnumber');
        $table->string('company');
        $table->timestamps();

这是我的受让人表:

  Schema::create('assignees', function (Blueprint $table) {
            $table->increments('id');
            $table->string('cabinet');
            $table->time('timein');
            $table->time('timeout');
            $table->string('refnumber');
            $table->timestamps();
            $table->integer('customer_id')->unsigned()->index()->nullable();

这是我的受理人控制器,其中所属功能:

  class Assignee extends Model
{
    //
    protected $fillable = [
        'cabinet', 'customer_id','timein','timeout','refnumber',
    ];

    public function cust()
    {
        return $this->belongsTo('App\Customer');
    }
}

这是我的 index.blade.php

  <table class="table table-bordered">
    <tr>
        <th>No</th>
        <th>Entry id:</th>
        <th>Person Name</th>
        <th>Referance No:</th>
        <th>timein</th>
        <th>timeout</th>
        <th width="280px">Action</th>
    </tr>
    @foreach ($assignees as $assignee)
    <tr>
        <td>{{ ++$i }}</td>
        <td>{{ $assignee->id }}</td>
        <td>{{$assignee->customer-name}}</td>
        <td>{{ $assignee->refnumber }}</td>
        <td>{{ $assignee->timein }}</td>
        <td>{{ $assignee->timeout }}</td>

运行页面时出现以下错误:

Use of undefined constant name - assumed 'name' (View: /Users/user/Documents/Laravel/blog/resources/views/assignees/index.blade.php)

在创建“Assignee 时,laravel 不会执行关系检查,

我做错了什么?我应该在迁移文件夹中声明关系还是在模型中声明就足够了?

【问题讨论】:

  • 你在&lt;td&gt;{{$assignee-&gt;customer-name}}&lt;/td&gt; 中输了&gt;

标签: php laravel


【解决方案1】:

你应该试试这个;

<table class="table table-bordered">
    <tr>
        <th>No</th>
        <th>Entry id:</th>
        <th>Person Name</th>
        <th>Referance No:</th>
        <th>timein</th>
        <th>timeout</th>
        <th width="280px">Action</th>
    </tr>
    @foreach ($assignees as $assignee)
    <tr>
        <td>{{ ++$i }}</td>
        <td>{{ $assignee->id }}</td>
        <td>{{$assignee->customer->name}}</td>
        <td>{{ $assignee->refnumber }}</td>
        <td>{{ $assignee->timein }}</td>
        <td>{{ $assignee->timeout }}</td>

【讨论】:

    【解决方案2】:

    你的问题在这里&lt;td&gt;{{$assignee-&gt;customer-name}}&lt;/td&gt;

    它应该是&lt;td&gt;{{$assignee-&gt;cust-&gt;name}}&lt;/td&gt;,而你错过了这个-&gt;,所以它假定名称是一个常量。

    【讨论】:

      【解决方案3】:

      您的代码中有两个问题。

      1. 此行存在语法错误。这引发了上述问题。

        <td>{{$assignee->customer-name}}</td>
        

        应该是

        <td>{{$assignee->customer->name}}</td>
        
      2. 但是,您将您的关系命名为 cust 而不是 customer。所以你也需要解决这个问题。

        <td>{{$assignee->cust->name}}</td>
        

      这应该可以修复您的代码。

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 2018-03-08
        • 2020-01-15
        • 2015-11-29
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多