1

I'm new to Laravel and I'm trying to retrieve the id value of a selected row using query builder but it's not working. I've tried it in many ways according to the Laravel documentation and I still have a problem. I think it's related to the use of a variable but I don't know how to fix it.

   public function submit_idea(Request $request)
   {
      $key=$request->input('key');
      $workshop_id= DB::table('workshops')->where('autokey',$key)->value('id');
      $id = auth()->User()->id;
      $idea=new Idea;
      $idea->title=$request->input('title');
      $idea->description=$request->input('description');
      $idea->user_id=$id;
      $idea->workshop_id=$workshop_id;
      $idea->save();
      return view('submit_idea');
   }

the error i'm getting is: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'workshop_id' cannot be null (SQL: insert into ideas (title, description, user_id, workshop_id) values (ppp, iiuu, 7, ?))

Can anyone help me, please?

Batoulz
  • 45
  • 1
  • 7

2 Answers2

0

Change:

$workshop_id = DB::table('workshops')->where('autokey',$key)->value('id');

To:

$workshop_id = DB::table('workshops')->select('id')->where('autokey',$key)->first();

By the way, the errors means that the workshop_id can't be null. If it can be null, be sure to add nullable() to the column in your migration file.

hhelderneves
  • 925
  • 8
  • 24
0

You can also working with

$workshopId = DB::table('workshops')->where('autokey', $key)->first()->pluck('name');

echo $workshopId;

Community
  • 1
  • 1
Raghu Aryan
  • 53
  • 1
  • 5