0

chatcontroller.php

public function CreateConversation(Request $req)
{
    $user_id = Auth::user()->id;
    echo $req->to_id;
    conversation::create(
        [
            'user_id'  => $user_id,
            'user2_id' => $req->to_id,
        ]);
}

Createconversation.vue

<script>
export default{
components:
{
    AppLayout,
},
data()
{
    return{
       users: [],
       to_id: '',
    }
},

methods:
{
    
    CreateConversation(to_id)
    {
        this.to_id = to_id;
        console.log(to_id);
        axios.get('CreateConversation',{to_id: this.to_id,});
    }
}
}
</script>

Web.php

Route::get('/CreateConversation',[ChatsController::class,'CreateConversation']);

when i call createconversation from vue console.log the data correctly but in php it is null

error message: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user2_id' cannot be null

why it is null i dont know help me out!! is the syntax wrong?

2
  • Did that console.log(to_id) show some value or its null? Commented Mar 22, 2021 at 16:29
  • yess it shows correct value i passed Commented Mar 22, 2021 at 16:34

1 Answer 1

1

You are sending a GET request not a POST so try that.

public function CreateConversation($to_id)
{
    conversation::create(
        [
            'user_id'  => auth()->user()->id,
            'user2_id' => $to_id,
        ]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes that works perfectly can you explain? why get request deos not works?. thank you
If you use GET request then you don't need Request Object as an Argument to your method.
That Request $req is used when you have a POST request.
i'm enlighten by your knowledge thank you

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.