Error Object of class Illuminate Database MySqlConnection could not be converted to string

0 votes

 I tried this using a variable,

$id = 3;
DB::transaction(function() {
    DB::table('users')->where('id','=',$id)->get();
});

It throws an error:

Undefined variable: id

I also tried to place to $id as a parameter like this:

$id = 3;
DB::transaction(function($id) {
    DB::table('users')->where('id', '=', $id)->get();
});

Still, an error:

Object of class Illuminate\Database\MySqlConnection could not be converted to string

Have I done anything wrong?

Sep 24, 2020 in Laravel by kartik
• 37,520 points
3,731 views

1 answer to this question.

0 votes

Hello @kartik,

The use keyword is what you need:

$id = 3;
DB::transaction(function($id) use ($id) {
    DB::table('users')->where('id', '=', $id)->get();
});

For PHP 7:

$id = 3;
DB::transaction(function() use ($id) {
    DB::table('users')->where('id', '=', $id)->get();
});

Hope it helps!!
Thank you!!

answered Sep 24, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

+2 votes
1 answer

Error: laravel.log could not be opened?

Hello, Never use 777 for directories on your ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,840 points
12,434 views
0 votes
1 answer
0 votes
1 answer

I get this error > Trying to get property of non-object . How to fix it?

Hello @aakash, If you want to save grades ...READ MORE

answered Aug 17, 2020 in Laravel by Niroj
• 82,840 points
24,085 views
0 votes
1 answer

Error:The Response content must be a string or object implementing __toString(), "boolean" given.

Hello @kartik, Your response must return some sort ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,840 points
5,851 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
4,230 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,278 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,149 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,048 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP