get a data from an array - laravel

get a data from an array ..code for getting cart
public function show()
{
$data = $this->cartService->getCart(auth()->user()->id);
dd($data);
}
Response
array:2 [
"cart" => array:9 [
"id" => 244
"user_id" => 53
"total_mrp" => "56000.00"
"promo" => "HELLO"
"discount" => "30.00"
"meta" => {#1575
+"sub_total": 56000
}
]
"cart_items" => array:1 [
]
]
]
]
HOW CAN I GET PROMO IN A Variabel???
i tried
return $data->promo;
but error

You can use collect(). You can read about this function from laravel doc
$data = $this->cartService->getCart(auth()->user()->id);
$cart = collect($data['cart']??[]);
dd($cart->get('promo'));

Related

How to pass array values into the mail class one by one

I want to pass the values of array into the Mail class. I have this code where i get the emails.
Getting Emails from Database
$vendorsId = OrdersProduct::select('vendor_id')->where('order_id', $order_id)->pluck('vendor_id');
$vendorEmails = Admin::select('email')->whereIn('id', $vendorsId)->get()->toArray();
Result
array:2 [▼
0 => array:1 [▼
"email" => "mxxxxx#gmail.com"
]
1 => array:1 [▼
"email" => "myyyyy#gmail.com"
]
]
So i want to pass these email address into the mail class to send mail one by one, here below is the mail code
Sending Emails
$messageData = [
'order_id' => $order_id,
];
Mail::send('emails.order', $messageData, function ($message) use ($email) {
$message->to($email)->subject('Order Placed');
});
Thanks!
Why not use foreach?
$email = Auth::user()->email;
$messageData = [
'email' => $email,
'name' => Auth::user()->name,
'order_id' => $order_id,
'orderDetails' => $orderDetails,
];
foreach ($vendorEmails as $vemail) {
Mail::send('emails.order', $messageData, function ($message) use ($email, $vemail)
{
$message->to($vemail)->subject('Order Placed - stylooworld.info');
});
}
You can try like this ;
$emailList = Admin::->whereIn('id', $vendorsId)->pluck('email')->toArray();
Mail::send('emails.order', $messageData, function ($message) use ($emailList) {
$message->to($emailList)->subject('Order Placed');
});

Laravel belongsto relations get property problem

I have belongsto relation:
class Yetkiliservis extends Model
{
protected $table = 'yetkiliservis';
protected $guarded=[];
public function bolge(){
return $this->belongsTo(Bolgeler::class);
}
}
when I convert the model to array everything is right. It shows relation.
$yetkiliservisler = Yetkiliservis::with('bolge')->get();
dd($yetkiliservisler[0]->toArray());
result :
array:22 [▼
"id" => 1
"vergi_no" => "1"
"yerel_adi" => "1"
"bolge" => array:6 [▼ <------------------------------------
"id" => 1
"bolge_adi" => "İSTANBUL"
"ad_soyad" => "istanbul"
"email" => "istanbul#mail.com"
"created_at" => "2020-04-24 15:53:31"
"updated_at" => "2020-04-24 15:53:31"
]
"yetkili_adi" => "1"
]
But when I try to get by the property it shows null.
$yetkiliservisler = Yetkiliservis::with('bolge')->get();
dd($yetkiliservisler[0]->getAttributes());
result :
array:22 [▼
"id" => 1
"vergi_no" => "1"
"yerel_adi" => "1"
"bolge" => null <--------------------------------
"yetkili_adi" => "1"
]
First you change the raname like this
public function bolges(){
return $this->hasMany(Bolge::class,'yetkiliservi_id','id');
}
After you execute this command
composer dump-autoload
Then first check php artisan tinker; relation is correct??
Then try this
$yetkiliservisler[0]->bolges;
Hope it will helpful for you

Laravel parameterBag as array issues when inserting

I am trying to insert mass data but i get this error when i iterate through the request:
Symfony\Component\Debug\Exception\FatalThrowableError Cannot use
object of type Symfony\Component\HttpFoundation\ParameterBag as array
Request example:
request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
#parameters: array:4 [▼
"_token" => "Z1sF2K5LHU1bsQ4l3JRaOLCTQDmJ47qakigmrfI5"
"name" => "Rousaddasd 1"
"secsaddsans" => array:1 [▼
"secsdfs1" => array:2 [▼
"name" => "Sectdfs 1"
]
]
"submit" => "Submit"
]
}
Loop to go through the request and insert:
$abc = new abc;
$abc->create(['user_id' => 1, 'name' => $request->name, 'description' => 'test description']);
foreach($request as $item){
$asd = new asd;
$asd->name = $item['name'];
$asd->description = 'test description';
$abc->asds()->create([$asd]);
}
Im adding some data "inline" because i didnt handle them in the form yet, thats why the added duration and duration_unit for example.
You can use $request as array like this:
$request = $request->all();
And loop like:
foreach($request['sections'] as $item) {
....
foreach($item['exercises'] as $item2) {
....
}
}

Laravel resource ignoring given fields

I'm trying to convert a given API response in something easy to work with.
This is what I have so far:
Controller:
public function drive()
{
$optParams = [
'corpora' => 'drive',
'driveId' => env('GOOGLE_DRIVE_ID'),
'includeItemsFromAllDrives' => true,
'supportsAllDrives' => true,
'fields' => 'files(*)'
];
$results = $this->googleDrive->files->listFiles($optParams);
$data = collect($results);
$collection = new FileCollection($data);
dd($collection);
return 'Hi!';
}
FileCollection resource:
public function toArray($request)
{
return [
'data' => $this->collection,
'meta' => ['files_count' => $this->collection->count()],
];
}
File resource:
public function toArray($request)
{
return [
'name' => $this->resource['name'],
'mime' => $this->resource['mimeType'],
'parents' => $this->resource['parents'],
'version' => $this->resource['version'],
'webDownloadLink' => $this->resource['webContentLink'],
'webLink' => $this->resource['webViewLink'],
'modified_at' => $this->resource['modifiedTime'],
'size' => $this->resource['size']
];
}
This is the result I'm getting:
FileCollection {#223 ▼
+collects: null
+collection: Collection {#243 ▶}
+resource: Collection {#243 ▼
#items: array:2 [▼
0 => File {#224 ▼
+resource: Google_Service_Drive_DriveFile {#279 ▶}
+with: []
+additional: []
}
1 => File {#263 ▼
+resource: Google_Service_Drive_DriveFile {#269 ▶}
+with: []
+additional: []
}
]
}
+with: []
+additional: []
}
So as you can see the types of resources are correct, but for some reason the 'meta' in the FileCollection isn't there, nor are the fields in the File resource. All fields are still shown, like 30+ (instead of the 8 requested in the FileResource).
Am I missing something here?
EDIT:
When accessing the collection with the toArray() method ($collection->toArray(null)), I do indeed get the appropriate result:
array:2 [▼
"data" => Collection {#243 ▼
#items: array:2 [▼
0 => File {#224 ▶}
1 => File {#263 ▶}
]
}
"meta" => array:1 [▼
"files_count" => 2
]
]
So how do I make sure now that this collection uses the toArray() of the FileResources? (I'm still getting over 30 fields, instead of 8).
PS: I don't really get why I need to call the toArray(), nowhere in their documentation is it written : https://laravel.com/docs/5.8/eloquent-resources .

Laravel 5: test return redirect with session

I have an insert method in Laravel, that return a message
if ($update === true) {
return redirect(route('settings-cloudflare'))
->with('success', trans('common.operation_completed_successfully'));
// validation ok
} else {
How can I test that redirected view has "success" inside?
My test is:
$response = $this->followingRedirects()->actingAs($user)->post(Route('settings-cloudflare-store'),$data);
dd(session()->all());
But session doesn't have "success" inside.
This is the dd of session:
array:3 [
"_token" => "2jV8sVgRluET9UCng2HzqDYOgSkTVYMqJTT20MsP"
"_flash" => array:2 [
"new" => []
"old" => []
]
"_previous" => array:1 [
"url" => "http://192.168.1.101/settings/cloudflare"
]
]
Thank you very much

Resources