Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 属于拉威尔的关系_Php_Laravel - Fatal编程技术网

Php 属于拉威尔的关系

Php 属于拉威尔的关系,php,laravel,Php,Laravel,我试图建立一对多的关系,每个客户可以分配到多个条目,下面是我的迁移表 客户表: Schema::create('customers', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('idtype'); $table->string('idnumber');

我试图建立一对多的关系,每个客户可以分配到多个条目,下面是我的迁移表

客户表:

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>
创建“受让人”时,laravel没有强制执行关系检查

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

您应该试试这个

<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>

不
条目id:
人名
参考编号:
蒂梅因
超时
行动
@foreach($受让人作为$受让人)
{{++$i}
{{$assignee->id}
{{$assignee->customer->name}
{{$assignee->refnumber}
{{$assignee->timein}
{{$assignee->timeout}

您的问题就在这里
{{{$assignee->customer name}


它应该是
{{$assignee->cust->name}
,而您错过了这个
->
,因此它假设name是一个constatnt。

您的代码中有两个问题

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

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

  • 这将修复您的代码。

    您在
    {{{$assignee->customer name}}
    行中丢失了
    <td>{{$assignee->customer-name}}</td>
    
    <td>{{$assignee->customer->name}}</td>
    
    <td>{{$assignee->cust->name}}</td>