Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Add data to the $request->all()

I wan to add the sector id to the request but when I submit the data nothing store on it. Here is my code

public function store(QuestionRequest $request)
    {
        $data = $request->all();
        Question::create($data);
        $sectors =  Sector::lists('id');
        foreach($sectors as $sector){
            CustomizeQuestion::create(array_add($request->all(), 'sector_id', $sector));
        }

        flash()->success('New question has been added.');
        return redirect('questions');
    }

I have tried this code also but it is the same :

public function store(QuestionRequest $request)
    {
        $data = $request->all();
        Question::create($data);
        $sectors =  Sector::lists('id');
        foreach($sectors as $sector){
            $data['sector_id'] = $sector;
            CustomizeQuestion::create($data);
        }

        flash()->success('New question has been added.');
        return redirect('questions');
    }
like image 603
Alaa Mohammad Avatar asked Feb 08 '23 09:02

Alaa Mohammad


1 Answers

If you only want to add one 'id' to your request as you said, you can simply do this before creating anything :

$data = $request->all();
$data['sector_id'] = whatever you want;
Question::create($data);

Or like the second way you showed.

If this approach doesn't work verify if you have your properties specified in the model's fillable array and if you are using the correct property name as you specified in your migration.

like image 178
cbcaio Avatar answered Feb 10 '23 23:02

cbcaio