mercredi 2 décembre 2015

laravel 5 - creating a new "thing" in a controller method

I'm looking at this tutorial here: http://ift.tt/21uw7Yn.

But rather than follow it blindly, I'm trying to make it relevant to my needs. I have come across a problem. I'm looking at this code:

/**
 * Create a new task.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|max:255',
    ]);

    $request->user()->tasks()->create([
        'name' => $request->name,
    ]);

    return redirect('/tasks');
}

Ok. So in my application I want to create a country. No user associated with it, it's just a form posting the name of a country to this route. My form passes validation and if I use the following code, I can save the new country record to my database:

If I substitute

$request->user()->tasks()->create([
        'name' => $request->name,
    ]);

with...

$country = new Country;
$country ->name = $request->name;
$country ->save();

...then this works out ok. I don't really want this though.

What I would like to do is use something similar to the code in the tutorial. Can anyone tell me what I should do please?

I have tried a few different guesses, but no luck. They are exactly that tho. Guesses...

Thanks.

DS



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire