0

Trying to redirect to the previous page with message when user doesn't have enough balance. Controller:

if ($balance[0] > $kitap_baha[0]) {

    $userkitap = new UlanyjyKitap();
    $userkitap->user_id = Auth::id();
    $userkitap->yarysh_id = $id;
    $userkitap->save();

    $galan = $balance[0] - $kitap_baha[0];
    $user = User::find(Auth::id());
    $user->balance = $galan;
    $user->save();

    return response()->download($downloads[0]);
} else {
    return redirect()->back()->with('status', 'Balansyňyz ýeterlik däl');
}

View:

@if (session('status'))
    <div style="margin-top: 20px;">
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <strong>Bagyşlaň!</strong> {{ session('status') }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>
@endif

Result of dd($balance, $kitap_baha); is:

 Collection {#311 ▼
  #items: array:1 [▼
    0 => 7.0
  ]
}
Collection {#303 ▼
  #items: array:1 [▼
    0 => 10.0
  ]
}

$balance - user's balance $kitap_baha - book's price

It returns correctly, but i think there is no session in view.

Am I doing something wrong here ?

Rwd
  • 34,180
  • 6
  • 64
  • 78
Maksat
  • 311
  • 5
  • 24
  • Use `return redirect(...)->with(...)` or `return back()->with(...)`; might be an issue with chaining them. – Tim Lewis Sep 06 '18 at 20:52
  • it didn't help `return back()->with('status', 'Balansyňyz ýeterlik däl');` – Maksat Sep 06 '18 at 20:59
  • Ok; wasn't sure if chaining those methods would mess with the logic, seems not to. Is your comparison correct? Do a `dd($balance, $kitap_baha);` in your Controller and verify that your comparison is correct/functioning as expected. Update your question with the result of `dd($balance, $kitap_baha);` also, if you can. – Tim Lewis Sep 06 '18 at 21:03
  • I did it, all things are correct, $balance and $kitap_baha are correct – Maksat Sep 06 '18 at 21:07
  • OK... Can you post the result of `dd($balance, $kitap_baha);` or no? I won't be able to help further if you don't (don't see anything wrong with any other parts of the question) – Tim Lewis Sep 06 '18 at 21:09
  • `Collection {#311 ▼ #items: array:1 [▼ 0 => 7.0 ] }` – Maksat Sep 06 '18 at 21:12
  • `Collection {#303 ▼ #items: array:1 [▼ 0 => 10.0 ] }` – Maksat Sep 06 '18 at 21:12
  • Sigh... Don't post those as comments; edit that into your question please... But, that aside, it looks like it should be hitting the `else` statement, so session values should be getting set. Any errors in `storage/logs`? – Tim Lewis Sep 06 '18 at 21:14
  • `$kitap_baha` - price of the book, `$balance` - user's balance – Maksat Sep 06 '18 at 21:14
  • Edit those 3 comments into your question as clarification; they'll get buried in the comments. (In fact they already are) – Tim Lewis Sep 06 '18 at 21:15

0 Answers0