Php Laravel从联接表中给表单模型赋值
我有代码来获取数据并像这样连接两个表:Php Laravel从联接表中给表单模型赋值,php,laravel,jointable,Php,Laravel,Jointable,我有代码来获取数据并像这样连接两个表: $loan = Loan::findOrFail( $id ) ->join('customers', 'loans.customer_id', '=', 'customers.id') ->join('loans_detail','loans.id','=','loans_detail.loans_id') ->get(); return
$loan = Loan::findOrFail( $id )
->join('customers', 'loans.customer_id', '=', 'customers.id')
->join('loans_detail','loans.id','=','loans_detail.loans_id')
->get();
return view( 'loans.detail', compact( [ 'loan' ] ) );
然后我将该数据返回到:
{!! Form::model( $loan, [ 'method' => 'put' ] ) !!}
{!! Form::text( 'payment_type', null, [ 'class' => 'form-control'] ) !!}
但数据(付款类型)不显示。但是,如果删除连接代码,则会显示数据(付款类型)
若我对带有联接表的数据库使用dd()或不使用数据表,则会产生一些不同的结果
带联接表
Builder {#783 ▼
#query: Builder {#782 ▶}
#model: Loan {#779 ▶}
#eagerLoad: []
#localMacros: []
#onDelete: null
#passthru: array:12 [▶]
#scopes: []
#removedScopes: []
}
Loan {#779 ▼
#table: "loans"
#guarded: []
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
不带外联接表
Builder {#783 ▼
#query: Builder {#782 ▶}
#model: Loan {#779 ▶}
#eagerLoad: []
#localMacros: []
#onDelete: null
#passthru: array:12 [▶]
#scopes: []
#removedScopes: []
}
Loan {#779 ▼
#table: "loans"
#guarded: []
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
findOrFail()
已执行查询,之后调用join()
启动新查询
试试这个:
$loan = Loan::whereKey($id)
->join('customers', 'loans.customer_id', '=', 'customers.id')
->join('loans_detail','loans.id','=','loans_detail.loans_id')
->first();
顺便说一句:这种查询应该使用Relationships:findOrFail()
已执行查询,之后调用join()
启动新查询
试试这个:
$loan = Loan::whereKey($id)
->join('customers', 'loans.customer_id', '=', 'customers.id')
->join('loans_detail','loans.id','=','loans_detail.loans_id')
->first();
顺便说一句:这种查询应该使用Relationships: