本文整理汇总了PHP中app\models\Account::findOrFail方法的典型用法代码示例。如果您正苦于以下问题:PHP Account::findOrFail方法的具体用法?PHP Account::findOrFail怎么用?PHP Account::findOrFail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Account
的用法示例。
在下文中一共展示了Account::findOrFail方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update
/**
* UPDATE /api/favouritesTransactions/{favouriteTransactions}
* @param Request $request
* @param FavouriteTransaction $favourite
* @return Response
*/
public function update(Request $request, FavouriteTransaction $favourite)
{
// Create an array with the new fields merged
$data = array_compare($favourite->toArray(), $request->only(['name', 'type', 'description', 'merchant', 'total']));
$favourite->update($data);
if ($request->has('account_id')) {
$favourite->account()->associate(Account::findOrFail($request->get('account_id')));
$favourite->fromAccount()->dissociate();
$favourite->toAccount()->dissociate();
$favourite->save();
}
if ($request->has('from_account_id')) {
$favourite->fromAccount()->associate(Account::findOrFail($request->get('from_account_id')));
$favourite->account()->dissociate();
$favourite->save();
}
if ($request->has('to_account_id')) {
$favourite->toAccount()->associate(Account::findOrFail($request->get('to_account_id')));
$favourite->account()->dissociate();
$favourite->save();
}
if ($request->has('budget_ids')) {
$favourite->budgets()->sync($request->get('budget_ids'));
}
$favourite = $this->transform($this->createItem($favourite, new FavouriteTransactionTransformer()))['data'];
return response($favourite, Response::HTTP_OK);
}
示例2: update
/**
* PUT /api/transactions/{transactions}
* @param Request $request
* @param Transaction $transaction
* @return Response
*/
public function update(Request $request, Transaction $transaction)
{
//For adding budgets to many transactions at once
if ($request->has('addingBudgets')) {
$transaction = $this->budgetTransactionRepository->addBudgets($request, $transaction);
} else {
$previousTotal = $transaction->total;
$data = array_filter(array_diff_assoc($request->only(['date', 'description', 'merchant', 'total', 'type', 'reconciled', 'allocated', 'minutes']), $transaction->toArray()), 'removeFalseKeepZeroAndEmptyStrings');
if ($request->has('account_id')) {
$transaction->account()->associate(Account::findOrFail($request->get('account_id')));
}
//Make the total positive if the type has been changed from expense to income
if (isset($data['type']) && $transaction->type === 'expense' && $data['type'] === 'income') {
if (isset($data['total']) && $data['total'] < 0) {
//The user has changed the total as well as the type,
//but the total is negative and it should be positive
$data['total'] *= -1;
} else {
//The user has changed the type but not the total
$transaction->total *= -1;
$transaction->save();
}
} else {
if (isset($data['type']) && $transaction->type === 'income' && $data['type'] === 'expense') {
if (isset($data['total']) && $data['total'] > 0) {
//The user has changed the total as well as the type,
//but the total is positive and it should be negative
$data['total'] *= -1;
} else {
//The user has changed the type but not the total
$transaction->total *= -1;
$transaction->save();
}
} else {
if ($transaction->type === 'expense' && array_key_exists('total', $data) && $data['total'] > 0) {
$data['total'] *= -1;
} else {
if ($transaction->type === 'income' && array_key_exists('total', $data) && $data['total'] < 0) {
$data['total'] *= -1;
}
}
}
}
// if(empty($data)) {
// return $this->responseNotModified();
// }
//Fire event
//Todo: update the savings when event is fired
event(new TransactionWasUpdated($transaction, $data));
$transaction->update($data);
$transaction->save();
if ($request->has('budget_ids')) {
$transaction->budgets()->sync($request->get('budget_ids'));
//Change calculated_allocation from null to 0
$budgetsAdded = $transaction->budgets()->wherePivot('calculated_allocation', null)->get();
foreach ($budgetsAdded as $budget) {
$transaction->budgets()->updateExistingPivot($budget->id, ['calculated_allocation' => '0']);
}
}
//If the total has changed, update the allocation if the allocation is a percentage of the transaction total
if ($previousTotal !== $transaction->total) {
$budgetsToUpdateAllocation = $transaction->budgets()->wherePivot('allocated_percent', '>', 0)->get();
foreach ($budgetsToUpdateAllocation as $budget) {
$calculatedAllocation = $transaction->total / 100 * $budget->pivot->allocated_percent;
$transaction->budgets()->updateExistingPivot($budget->id, ['calculated_allocation' => $calculatedAllocation]);
}
}
}
$transaction = $this->transform($this->createItem($transaction, new TransactionTransformer()))['data'];
return response($transaction, Response::HTTP_OK);
}