databaselaravelsql-updatelaravel-controllerlaravel-datatables

Update table DB in Laravel with findOrNew


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');
    };
 }

Solution

  • 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.