2

I am having trouble saving this into database. When I submit my data into database it will only show name_of_bear and all the many relationship stuff(type_of_fish) but not the type_of_bear

Can someone explain to me why it can't work and also maybe give me an example on how it should be done. Thank you

Controller: (this works)

public function submit(Request $request)
{
    $fishType= $request->input('type_of_fish');
    $Name = $request->input('Name');
    $bearType = $request->input('bearType');

    $bear = Bear::create(['Name' => $request->input('name_of_bear')]);
    $bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);

    return ('thank you');

}

But if I were to do this:

Controller : (doesn't work)

public function submit(Request $request)
{
    $fishType= $request->input('type_of_fish');
    $Name = $request->input('Name');
    $bearType = $request->input('bearType');

    $bear = Bear::create(['Name' => $Name]);
    $bear = Bear::create(['bearType' => $bearType]); --> doesn't  work if add in this
    $bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);

    return ('thank you');

}

or this:

Controller: (doesn't work)

public function submit(Request $request)
{
    $fishType= $request->input('type_of_fish');
    $Name = $request->input('Name');
    $bearType = $request->input('bearType');

    $bear = Bear::create(['Name' => $Name], ['bearType' => $bearType]); --> doesn't work
    $bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);

    return ('thank you');

}
3
  • try $bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]); Commented Oct 11, 2017 at 7:18
  • there is another way to inserting data to data base if you need i can share it with you Commented Oct 11, 2017 at 7:19
  • @Jigar Yup it works thanks a lot. Sure I would love to know more about it (to Gaurav) Commented Oct 11, 2017 at 7:20

4 Answers 4

1

You can add it like this

$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);

full code:

public function submit(Request $request)
{
    $fishType= $request->input('type_of_fish');
    $Name = $request->input('Name');
    $bearType = $request->input('bearType');

    $bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);
    return ('thank you');
} // removed extra }

For further information you can read documentation

Sign up to request clarification or add additional context in comments.

5 Comments

Tks but the documentation I had read it already and I find it a little difficult to understand. Is there possibly another resource where they would give more examples?
Yes there may be , you just need to google with specific topic. You can accept as answer if found useful :) .
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]); is there any limit in using this? Because when I try add another field example $bear = Bear::create(['Name' => $Name, bearType=> $bearType, 'bearLocation' => $bearLocation]); it only show name and type and not the location in the database
Oh nvm I forgot to put protected fillable inside my model hahaha tks :)
Glad to help you :)
1

You can just use:

$bear = Bear::create(['Name' => $Name, 'bearType' => $bearType]);

For more info, visit this link.

Comments

1

You can insert data like this:

public function submit(Request $request)
{
    $data = array();
    $data['type_of_fish']= $request->type_of_fish;
    $data['Name'] = $request->Name;
    $data['bearType'] = $request->bearType;

    $bear = Bear::create($data);

    return ('thank you');

}

For more information read Documentation

Comments

1

I would do it like this, assuming that post array keys matches column names.

$bear = Bear::create($request->all()->except(['_token', 'type_of_fish']));    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.