I’m getting this error: Indirect modification of overloaded element of App\Models\ has no effect, change ->get(); to ->first->toArray(); another error Call to a member function toArray() on null
here the code
$penjualan = Penjualan::find(session('id_penjualan'));
$detail = PenjualanDetail::with('produk')->where('id_penjualan', session('id_penjualan'))->get();
$transaction = new Penjualan();
foreach ($detail as $item) {
$transaction_details = [
'order_id' => $penjualan->id_penjualan,
'gross_amount' => $penjualan->bayar,
];
$item_details = [
'id' => $penjualan->id_penjualan,
'price' => $item->harga_jual,
'quantity' => $item->jumlah,
'name' => $item->produk->nama_produk,
];
$customer_details = [
'first_name' => $request->get('uname'),
'last_name' => '',
'email' => $request->get('email'),
'phone' => $request->get('number'),
];
$transaction = [
'transaction_details' => $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
];
}
$snapToken = Midtrans\Snap::getSnapToken($transaction);
$transaction->snap_token = $snapToken;
$transaction->save();
anyone could help me to fix this?
I have done Midtrans payment before.
The problem you've got with your current code are
you want to store snap_token inside your Penjualan model, but you use new Penjualan()
when multiple items are ordered, your code do not support it cause $item_details is a single array inside foreach loop
So here is an updated code with some notes and recommendations.
$id = session('id_penjualan');
$validated = $request->validate([
'uname' => 'required|string',
// 'lname' => 'nullable|string',
'email' => 'required|string',
'number' => 'required|string',
]);
// use single query to retrieve Penjualan and PenjualanDetail
// for product name, recommend to store it within PenjualanDetail, along with price and base_price
$penjualan = Penjualan::query()
->with('details')
->find($id);
if (!$penjualan) {
// you can redirect your customer to cart or any other page
return redirect('cart')->with('error', 'Can not find Penjualan.');
}
$item_details = [];
foreach ($penjualan->details as $item) {
$item_details[] = [
'id' => $item->id,
'price' => $item->harga_jual,
'quantity' => $item->jumlah,
// recommended product's name to be stored within PenjualanDetail
'name' => $item->nama_produk,
];
}
$transaction_details = [
'order_id' => $penjualan->id_penjualan,
// "bayar" must not contain any decimal, use cast Money
'gross_amount' => $penjualan->bayar,
];
// Optional
$customer_details = [
'first_name' => $validated['uname'],
'last_name' => '', // $validated['lname'],
'email' => $validated['email'],
'phone' => $validated['number'],
// 'billing_address' => '',
// 'shipping_address' => '',
];
// this is a simple array that will be sent to method getSnapToken()
$transaction = [
'transaction_details' => $transaction_details,
'customer_details' => $customer_details,
'item_details' => $item_details,
];
// this method only accept good old array
$snapToken = Midtrans\Snap::getSnapToken($transaction);
$penjualan->snap_token = $snapToken;
// recommend to store your customer detail here, either multiple fields or single array field storing $customer_details
// $penjualan->first_name = $customer_details['first_name'];
// $penjualan->last_name = $customer_details['last_name'];
// $penjualan->email = $customer_details['email'];
// $penjualan->phone = $customer_details['phone'];
// $penjualan->billing_address = $customer_details['billing_address'];
// $penjualan->shipping_address = $customer_details['shipping_address'];
$penjualan->save();
I commented out some parts that you might not use, where I did used them in the past.
Feel free to adjust them to suite your needs.
public function getSnapToken ()
{
$id = session('id_penjualan');
$member = Member::orderBy('nama')->get();
$penjualan = Penjualan::with(['penjualan_detail' => $detail = function ($query) {
$query->where('item', 'kode_produk, subtotal, jumlah, nama_produk');
}])->where('id_penjualan', ('id_penjualan'))
->orderBy('id_penjualan')
->get();
$transaction = array($penjualan);
foreach ( $detail as $item ) {
$transaction_details = [
'order_id' => $this->penjualan->id_penjualan,
'gross_amount' => $this->penjualan->bayar,
];
$item_details = [
'id' => $item->produk->kode_produk,
'price' => $item->jumlah * $item->subtotal,
'quantity' => $item->jumlah,
'name' => $item->produk->nama_produk,
];
$customer_details = [
'id' => $penjualan->member->kode_member,
'first_name' => $validated['uname'],
'last_name' => $validated['lname'],
'email' => $validated['email'],
'phone' => $validated['number'],
// 'billing_address' => '',
// 'shipping_address' => '',
];
$transaction = [
'transaction_details' => $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
];
}
$snap_token = '';
try {
$snap_token = \Midtrans\Snap::getSnapToken($transaction);
}
catch (\Exception $e) {
echo $e->getMessage();
}
echo "snap_token = ".$snap_token;
}
public function payment(Penjualan $penjualan)
{
$snap_token = $penjualan->snap_token;
if (is_null($snap_token)) {
$midtrans = new CreateSnapTokenService($penjualan);
$snap_token = $midtrans->getSnapToken();
$snap_token = Penjualan::where('id_penjualan')->update(['snap_token' => $snap_token]);
}
return view('penjualan.payment', ['snap_token'=>$snap_token]);
}
Related
I'm trying to do simple function edit student table field, But its updating only first field: first_name
My controller.
public function editStudent(Request $request)
{
$studId = $request->get('studId');
$studFirstName = $request->get('firstName');
$studLastName = $request->get('lastName');
$studGender = $request->get('gender');
$studBirthday = $request->get('birthday');
$studSchoolId = $request->get('schoolId');
$update = Student::where('id', $studId)->update(['first_name' => $studFirstName],
['last_name' => $studLastName], ['gender' => $studGender], ['birthday', $studBirthday], ['school_id' => $studSchoolId]);
return response()->json([
"update" => $update,
]);
}
Update takes a single array, you're passing in multiple arrays
$update = Student::where('id', $studId)
->update([
'first_name' => $studFirstName,
'last_name' => $studLastName,
'gender' => $studGender,
'birthday'=> $studBirthday,
'school_id' => $studSchoolId
]);
Its better to get the item from DB then update that on a second query:
$student = Student::find($studId);
$student->update([
'first_name' => $request->firstName,
'last_name' => $request->lastName,
'gender' => $request->gender,
'birthday'=> $request->birthday,
'school_id' => $request->schoolId
]);
i have a problem, every time I enter or refresh a page it inserts a new record
Controller:
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}
Image Database:
I suggest adding a check to see if method is get or post...
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
if ($request->isMethod('post')) {
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
}
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}
This is the my back-end code. I wrote the test for this function as follows:
public function index(Request $request)
{
$fields = 'in:' . implode(',', Schema::getColumnListing('suppliers'));
$messages = [
'order_by.in' => 'The selected column name is invalid.',
];
$this->validate($request, [
'page' => ['numeric'],
'per_page' => ['numeric'],
'order_direction' => ['in:desc,asc,DESC,ASC'],
'order_by' => [$fields],
], $messages);
$perPage = 10;
$filter = '';
$orderBy = 'id';
$orderDirection = 'DESC';
try {
if ($request->has('per_page')) $perPage = (int)$request->per_page;
if ($request->has('filter')) $filter = $request->filter;
if ($request->has('order_by')) $orderBy = $request->order_by;
if ($request->has('order_direction')) $orderDirection = strtoupper($request->order_direction);
$suppliers = Supplier::select('id', 'firstname', 'lastname', 'phone', 'email', 'custom_field_1', 'custom_field_2')->where('store_id', Auth::user()->store);
if (!!$filter) {
$suppliers->where('id', 'LIKE', "%{$filter}%")
->orWhere('firstname', 'LIKE', "%{$filter}%")
->orWhere('lastname', 'LIKE', "%{$filter}%")
->orWhere('email', 'LIKE', "%{$filter}%")
->orWhere('phone', 'LIKE', "%{$filter}%");
}
$suppliers->orderBy($orderBy, $orderDirection);
$suppliers = $suppliers->paginate($perPage);
return response()->json([
'success' => true,
'data' => $suppliers->toArray(),
], Response::HTTP_OK);
} catch (Exception $e) {
report($e);
return serverExceptionMessage();
}
}
This is what I have tried and test. I'm creating a store because store_id is a foreign key in my supplier table and adding suppliers dynamically and filtering the firstname through the URL and getting the result from seeJson as an array:
public function testSupplierListViewPaginationFilterTest()
{
$user = factory('App\Models\User')->make();
$store = (factory('App\Models\Store')->make())->getAttributes();
$this->actingAs($user)
->post('/create-new-store', $store)
->seeStatusCode(Response::HTTP_OK);
$attributes = [
'id' => 1,
'firstname' => 'ashid',
'lastname' => 'mhd',
'phone' => 776358547,
'email' => 'ashid#email.com',
'custom_field_1' => 'test',
'custom_field_2' => 'test',
];
$user->store = Store::latest()->first()->id;
$attributes['store_id'] = Auth::user()->store;
$supplier = Supplier::create($attributes);
$this->actingAs($user)
->get('/suppliers?filter=' . $supplier['firstname'], $attributes)
->seeStatusCode(Response::HTTP_OK)->seeJson([
'success' => true,
"data" =>
[
'id' => 1,
'firstname' => 'ashid',
'lastname' => 'mhd',
'phone' => 776358547,
'email' => 'ashid#email.com',
'custom_field_1' => 'test',
'custom_field_2' => 'test',
]
]);
}
I'm getting following error:
1) SupplierTest::testSupplierListViewPaginationFilterTest
Unable to find JSON fragment
["data":"custom_field_1":"test","custom_field_2":"test","email":"ashid#email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":776358547}] within [{"data":{"current_page":1,"data":[{"custom_field_1":"test","custom_field_2":"test","email":"ashid#email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":"776358547"}],"first_page_url":"http:\/\/localhost\/suppliers?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/suppliers?page=1","next_page_url":null,"path":"http:\/\/localhost\/suppliers","per_page":10,"prev_page_url":null,"to":1,"total":1},"success":true}].
Failed asserting that false is true.
I need a best solution for this problem .
I have to make permissions on seeder. Right now I have something like this:
$adminPermissions = collect([
$adminPermissions = collect([
'permission_read',
'permission_list',
'permission_create',
'permission_edit',
'permission_delete',
'role_read',
'role_list',
'role_create',
'role_edit',
'role_delete',
])->map(function ($name) {
return Permission::create([
'name' => $name
]);
});
])->map(function ($name) {
return Permission::create([
'name' => $name
]);
});
But I want to make something like this:
$rolePermissions = [
'role_read',
'role_list',
'role_create',
'role_edit',
'role_delete',
];
$permissionsForPermissions = [
'permission_read',
'permission_list',
'permission_create',
'permission_edit',
'permission_delete',
];
$adminPermissions = collect([
$PermissionsForPermissions,
$rolePermissions
])->map(function ($name) {
return Permission::create([
'name' => $name
]);
});
I know that this will not work because collect is waiting only for one array but I am asking if it's possible to do something like this because the first example is ugly.
You can use array_merge(), to merge your permission's arrays into one. So it will be something like:
$adminPermissions = collect(
array_merge(
$permissionsForPermissions,
$rolePermissions
)
)->map(function ($name) {
return Permission::create([
'name' => $name
]);
});
P.S. I am still not sure this is "more readable", but tastes differs.
I am not sure but you can try this. I used it like this way
$permission = array(
array('name' => 'user_view'),
array('name' => 'user_create'),
array('name' => 'user_edit'),
array('name' => 'user_delete'),
array('name' => 'project_view'),
array('name' => 'project_create'),
array('name' => 'project_edit'),
array('name' => 'project_delete'),
array('name' => 'team_view'),
array('name' => 'team_create'),
array('name' => 'team_edit'),
array('name' => 'team_delete')
);
Permission::insert($permission);
I'm building a small application in Laravel 5.5 where I'm having two models: Interaction and Interaction Summary in my Interaction model I'm having following relationship:
public function meetingSummaries()
{
return $this->hasMany('App\InteractionSummary');
}
Now to create the interaction I'm using following in my controller:
$data = $request->only('user_id', 'event_type', 'schedule', 'summaries', 'type', 'venue', 'with_client');
$data['schedule'] = Carbon::parse($request->schedule)->addHours(5)->addMinutes(30)->toDateTimeString();
$meeting = [];
$meeting['user_id']= $data['user_id'];
$meeting['schedule'] = $data['schedule'];
$meeting['type'] = $data['type'];
$meeting['with_client'] = $data['with_client'];
$meeting['venue'] = $data['venue'];
$meeting['event_type'] = $data['event_type'];
$interaction = Interaction::create($meeting);
if($data['summaries'])
{
$container = [];
foreach($data['summaries'] as $summary)
{
$summary = (Object)$summary;
if($summary->client['label'])
{
$container[] = new InteractionSummary([
'company_id' => $summary->client['label'],
'nature' => $summary->type,
'user_id' => $summary->mention['label'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
}
}
$interaction->meetingSummaries()->saveMany($container);
}
But while updating I don't know to overcome this, as in my fields there might be new or old relational data. I'm trying something like this:
if($data['summaries'])
{
$container = [];
foreach($data['summaries'] as $summary)
{
$summary = (Object)$summary;
if($summary->id)
{
$container[] = new InteractionSummary([
'id' => $summary->id,
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
else {
$container[] = new InteractionSummary([
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
}
$interaction->meetingSummaries()->save($container);
}
Which is sending me error:
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given, called in ~\app\Http\Controllers\InteractionsController.php on line 449
If I do:
$interaction->meetingSummaries()->saveMany($container);
I get repeated new fields.
Guide me how to overcome this. Thanks.
Use the findOrNew() method:
$summaryItem = InteractionSummary::findOrNew($summary->id);
$summaryItem->fill([
'company_id' => $summary->client['value'],
'nature' => $summary->type,
'user_id' => $summary->mention['value'],
'action' => $summary->action,
'feedback' => $summary->comment
]);