Viewed   546 times

In Laravel, there is a function return back();, which returns the user to the previous page. Is it possible to return back(); more than once within one function to return the user back twice or several times? I tried

public function ....()
{
  return back();
  return back();
}

but it doesn't seem to work.

 Answers

3

No, but you could use session system to save URLs of 2-3-4 pages back. Use Session:: facade or session() helper for shorter syntax:

$links = session()->has('links') ? session('links') : [];
$currentLink = request()->path(); // Getting current URI like 'category/books/'
array_unshift($links, $currentLink); // Putting it in the beginning of links array
session(['links' => $links]); // Saving links array to the session

And to use it:

return redirect(session('links')[2]); // Will redirect 2 links back
Saturday, December 24, 2022
3

We managed to resolve this by modifying the exceptions handler found in AppExceptionsHandler.php adding it in the render function.

public function render($request, Exception $e)
{
    if ($e instanceof AuthorizationException)
    {
        return response()->json(['error' => 'Not authorized.'],403);
    }
    return parent::render($request, $e);
}
Sunday, October 9, 2022
 
tute
 
3

Make sure to get() the subpages as IlluminateSupportCollection and mapWithKeys() to reformat the results. Use toArray() to provide the format Nova assumes:

private function selectOptions(): array
{
    $subpages = DB::table('subpages')->get();
    return $subpages->mapWithKeys(function ($subpage) {
        return [$subpage->slug => $subpage->slug];
    })->toArray();
}

This is how the returned result should look like:

[
    'my-article-1' => 'my-article-1',
    'my-article-2' => 'my-article-2',
    'my-article-3' => 'my-article-3',
]
Thursday, September 1, 2022
 
talha_q
 
4

You can achieve what you want in one iteration over the data using reduce like so:

$variations = [];

$result = array_reduce($filters, function ($result, $filter) use ($variations) {
    $filter['brand']['name'] = strtolower($filter['brand']['name']);
    if ($result['brands']->where('name', $filter['brand']['name'])->isEmpty()) {
        $result['brands']->push($filter['brand']);
    }

    foreach ($filter['options'] as $option) {
        $option['name'] = strtolower($option['name']);

        if ($result['options']->where('name', $option['name'])->isEmpty()) {
            $result['options']->push($option);
        }
    }

    if (isset($filter['rating']['id'])) {
        if ($result['ratings']->where('id', $filter['rating']['id'])->isEmpty()) {
            $result['ratings']->push($filter['rating']);
        }
    }

    foreach ($filter['tags'] as $tag) {
        $tag['name'] = strtolower($tag['name']);

        if ($result['tags']->where('name', $tag['name'])->isEmpty()) {
            $result['tags']->push($tag);
        }
    }

    foreach ($filter['variations'] as $variation) {
        $variation['name'] = strtolower($variation['name']);
        $variationName = $variation['name'];         
        $children = collect($variation['children'])->pluck('name');

        if ($result['variations']->where('name', $variation['name'])->isEmpty()) {
            $result['variations']->push($variation);
            $variations[$variationName] = $children;

        } else {
            $different = $variations[$variationName]->diff($children);
            
            if ($different->isNotEmpty()) {
               $result['variations']->push($variation);
               foreach ($different as $childName) {
                   $variations[$variationName]->push($childName);
               }  
            }
        }
    }

    return $result;

}, collect([
    'brands' => collect(),
    'options' => collect(),
    'ratings' => collect(),
    'tags' => collect(),
    'variations' => collect()
]));

If you need the result as an array, you can use the collection's toArray method:

    $result->toArray();
Friday, August 12, 2022
3

Decode to array and array_combine with the new keys.
Then loop the 'agent' and replace the keys again with array_combine.

$arr = json_decode($json, true);
$mainkeys = ["url", "secret_token", "agent"];
$subkeys = ["id", "quantity"];

$arr = array_combine(array_slice($mainkeys,0,count($arr)), $arr);

if(isset($arr["agent"])){
    foreach($arr["agent"] as &$val){
        $val = array_combine($subkeys, $val);
    }
}
unset($val); 

https://3v4l.org/mKePQ

array(3) {
  ["url"]=>
  string(26) "https://www.gosdoddgle.com"
  ["secret_token"]=>
  string(25) "stringstrinngstringstring"
  ["agent"]=>
  array(2) {
    [0]=>
    array(2) {
      ["id"]=>
      string(6) "sdsds1"
      ["quantity"]=>
      string(6) "sdsds1"
    }
    [1]=>
    &array(2) {
      ["id"]=>
      string(6) "sdsds1"
      ["quantity"]=>
      string(6) "sdsds1"
    }
  }
}
Tuesday, August 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :