1 Replies - 178 Views - Last Post: 28 November 2017 - 10:46 AM Rate Topic: -----

#1 Splashsky   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 311
  • Joined: 25-August 13

Laravel Custom Form Fail Message

Posted 28 November 2017 - 06:27 AM

When a player attempts to create a guild, it's simply putting in the name of the guild then validation takes over. For basic Laravel validation, I simply have the required, min, alpha_dash and unique rules set. Next, I have to check and see if the player can afford to create the guild based on their current Gold value. I also need to make sure the player isn't already in a guild. At the moment, it's simply an if statement and it dies and dumps (dd()).
public function store(Request $request)
{
    $request->validate([
        'name' => 'required|min:4|alpha_dash|unique:guilds'
    ]);

    $char  = Auth::user()->character;
    $cost  = config('game.create-guild-cost');
    $guild = new Guild;

    if($char->gold < $cost) {
        dd('Not enough money');
    }

    if($char->guild != null) {
        dd('You cannot already be in a guild.');
    }

    $guild->name = request('name');
    $guild->leader_id = $char->id;
    $guild->save();

    $char->gold = $char->gold - $cost;
    $char->guild_id = $guild->id;
    $char->save();

    return redirect()->route('guilds.show', ['guild' => $guild]);
}



and I can't seem to find any way to make those if's into an error message that gets passed back to the route if it fails. o;

Is This A Good Question/Topic? 0
  • +

Replies To: Laravel Custom Form Fail Message

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2040
  • View blog
  • Posts: 6,262
  • Joined: 15-January 14

Re: Laravel Custom Form Fail Message

Posted 28 November 2017 - 10:46 AM

You're passing the guild to the route, so you can probably also pass a message or whatever you want to pass.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1