0

How I can update table in DB, with use function findOrNew I want first check column in table. If I have same title not add, if I dont have - push I campare @title@ in table DB.

public function update(Request $request, $id)
{$news = News::findOrNew($request->title);
    if($news->title==$request->title){

    }else{
        $news->'title'=$request->input('title');
    };
 }
Oleg St
  • 9
  • 5

1 Answers1

0

Welcome to SO ... I think you are looking for firstOrNew not findOrNew which is used for searching on the primary key specifically.

$news = News::firstOrNew(['title' => $request->input('title')]);

$news will be the existing record that has a 'title' that matches the request input or it will be a new instance with the 'title' set to the value you were searching for.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • Thank you so much. But I want check title. If I have title in table DB - return @mistake@, if dont have - add news in table – Oleg St Jul 17 '20 at 05:15
  • this is doing what you think you are attempting to do in your example ... it is searching the database for a matching record, if it can't find one it gives you a new instance with the values filled – lagbox Jul 17 '20 at 05:17
  • oh, I understand. I dont need made check. – Oleg St Jul 17 '20 at 05:20