0

I am using the Laravel framework to work with my MySQL database, and currently want to update my database from a JSON object, that will be sent from somewhere else.

Currently, I have it the same as my 'Store' function, which is obviously not going to work, because it will update everything, or refuse to work because it is missing information.

This is the for each I have currently, it does not work, but I am not experienced with how it is best to parse a JSON with a for-each, then store it.

 public function update(Request $request,$student)
    {
        $storeData = User::find($student);
        
       foreach ($request as $value) {
            $storeData-> username = $value;
       }

Here is my store function, with all the info that the front-end team may send in a JSON format.

$storeData->username=$request->input('username');
        $storeData->password=$request->input('password');
        $storeData->email=$request->input('email');
        $storeData->location=$request->input('location');
        $storeData->role=DB::table('users')->where('user_id', $student)->value('role');
        $storeData->devotional_id=$request->input('devotional_id');
        $storeData->gift_id=$request->input('gift_id');

       $storeData->save(); 
        return dd("Info Recieved");

3 Answers 3

1

You can write the method like the below snippet.

Also, assume you are working with laravel API, so you don't need to parse the incoming JSON input, but you will receive these values as items in the request object.

However, you should use the filled method in order to determine if the field is existing and has a value, the update function will override with empty values otherwise.

I just added this method to the first input, but you have to use it each and every input if you are not sure what the front end will pass.

public function update(Request $request, $student)
{
    $storeData = User::find($student); // should be id

    if ($request->filled('username')) { // use this for other items also
        $storeData->username      =  $request->input('username');
    }
    $storeData->password      =  $request->input('password');
    $storeData->email         =  $request->input('email');
    $storeData->location      =  $request->input('location');
    $storeData->role          =  DB::table('users')->where('user_id', $student)->value('role');
    $storeData->devotional_id =  $request->input('devotional_id');
    $storeData->gift_id       =  $request->input('gift_id');

    $storeData->update(); 
    dd("Info Recieved");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Why would they send json data from the front in a post?

Really it would be from a form input. Like Rinto said it would be request object.

$user->username = $request->user_name;

I'm gathering this is a form on the front to create a new user. Why not use the built in auth scaffolding that has this set up for you in the register area?

So I'd personally use...

//look up user that matches the email or create a new user
$user = User::firstOrNew(['email' =>  request('email')]);

//add other input values here 
$user->name = request('name');

//save  
$user->save();

Hard to give an exact answer to this when the question is a bit vague in what you're doing. There are many methods in Laravel to accomplish things. From your code it just looks like registration. Also, the big gotcha I see in your code is you are passing a text only password and then adding that password in plain text to your database. That is a big security flaw.

Comments

0

you can convert your JSON object to an array and then do your foreach loop on the new array. To update a table in Laravel it's update ($storeData->update();) not save. Save is to insert.

$Arr = json_decode($request, true);

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.