Laravel 5创建一个新的记录是属于关联3个其他表(Laravel 5 Creating a N

2019-10-23 21:57发布

我有4代表的用户,地区,租金和位置

  1. 每个用户的hasMany出租
  2. 每个地区的hasMany出租
  3. 每个位置的hasMany出租
  4. 基本上是租赁属于关联的所有3个表

地区 - ID,名称位置 - ID,STREET_ADDRESS,市,省,POSTAL_CODE出租 - ID,REGION_ID,LOCATION_ID,USER_ID

我已经设置了这些表之间的hasMany和属于关联关系,并在测试廷克他们,但现在我想创建和更新出租。

  • 该REGION_ID向上传递通过请求- $ regionId
  • user_ID的是验证:: ID() - $用户id
  • 该LOCATION_ID通过取请求只有()部分,做地方台的检查中发现,如果它存在,我抢LOCATION_ID - $ locationId
  • 其余职位数据只()再次使用这些数据抓起- $ rentalData

这一切工作了这一点,但你如何创建和使用我已经提取的ID和数据更新出租,这几乎是工作原理:

Location::find($locationId)->rentals()->create($rentalData);

但是,需要获得$ locationId和$用户id混进去不知何故,它看起来不正确,使他们可填写。

我一直在玩它像这样至今:

// 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);

UPDATE

因此,我可以继续使用ORM掉线会和这样做,但我还是想明白以后我学会了看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);

UPDATE

这个怎么样的解决方案?

RentalRequest检查REGION_ID存在,用户永远是验证用户::()。

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')
    ]);
}

Answer 1:

我不明白为什么做起来很复杂吗?

尝试这个:

解决方法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);


文章来源: Laravel 5 Creating a New Record that BelongsTo 3 Other Tables