刀片视图显示关系数据

时间:2018-06-09 14:44:14

标签: php eloquent laravel-5.6

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

1.1.1

show.blade.php

import React, { Component } from 'react';
import $ from 'jquery';
import './Navigation.scss';

class nav extends Component {

    componentDidMount = () => {
        // Hide Header on on scroll down
        var didScroll;
        var lastScrollTop = 0;
        var delta = 5;
        var navbarHeight = $('header').outerHeight();

        $(window).scroll(function(event){
            didScroll = true;
        });

    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);

    function hasScrolled() {
        var st = $(this).scrollTop();

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta) {
            return;
        }

        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            $("header").removeClass("nav-down").addClass("nav-up");
        } else {
            // Scroll Up
            if(st + $(window).height() < $(document).height()) {
                $("header").removeClass("nav-up").addClass("nav-down");
            }
        }

        lastScrollTop = st;
    }

}

render() {

    return (
        <header className="fixed-top">
            <nav id="small-top-nav" className="nav navbar navbar-expand-lg nav-down">

                /* ALL LINKS CODE */

            </nav>

        </header>
    );
}

export default nav;

控制器

.nav-up {
    top: -2.5rem;
}

.nav-down {
    down: 2.5rem;
}

客户端模型

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

ClientsAddresses Model

@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');
    }
}

外键迁移

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');
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你必须写下完整的路径:

public function addresses()
{
    return $this->hasMany('ClientsAddresses');
}


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

尝试:

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

答案 1 :(得分:1)

您必须更改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指出的那样,你还必须在你的人际关系中指定整个班级名称 如果两个模型共享相同的命名空间,请使用YourModel::class

相关问题