如何使用Laravel中的Eloquent获取一对多关系的数据?

时间:2017-12-01 11:07:17

标签: php laravel laravel-eloquent

如何使用Laravel中的Eloquent获取一对多关系的数据?实际上我跟着Eloquent Documentation。但我的代码返回一个空数组。这是我的工作:

数据库架构

父表: mtg_workspace 包含列(mw_id [主键],mw_name,...,mw_access)

子表: mtg_workspace_amenities 包含列(id [主键],wa_mwid,wa_name)

型号: MtgWorkspace.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MtgWorkspace extends Model
{
  protected $table = 'mtg_workspace';
  public $timestamps = false;

  public function MtgWorkspaceAmenities(){
    return $this->hasMany('App\MtgWorkspaceAmenities', 'wa_mwid');
  }
}

型号: MtgWorkspaceAmenities.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MtgWorkspaceAmenities extends Model
{
  protected $table = 'mtg_workspace_amenities';
  public $timestamps = false;

  public function MtgWorkspace(){
    return $this->belongsTo('App\MtgWorkspace');
  }
}

控制器: WorkspaceController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\MtgWorkspace;
use App\MtgWorkspaceAmenities;

class WorkspaceController extends Controller
{
    public function index()
   {
      $workspace = MtgWorkspace::with('MtgWorkspaceAmenities')->get();

      return $workspace;
    }
}

这里显示以下输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试定义doInBackground(Void... voids)模型的主键(因为它不是MessagesListAdapter messages; ListView messagesLv; public static ArrayList<Message> arrayList; public static ProgressDialog progressDialog; public static DynamoDBScanExpression dbScanExpression; public static List<Message> messageList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_messages); new Scan().execute(); } private class Scan extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(TestActivity.this); progressDialog.setTitle(name); progressDialog.setMessage("Searching for messages..."); progressDialog.setIndeterminate(false); progressDialog.show(); } @Override protected Void doInBackground(Void... voids) { dbScanExpression = new DynamoDBScanExpression(); Condition condition = new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(MainActivity.msgId)); dbScanExpression.addFilterCondition("msgId", condition); messageList = MainActivity.mapper.scan(Message.class, dbScanExpression); arrayList = new ArrayList<Message>(); for (Message msg : messageList) { if (msg.getUserId() == null || msg.getUserId().equals(MainActivity.userId)) { msg.setMsgId(msg.getMsgId()); msg.setDate(msg.getDate()); msg.setTime(msg.getTime()); msg.setMessage(msg.getMessage()); msg.setUserId(msg.getUserId()); arrayList.add(msg); } } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if (!messageList.isEmpty()) { messagesLv = (ListView) findViewById(R.id.messagesListView); messages = new MessagesListAdapter(MinaActivity.this, arrayList); messagesLv.setAdapter(messages); progressDialog.dismiss(); } else { TextView tv = (TextView) findViewById(R.id.noMessages); tv.setVisibility(View.VISIBLE); progressDialog.dismiss(); } } } ):

MtgWorkspace

我不认为它可以将孩子id与父{name}}模型匹配,因为它试图使用protected $primaryKey = 'mw_id'; 上的MtgWorkspaceAmenities属性来匹配MtgWorkspace上的外键,当它通过急切加载的结果旋转时。

旁注:

在定义时,您必须将更多参数传递给id上的反向关系,因为MtgWorkspace想要使用添加了MtgWorkspaceAmenities的调用方法名称蛇,如果没有另外说明的话作为关键。