Can't pass in data in store method

Viewed 31

I cant figure out why I am unable to pass in the "name" in my store method.
this line:- 'name' => $id->name,
I did check and the variable is not null.

Here's My Controller:-

$id = DB::table('friends')
      ->where('created_by', '=', $request->created_by)
      ->where('friends_id', '=', auth()->id())
      ->first();
      dd($id->name);
  if ($id && $id->friends_id == auth()->id()) {

      Post::create([
            'title'=>$data['title'],
            'body'=>$data['body'],
            'created_by'=>$request->created_by,
            'user_id'=>Auth::user()->id,
            'filled_by'=>Auth::user()->uuid,
            'name' => $id->name,

          ]);


  } else {

     Post::create([
            'title'=>$data['title'],
            'body'=>$data['body'],
            'created_by'=>$request->created_by,
            'user_id'=>Auth::user()->id,
            'filled_by'=>Auth::user()->uuid,

          ]);
}
1 Answers

Make sure, in Post the column name field is fillable.

protected $fillable = [
    'title','body','created_by','user_id','filled_by','name'
];

I would recommend you to follow this way of your if condition.

$post = [
    'title'=>$data['title'],
    'body'=>$data['body'],
    'created_by'=>$request->created_by,
    'user_id'=>Auth::user()->id,
    'filled_by'=>Auth::user()->uuid
]

if ($id && $id->friends_id == auth()->id()) {
    $post['name'] = $id->name;
}

Post::create($post);
Related