i'm trying this updateorcreate method and for some reason id does not insert nothing into database, here is my code example, it enters into catch but it does not display any error msg.
public function insertFacturiFluxFurnizor(Request $request)
{
$user = auth()->user();
$idFluxReceptie = $request->input('idFluxReceptie');
$facturi = $request->input('facturi');
try {
foreach ($facturi as $factura) {
$facturiWms = FacturiWms::updateOrCreate([
'codFactura' => $factura['codFactura']
], [
'fluxReceptie' => $idFluxReceptie,
'idFurnizor' => $factura['idFurnizor'],
'fiscalId' => $factura['fiscalId'],
'codFactura' => $factura['codFactura'],
'dataFactura' => $factura['dataFactura'],
'userId' => $user->userId
]);
}
return response()->json(['facturiWms' => $facturiWms, 'message' => 'CREATED'], 201);
} catch (\Exception $e) {
return response()->json(['message' => $e], 409);
}
}
}
Related
I used Laravel In-App purchase. But I got 422 error after $subscriptionReceipt variable.
This is my code
try {
$user = $request->user();
putenv('GOOGLE_APPLICATION_CREDENTIALS=../storage/keys/my-nourish-plan-app-8d8d0c926954.json');
$subscriptionReceipt = Product::googlePlay()->id($plan_id)->token($receipt)->get();
if ($subscriptionReceipt) {
$latestReceipt = $subscriptionReceipt->getLatestReceiptInfo()[0];
$date = Carbon::now();
Subscription::create(
[
'plan_id' => $request->plan_id,
'is_valid' => 1,
'product_id' => $latestReceipt->getProductId(),
'package_id' => $latestReceipt->getOriginalProductId(),
'transaction_id' => $latestReceipt->getTransactionId() ?? 0,
'start_date' => Carbon::parse($latestReceipt->getPurchaseDate()->toDateTime())->format('Y-m-d'),
'end_date' => Carbon::parse($latestReceipt->getExpiresDate()->toDateTime())->format('Y-m-d'),
'user_id' => $user->id,
'free_plans_month' => $date->month,
'free_plans_availability' => 1
]
);
return response()->apiSuccess(null, 'Subscription added!');
}
return response()->apiError('Receipt verification failed');
} catch (\Exception $e) {
Log::error($e);
return response()->apiError('Something went wrong please try again');
}
I am building registration form with Passport in Laravel using repository pattern, and when I try to register with postman I get error
Undefined property: Illuminate\\Http\\Response::$id
It breaks when I try and return response like this
return response(['user' => $user, 'access_token' => $accessToken]);
but when I just return $user instead then it works fine, but I don't have access token then. How can I register succesfully with access token? Any help is appreciated. Here is my code.
AuthController.php
public function register(RegisterUserRequest $request)
{
try {
$user = $this->service->createNewUser($request);
return $this->returnResource($user);
} catch (\Exception $exception) {
return $this->failureJson($exception);
}
}
AuthService.php
public function createNewUser(BaseRequest $request)
{
DB::beginTransaction();
try {
$request['confirmation_hash'] = Str::random(50);
$request['password'] = Hash::make($request['password']);
$user = parent::create($request);
Mail::to($user['email'])->send(new ConfirmationMail($user)); // Send email to user to confirm his email!
$accessToken = $user->createToken('authToken')->accessToken;
DB::commit();
return response(['user' => $user, 'access_token' => $accessToken]); // HERE IT BREAKS!!!
} catch (\Exception $exception) {
DB::rollBack();
throw new \Exception($exception->getMessage());
}
}
UserResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
'password' => $this->password,
'remember_token' => $this->remember_token,
'api_token' => $this->api_token,
'confirmation_hash' => $this->confirmation_hash,
'email_verified_at' => $this->email_verified_at,
'is_verified' => $this->is_verified,
'verification_expires_at' => $this->verification_expires_at
];
}
Inside your createNewUser try to just return user return $user and then inside your UserResource.php create the token ' api_token' => $this->createToken('authToken')->accessToken
Here's my DB transaction...
public function store(Request $request)
{
DB::beginTransaction();
try {
$superdoc = SuperDoc::create([
'user_id' => $request->user_id,
'project_name' => $request->project_name,
'status' => $request->status,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
'lines' => $request->lines,
'words' => $request->words,
]);
$lastInsertedId = DB::select('SELECT MAX(ID) FROM super_docs');
} catch (ValidationException $e) {
DB::rollBack();
return redirect()->back();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
try {
$document = Document::create([
'superD_id' => $request->superD_id,
'url' => $request->url,
'status' => $request->status,
'text' => $request->text,
'doc_type' => $request->doc_type,
'language' => $request->language,
'date' => $request->date,
]);
return response()->json($superdoc, 201);
} catch (ValidationException $e) {
DB::rollBack();
return redirect()->back();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
DB::commit();
}
I want to set the super_doc to auto-generated last id into the document table superD_id in the above transaction. Can anyone help me?
I don't have Knowledge about laravel bu i can give an idea, After inserting into SuperDoc table , you can get that model and the model will give you the model superD_id , that superD_id you can use in the Document Table.
So i think your code will look like this:
public function store(Request $request)
{
DB::beginTransaction();
try {
$superdoc = SuperDoc::create([
'user_id' => $request->user_id,
'project_name' => $request->project_name,
'status' => $request->status,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
'lines' => $request->lines,
'words' => $request->words,
]);
//$lastInsertedId=DB::select('SELECT MAX(ID) FROM super_docs');
} catch (ValidationException $e) {
DB::rollBack();
return redirect()->back();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
try {
$document = Document::create([
//$superdoc will have object of saved data which will also have SuperD_id
'superD_id' => $superdoc->superD_id,
'url' => $request->url,
'status' => $request->status,
'text' => $request->text,
'doc_type' => $request->doc_type,
'language' => $request->language,
'date' => $request->date,
]);
return response()->json($superdoc, 201);
} catch (ValidationException $e) {
DB::rollBack();
return redirect()->back();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
DB::commit();
}
I amtrying to insert values to laravel table inside a foreach. In my view I am selecting some checkboxes and I need its value to get inserted into a table. In my controller, I am getting all the required values to be inserted into the table. But everytime when I run, the code inside the foreach results in error.
Here is my controller code.
try {
$e_id = request()->segment(3);
$et = $repository->find($e_id);
if (!empty($et)) {
$rules = [
's_users' => 'bail|required|array',
'e_id' => 'bail|required',
];
$messages = [
's_users.required' => required,
'e_id.required' => required ,
's_users.array' => required,
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return response()->json(['error' => 1, 'code' => 422, 'errors' => $validator->errors()], 422);
} else {
DB::transaction(function () use ($request, $admin) {
DB::table('e_users')->whereIn('user_id', $request->input('s_users'))->delete();
foreach ($request->input('s_users') as $suser) {
$eUsers = new EUsers();
$eUsers->e_id = $request->input('e_id');
$eUsers->uid = $suser;
$eUsers->save();
}
});
return response()->json(['error' => 0, 'code' => 200, 'message' => "success"], 200);
}
} else {
throw new \Exception("invalid);
}
} catch (\Exception $e) {
return response()->json(['error' => 1, 'code' => 200, 'message' => "unknown error occurred"], 200);
}
The above code always throws exception, which shows unknown error occured. Thanks in advance
I'm modifying the signup function that store user's data( email and password). I wish to include more data such as phone number, name and national ID. I tried but in mysql database it doesn't show anything. Here is what I have done :
public function signup(Request $request)
{
$credentials = $request->only([
'national_id' ,
'name' ,
'phone',
]);
$validator = Validator::make($credentials, [
'name' => 'required',
'email' => 'sometimes|email|unique:users',
'password' => 'sometimes|min:6|confirmed',
'password_confirmation' => 'sometimes|min:3',
'national_id' => 'required','unique:national_id',
]);
if ($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
try {
$user = $this->users->create($request->except('roles', 'permissions'));
if (!$user->id) {
return $this->response->error('could_not_create_user', 500);
}
$hasToReleaseToken = Config::get('boilerplate.signup_token_release');
if ($hasToReleaseToken) {
$user->national_id = $request->national_id;
return $this->login($request);
}
return $this->response->created();
} catch (\Exception $e) {
return $this->response->error($e->getMessage(), 500);
}
}
when I dd($request->national_id it shows me the national_id data in postman, but in my database it's NULL.