1

我有 4 个表用户、区域、租赁和位置

  1. 每个用户都有很多租借
  2. 每个地区都有很多出租
  3. 每个位置都有很多出租
  4. 基本上一个 Rental 属于所有 3 张桌子

Region - id, name Location - id, street_address, city, Province, postal_code Rental - id, region_id, location_id, user_id

我已经在这些表之间设置了 hasMany 和 belongsTo 关系,并在 Tinker 中对其进行了测试,但现在我想创建和更新租借。

  • region_id 通过请求向上传递 - $regionId
  • user_id 是 Auth::id() - $userId
  • location_id 是通过仅获取请求的一部分并检查位置表来找到的,如果存在,我会获取 location_id - $locationId
  • 剩余的帖子数据再次使用 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')
    ]);
}
4

1 回答 1

2

我不明白为什么要把它弄得这么复杂?

尝试这个:

解决方案 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);
于 2015-07-15T22:49:41.627 回答