[英]How can I join one column in other table with Laravel?
實際上我使用的是 Laravel API。我有兩個表,一個名為所有者,另一個是包含名為 unit_no 的屬性的單位。 我想在帶有 reactJs 的下拉列表中顯示屬性 unit_no 中的值。但我不知道如何在后端部分執行此操作。 這是我的 controller 代碼:
function addOwner(Request $request){
$owner= new Owner();
$owner->name=$request->input('name');
$owner->email=$request->input('email');
$owner->password=$request->input('password');
$owner->telephone=$request->input('telephone');
$owner->cin=$request->input('cin');
$owner->presentAdress=$request->input('presentAdress');
$owner->permenantAdress=$request->input('permenantAdress');
$owner->file_path= $request->file('file')->store('owners');
$owner->save();
return $owner;
}
這是我的數據庫:
public function up()
{
Schema::create('owners', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('password');
$table->string('telephone');
$table->string('cin');
$table->string('presentAdress');
$table->string('permenantAdress');
$table->string('file_path');
$table->timestamps();
});
}
那么,您需要使用 eloquent。首先,如果您想進行聯接,則需要在 2 個表中的一個表中添加一個公共屬性。 例如,在 Units 中添加 owner_id 屬性。 在 Unit & Owner model 中,您將定義 Owner 和 Unit 表之間的關系。
但是對於兩個表的連接,一旦您將公共列添加到從 controller 遷移的 2 個表之一,您應該這樣做。
$join_table = DB::table('owner')
->join('units', 'owner.id', '=', 'units.owner_id');
從這一點開始,您甚至可以獲得所需的色譜柱。
$column = $join_table->pluck('unit_no');
無論如何,您應該改進問題不明確且不完整的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.