Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 Laravel5.3雄辩的关系没有得到准确的记录_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel5.3雄辩的关系没有得到准确的记录

Php Laravel5.3雄辩的关系没有得到准确的记录,php,laravel,eloquent,Php,Laravel,Eloquent,我使用Laravel5.3雄辩的关系从三个表中获取记录 客户表: class Client extends Authenticatable { use HasApiTokens, Notifiable; protected $table = "clients"; protected $fillable = [ 'name','country','place','mobile_no','email', 'password','api_token',

我使用Laravel5.3雄辩的关系从三个表中获取记录

客户表:

    class Client extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $table = "clients";
    protected $fillable = [
        'name','country','place','mobile_no','email', 'password','api_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

        public function services()
    {
        return $this->hasMany('App\SubscribeService');
    }
 <select>
                    @foreach($user->services as $services)
                    <option>{{$services->service_id}}</option>
                    @endforeach
                  </select>

服务表:

订阅服务表:

    class Client extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $table = "clients";
    protected $fillable = [
        'name','country','place','mobile_no','email', 'password','api_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

        public function services()
    {
        return $this->hasMany('App\SubscribeService');
    }
 <select>
                    @foreach($user->services as $services)
                    <option>{{$services->service_id}}</option>
                    @endforeach
                  </select>

我想显示已订阅服务的客户端列表,但我没有获取服务名称,我只是从subscribe\U service表中获取数据

客户端型号:

    class Client extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $table = "clients";
    protected $fillable = [
        'name','country','place','mobile_no','email', 'password','api_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

        public function services()
    {
        return $this->hasMany('App\SubscribeService');
    }
 <select>
                    @foreach($user->services as $services)
                    <option>{{$services->service_id}}</option>
                    @endforeach
                  </select>
客户端刀片服务器:

    class Client extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $table = "clients";
    protected $fillable = [
        'name','country','place','mobile_no','email', 'password','api_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

        public function services()
    {
        return $this->hasMany('App\SubscribeService');
    }
 <select>
                    @foreach($user->services as $services)
                    <option>{{$services->service_id}}</option>
                    @endforeach
                  </select>

@foreach($user->services as$services)
{{$services->service_id}
@endforeach

如何获取服务名称?请提供任何建议

如果您想在此处使用
hasMany()
,请执行以下操作:

$client = Client::with('services.service')->find($id);

@foreach ($client->services as $service)
    {{ $service->service->name }}
@endforeach
要实现这一点,您应该将此关系添加到
SubscribeService
模型中:

public function service()
{
    return $this->belongsTo('App\Service');
}
或者,您可以通过定义
belongtomany()
关系来使用多对多关系。在这种情况下,您可以执行以下操作:

$client = Client::with('services')->find($id);

@foreach ($client->services as $service)
    {{ $service->name }}
@endforeach

如果要在此处使用
hasMany()
,请执行以下操作:

$client = Client::with('services.service')->find($id);

@foreach ($client->services as $service)
    {{ $service->service->name }}
@endforeach
要实现这一点,您应该将此关系添加到
SubscribeService
模型中:

public function service()
{
    return $this->belongsTo('App\Service');
}
或者,您可以通过定义
belongtomany()
关系来使用多对多关系。在这种情况下,您可以执行以下操作:

$client = Client::with('services')->find($id);

@foreach ($client->services as $service)
    {{ $service->name }}
@endforeach

如果使用数据透视表,通常意味着模型(
客户机
服务
)之间的关系是一个
多个
,因此Laravel中每个模型之间的关系应该是
属于多个
,而不是
具有多个
。查看文档了解更多信息:如果使用透视表,通常意味着模型(
客户机
服务
)之间的关系是一个
多个
,因此Laravel中每个模型之间的关系应该是
属于多个
,而不是
具有多个
。查看文档了解更多信息:谢谢Alexey,我正在使用最后一个,但它返回相同的记录,我没有在记录中获取服务对象。[{“id”:1,“客户id”:7,“服务id”:1,“创建于”:“2017-03-25 11:27:04”,“更新于”:“2017-03-25 00:00:00”},{“id”:2,“客户id”:7,“服务id”:2,“创建于”:“2017-03-25 11:27:04”,“更新于”:“2017-03-25 00:00:00”{“id”:3,“客户id”:7,“服务id”:3,“创建于”:“2017-03-11:27:04”,“更新于”:“2017-03-25:00:00”{“客户id”:,“service_id”:4,“created_at:“2017-03-25 11:27:04”,“updated_at:“2017-03-25 00:00:00”}]在SubscribeService模型中添加了关系:public function service(){return$this->belongsTo('App\service');}和在客户端模型中:public function services(){return$this->hasMany('App SubscribeService');}我得到了正确的响应,但无法使用服务对象:{“id”:7,“name”:“Jhon Walker”,“email”:“Jhon”。walker@gmail.com“,”移动电话号码“:”788898“,”国家“:”新加坡“,”地方“:”印多尔“,”api“:”代币“$Xc7ohG“,”创建地点“:”2017“,”更新地点“:”2017“,”服务“:[{”id“:1,“服务id“:”1,“创建地点“:”2017 11:27:04“,”更新地点“:”2017-03-25 00:00:00,“服务”:{“id”:1,“标题”:“投资者的喜悦”,“描述”:“本产品专为相信买入和持有方法的投资者设计。”,“发布”:0,“免费试用”:“创建时间”:“2017-03-23 18:10:53”,“更新时间”:null}@Anirudh你说你在使用第二种方法。但在我的回答中,我写道如果你想使用
hasMany()
,你应该使用第一种方法。如果你想使用第二种方法,你应该定义
belongsToMany()
relationship.谢谢Alexey,我正在使用最后一个,但它返回的记录相同,我在记录中没有得到服务对象。[{“id”:1,“客户id”:7,“服务id”:1,“创建时间”:“2017-03-25 11:27:04”,“更新时间”:“2017-03-25 00:00:00”},{“id”:2,“客户id”:7,“服务id”:2,“创建时间”:“2017-03-25 11:27:04”,“更新时间”:“2017-03-25:00:00:00”},{“id”:3,“客户id”:7,“服务id”:3,“创建时间”:“2017-03-25 11:27:04”,“更新时间”:“2017-03-25 00:00:00”},{“id”:4,“客户id”:7,“服务id”:4,“创建时间”:“2017-03-25 11:27:04”,“更新时间”:“2017-03-25 00:00:00”}在订阅服务模型中添加了关系:公共功能服务({返回$this->belongsTo('App service');}在客户端模型中:public function services(){return$this->hasMany('App\SubscribeService');}我得到了正确的响应,但无法使用服务对象:{“id”:7,“name”:“Jhon Walker”,“email”:“Jhon”。walker@gmail.com“,”移动电话号码“:”788898“,”国家“:”新加坡“,”地点“:”印多尔“,”api_代币“:”Xc7ohG“,”创建地点“:”2017年,更新日期:2017年,服务日期:2017年,服务日期:1,客户编号:7,服务编号:1,创建日期:2017年11:27:04,更新日期:2017-03-25 00:00:00,服务日期:2017-03-23 18:10:53更新:null}]}@aniruddh您说您正在使用第二种方法。但是在我的回答中,我写道如果您想使用
hasMany()
您应该使用第一种方法。如果您想使用第二种方法,您应该定义
belongsToMany()
关系。