Laravel 5 创建属于 3 个其他表的新记录
Laravel 5 Creating a New Record that BelongsTo 3 Other Tables
我有 4 table 个用户、地区、租赁和位置
- 每个用户有很多出租
- 每个地区都有很多出租
- 每个位置都有很多出租
- 基本上一个Rental属于全部3 tables
地区 - id, name
位置 - ID,street_address,城市,省份,postal_code
出租 - id, region_id, location_id, user_id
我已经在这些 table 之间设置了 hasMany 和 belongsTo 关系并在 Tinker 中对其进行了测试,但现在我想创建和更新租赁。
- 通过请求向上传递了region_id-$regionId
- user_id 是 Auth::id() - $userId
- location_id 是通过请求的 only() 部分并检查位置 table 找到的,如果它存在,我抓住 location_id - $locationId
- 剩余的 post 数据再次使用 only() 获取该数据 - $rentalData
到目前为止所有这些都有效,但是您如何使用我提取的 ID 和数据创建和更新租金,这几乎有效:
Location::find($locationId)->rentals()->create($rentalData);
但是,需要以某种方式将 $locationId 和 $userId 混合在一起,让它们可填充似乎不正确。
到目前为止我一直这样玩:
// Retrieve the chosen rental region
$regionId = Region::find($request->input('region_id'));
// Retrieve authenticated user id
$userId = Auth::id();
// Retrieve rental location data
$rentalLocationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$locationData = RentalLocation::firstOrCreate($rentalLocationData);
$locationId = $locationData->id;
// Create the rental...?
$rental = Location::find($locationId)->rentals()->create($rentalData);
更新
所以我可以继续使用 ORM 并这样做,但我仍然想了解 Eloquent 是如何工作的,而不是我在观看 Laracast 视频时学到的基础知识所以任何帮助将不胜感激,现在我只是觉得它真的很混乱:
// Retrieve rental location data from the request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$rentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the foreign key ids not included in request
$foreignKeyIds = [ 'user_id' => Auth::id(), 'rental_location_id' => $rentalLocation->id ];
// Retrieve request data for creating a rental, and merge with foreign key ids
$requestData = $request->only('region_id', 'stall', 'level', 'description');
$rentalData = array_merge($foreignKeyIds, $requestData);
// Insert new rental with all field attributes included
DB::table('rentals')->insert($rentalData);
更新
这个解决方案怎么样?
RentalRequest 检查 region_id 是否存在,用户将永远是 Auth::user()。
public function store(RentalRequest $request)
{
// Retrieve the authenticated user
$User = Auth::user();
// Retrieve rental location data from request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$RentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the region for inserting the rental
$Region = Region::find($request->input('region_id'));
// Retrieve rental data from request
$requestData = $request->only('stall', 'level', 'description');
// Create a new rental, fill with request data, and add relationships
$rental = new Rental;
$rental->fill($requestData);
$rental->owner()->associate($User);
$rental->location()->associate($RentalLocation);
// Persist rental to database
$rental = $Region->rentals()->save($rental);
// Return rental data for capture by AngularJS interceptor
// TODO: filter rental data don't need it all returned to client
return response()->json([
'rental' => $rental,
'action' => 'rental_registered',
'message' => trans('rental.registered')
]);
}
我不明白为什么要把它搞得这么复杂?
试试这个:
解决方案 1
$User = User::find(Auth::id());
if(!$User) {
return 'no user';
}
$Region = Region::find($request->input('region_id'));
if(!$Region) {
return 'no region';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);
解决方案 2
// ->remember() for caching request to prevent many queries, for faster result use apc cache in config files, no need to use memcache when You're using 1 app server
if(!User::where('id', '=', Auth::id())->remember(1440)->exists()) {
return 'no user';
}
if(!Region::where('id', '=', $request->input('region_id'))->remember(1440)->exists()) {
return 'no user';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);
我有 4 table 个用户、地区、租赁和位置
- 每个用户有很多出租
- 每个地区都有很多出租
- 每个位置都有很多出租
- 基本上一个Rental属于全部3 tables
地区 - id, name 位置 - ID,street_address,城市,省份,postal_code 出租 - id, region_id, location_id, user_id
我已经在这些 table 之间设置了 hasMany 和 belongsTo 关系并在 Tinker 中对其进行了测试,但现在我想创建和更新租赁。
- 通过请求向上传递了region_id-$regionId
- user_id 是 Auth::id() - $userId
- location_id 是通过请求的 only() 部分并检查位置 table 找到的,如果它存在,我抓住 location_id - $locationId
- 剩余的 post 数据再次使用 only() 获取该数据 - $rentalData
到目前为止所有这些都有效,但是您如何使用我提取的 ID 和数据创建和更新租金,这几乎有效:
Location::find($locationId)->rentals()->create($rentalData);
但是,需要以某种方式将 $locationId 和 $userId 混合在一起,让它们可填充似乎不正确。
到目前为止我一直这样玩:
// Retrieve the chosen rental region
$regionId = Region::find($request->input('region_id'));
// Retrieve authenticated user id
$userId = Auth::id();
// Retrieve rental location data
$rentalLocationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$locationData = RentalLocation::firstOrCreate($rentalLocationData);
$locationId = $locationData->id;
// Create the rental...?
$rental = Location::find($locationId)->rentals()->create($rentalData);
更新
所以我可以继续使用 ORM 并这样做,但我仍然想了解 Eloquent 是如何工作的,而不是我在观看 Laracast 视频时学到的基础知识所以任何帮助将不胜感激,现在我只是觉得它真的很混乱:
// Retrieve rental location data from the request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$rentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the foreign key ids not included in request
$foreignKeyIds = [ 'user_id' => Auth::id(), 'rental_location_id' => $rentalLocation->id ];
// Retrieve request data for creating a rental, and merge with foreign key ids
$requestData = $request->only('region_id', 'stall', 'level', 'description');
$rentalData = array_merge($foreignKeyIds, $requestData);
// Insert new rental with all field attributes included
DB::table('rentals')->insert($rentalData);
更新
这个解决方案怎么样?
RentalRequest 检查 region_id 是否存在,用户将永远是 Auth::user()。
public function store(RentalRequest $request)
{
// Retrieve the authenticated user
$User = Auth::user();
// Retrieve rental location data from request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$RentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the region for inserting the rental
$Region = Region::find($request->input('region_id'));
// Retrieve rental data from request
$requestData = $request->only('stall', 'level', 'description');
// Create a new rental, fill with request data, and add relationships
$rental = new Rental;
$rental->fill($requestData);
$rental->owner()->associate($User);
$rental->location()->associate($RentalLocation);
// Persist rental to database
$rental = $Region->rentals()->save($rental);
// Return rental data for capture by AngularJS interceptor
// TODO: filter rental data don't need it all returned to client
return response()->json([
'rental' => $rental,
'action' => 'rental_registered',
'message' => trans('rental.registered')
]);
}
我不明白为什么要把它搞得这么复杂?
试试这个:
解决方案 1
$User = User::find(Auth::id());
if(!$User) {
return 'no user';
}
$Region = Region::find($request->input('region_id'));
if(!$Region) {
return 'no region';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);
解决方案 2
// ->remember() for caching request to prevent many queries, for faster result use apc cache in config files, no need to use memcache when You're using 1 app server
if(!User::where('id', '=', Auth::id())->remember(1440)->exists()) {
return 'no user';
}
if(!Region::where('id', '=', $request->input('region_id'))->remember(1440)->exists()) {
return 'no user';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);