Blade 查看显示关系数据

Blade view show relationship data

我有一个客户端模型,客户端属于很​​多客户端地址。在控制器上,show 方法在客户端中传递,但如果我尝试访问地址方法,它会抛出以下错误。

Invalid argument supplied for foreach() (View: /var/www/resources/views/clients/show.blade.php)

show.blade.php

@extends('layouts.app')
@section('content')
<section class="content-header">
    <h1 class="pull-left">Client {{ $client->client_name }}</h1>
</section>
<div class="content">
    <div class="clearfix"></div>

    @include('flash::message')

    <div class="clearfix"></div>
    <div class="box box-primary">
        <div class="box-body">

            @foreach ($client->addresses as $address)
               {{ $address->address_line1 }}
            @endforeach
        </div>
    </div>
    <div class="text-center">

    </div>
</div>
@endsection

控制器

/**
 * Display the specified resource.
 *
 * @param  \App\Client  $client
 * @return \Illuminate\Http\Response
 */
public function show(Client $client)
{
    //
    return view('clients.show')->with('client', $client);
}

客户端模型

class Client extends Model
{
    //
    use SoftDeletes;

    protected $table = 'clients';
    //protected $primaryKey  = 'id';

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';


    protected $dates = ['deleted_at'];


    public $fillable = [
        'client_ref',
        'client_name',
        'is_active'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'client_ref' => 'string',
        'client_name' => 'string',
        'is_active' => 'string',
        'created_at' => 'date',
        'updated_at' => 'date'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [

    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function addresses()
    {
        return $this->hasMany('ClientsAddresses');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function history()
    {
        return $this->hasMany('ClientsHistory');
    }
}

ClientsAddresses 模型

class ClientsAddresses extends Model
{
    //
    protected $table = 'clients_addresses';

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    public $fillable = [
        'client_id', 'address_type', 'is_active', 'address_line1', 'address_line2', 'address_line3',
        'city', 'country', 'postcode'
    ];

    public function Client(){
        return $this->hasOne('App\Models\Client');
    }
}

客户迁移

Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->string("client_ref");
        $table->string('client_name');
        $table->enum('is_active', ['Y', 'N'])->default('Y');
        $table->timestamps();
        $table->softDeletes();
    });

客户地址迁移

Schema::create('clients_addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('client_id')->comments('id from the clients table');
    $table->enum('address_type', ['Billing', 'Shipping'])->default('Billing');
    $table->enum('is_active', ['Y', 'N'])->default('Y');
    $table->string('address_line1');
    $table->string('address_line2')->nullable();
    $table->string('address_line3')->nullable();
    $table->string('city');
    $table->string('country')->default('UK');
    $table->string('postcode');
    $table->timestamps();
    $table->index(['address_type', 'is_active']);
 });

外键迁移

Schema::table('clients_addresses', function (Blueprint $table) {
    //
    if (Schema::hasColumn('clients_addresses', 'client_id')) {
        $table->foreign('client_id')->references('id')->on('clients');
    }
});

我认为您必须在 :

中写入完整路径
public function addresses()
{
    return $this->hasMany('ClientsAddresses');
}


public function history()
{
    return $this->hasMany('ClientsHistory');
}

试一试:

return $this->hasMany('App\ClientsHistory');
 return $this->hasMany('App\ClientsAddresses');

您必须更改 Laravel 用于路由模型绑定的参数类型提示:

/**
 * Display the specified resource.
 *
 * @param  \App\Models\Client  $client
 * @return \Illuminate\Http\Response
 */
public function show(\App\Models\Client $client)
{
    return view('clients.show')->with('client', $client);
}

正如 Ghyath Darwish 指出的那样,您还必须在关系中指定整个 class 名称。
或者如果两个模型共享相同的命名空间,则使用 YourModel::class