简体   繁体   English

如何在laravel 5.3上使用雄辩的关系?

[英]How to use eloquent-relationships on laravel 5.3?

My controller is like this : 我的控制器是这样的:

public function create()
{ 
    $data = array(
                'email' => 'chelsea@gmail.com'
            );

    $data = Order::where('email', $data['email'])->first();
    dd($data);
}

My model order is like this : 我的模型顺序是这样的:

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

class Order extends Model
{
    use SoftDeletes, Notifiable;

    public $table = 'orders';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'number',
        'user_id',
        'store_id',
        'total_amount',
        'total_product',
        'service_amount',
        'status',
        'checkout_at'
    ];

    protected $casts = [
        'number' => 'string',
        'user_id' => 'integer',
        'store_id' => 'integer',
        'total_amount' => 'integer',
        'total_product' => 'integer',
        'service_amount' => 'integer',
        'status' => 'integer',
        'checkout_at' => 'date'
    ];

    public static $rules = [
        'user_id' => 'required',
        'store_id' => 'required'
    ];

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class,'store_id','id');
    }
}

table user have field id, name, email etc 表用户具有字段ID,名称,电子邮件等

table store have field id, name, email etc 表存储具有字段ID,名称,电子邮件等

I want get user name and store name from table user and table store. 我想从表用户和表存储中获取用户名和存储名。 Then, I want store it on variable $data 然后,我要将其存储在变量$data

How can I do it? 我该怎么做?

UPDATE UPDATE

My model user is like this : 我的模型用户是这样的:

<?php

namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
    public $table = 'users';
    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'email',
        'password',
        'full_name',
        'birth_date',,
        'mobile_number',
    ];

    protected $casts = [
        'name' => 'string',
        'email' => 'string',
        'password' => 'string',
        'full_name' => 'string',
        'birth_date' => 'date',,
        'mobile_number' => 'string',
    ];

    public static $rules = [
        'name' => 'required',
        'email' => 'required'
    ];
}

My model store is like this : 我的模型商店是这样的:

<?php

namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Store extends Model
{
    use SoftDeletes;
    public $table = 'stores';
    protected $dates = ['deleted_at'];

    public $fillable = [
        'user_id',
        'domain',
        'name',
        'email'
    ];

    protected $casts = [
        'user_id' => 'integer',
        'domain' => 'string',
        'name' => 'string',
        'email' => 'string'
    ];

    public static $rules = [
        'user_id' => 'required',
        'domain' => 'required',
        'name' => 'required'
    ];

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }
}

First you need to make model for User Table and Store table then define respective table names in both model after that...... 首先,您需要为用户表和存储表创建模型,然后在两个模型中定义各自的表名……

public function create(Request $request)
{ 
    $data  = $request->all();
    $email = 'chelsea@gmail.com';
    $user  = User::where('email', $email)->first();
    $store = Store::where('email', $user->email)->first();
    $order = Order::where('user_id', $user->id)->where('store_id', $store->id)->first();


    $result = array();
    $result['orderTable'] = $order;
    return $result;


}

Use with in your order controller query, just pass the array of relationships that you have declared in your order model query will fetch the related tables data based on model relations, like below 使用with您的订单控制器的查询,只是通过基于模型的关系,您在您的订单模型查询已宣布的关系将获取相关表中的数据的阵列,像下面

In order controller 订单控制器

$data = Order::where('col', 'val')->with(['user','store'])->first();    

In order model 订单模型

public function user()
{
    return $this->belongsTo(User::class,'user_id','id');
}

public function store()
{
    return $this->belongsTo(Store::class,'store_id','id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM