Dump within map function in Laravel - laravel

Within a map function i would like to use dd(); dump but i only get the first object.
When i use print_r(); within a map function i get all objects
$valeurCategorieCible = $CategoriesItineraires->map(function ($item) {
return $item->ciblesParCategorie->map(function ($ciblesParCategorie) {
return $ciblesParCategorie->cibles->map(function ($items) {
print_r($items); // Return all objects
dd($items); // Return only the first object then stop !
});
});
});

According to the documentation for dd() it "dumps the given variables and ends execution of the script" - the important part is "ends execution of the script"!
If you don't want to end execution use dump() instead of dd()

Related

how do i show paginate links when i have called it from another function laravel

I have this function from where i call another function in services. I have paginated in that function. now the thing is the number of data is as I have decaled but I can't just use links in view.
function example (){
$a = $this->exampleService->paginationfunction($all,12);
return view->(['a'=>$a])
}
in example service:
function paginationfunction($all,$paginate){
$x = Users::where('address',$all->name);
return $x->paginate($paginate);
}
now in view i have used $a->links() which shows error of
Method Illuminate\Database\Eloquent\Collection::links does not exist.

Unable to return function value from custom command

I am having a hard time returning a value from a custom command function to the main file.
command.js
spec.js
it does not return the expected value
Do you want to return the decodedTotp value?
If yes, you have to do this explicitly at the end of your custom function:
<...>
console.log(decodedTotp)
return decodedTotp
})
})

How can i get the buyers to still be able to purchase the product even if its unavailable?

I'm trying to create waitlist to buy product, but change the status of the transaction from 'Confirmed' to 'Waitlist'.
For that I have added a variable 'status' and a function in Transaction Model as follows:
const CONFIRMED_TRANSACTION = 'confirmed';
const WAITLIST_TRANSACTION = 'waitlist'; //waitlist product
public function isConfirmed() {
return $this->status == Transaction::CONFIRMED_TRANSACTION;
}
Default value of Transaction->status is 'CONFIRMED_TRANSACTION'.
Now every time the Product->status changes to 'unavailable' i would like to change the value of the Transaction->status to 'WAITLIST_TRANSACTION' when the transaction is created.
I am trying to achieve it using Event Listeners as:
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
But this gives me error :
**ErrorException: Undefined variable: product in file /home/vagrant/restfulapi7/app/Providers/AppServiceProvider.php on line 29**
How can I achieve the same in a better way?
You're using what's called a lambda or anonymous function, which has a different scope to the rest of your code.
function($transaction) does not have access to a variable named $product. So you're trying to call ->isAvailable() on a variable which doesn't exist.
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
Assuming it exists in the scope in which you call Transaction::created(), you can pass the $product variable into your lambda function like this:
function($transaction) use ($product) { ... }
For more information on lambda functions, read the php docs here
This solves the issue:
if(!$transaction->product->isAvailable())

Laravel chunk returns null

I'm needing to chunk my query as it's making PHP run out of memory, however the below code just dumps null:
$chunked = $query->chunk(25, function ($events) {
//dd($events);
});
dd($chunked);
However, when I do the below, it dumps the first chunk of 25:
$chunked = $query->chunk(25, function ($events) {
dd($events);
});
//dd($chunked);
No combination of changing dd($events); to the likes of return $events, return true, iterating over each item in each chunk and returning that - works.
Am I stupid/doing something wrong or is this not working like it should?
chunk() is a helper method which you can use on an instance of Query Builder or Eloquent Builder. In both cases, the method returns void:
Query Builder
Eloquent Builder
This means that your $chunked variable will be always empty.
The syntax you need to employ is the following:
Query Builder
DB::table('users')->chunk(100, function($users) {
foreach ($users as $user) {
//
}
});
Eloquent
Flight::chunk(200, function ($flights) {
foreach ($flights as $flight) {
//
}
});
Basically, all you need to do is to use a loop within the callback function of chunk() to modify your results.

getting the value of the single field output using the codeigniter active record

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
Use row() like,
return $this->db->get()->row()->name;
Use row() for a single row, and result() for multiple rows.
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
$row = $this->db->get()->row();
if (isset($row)) {
return $row->name;
} else {
return false;
}
}
Simply call the function from the controller like so:
$response = $this->model_name->sban_name($asset)
Try this code of block , I already checked and works fine:
function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->row()->name;
}

Resources