Ahmad Hilman
Ahmad Hilman

Reputation: 103

Update query laravel return false result and nothing error show why?

i'm working with laravel project, and i have an issue that is update query result return false value if i update with the same data, how to solve this? do i have to validate first before run the query and send a notification that the data is the same? well this is my codes

public function update(Request $request)
{
    $kamar_id    = $request->input('kamar_id');
    $title       = $request->input('title');
    $content     = $request->input('content');
    $keyword     = $request->input('keyword');
    $description = $request->input('description');
    $prolog      = $request->input('prolog');
    $path        = $request->input('path');
    $sort        = $request->input('sort');
    $status      = $request->input('status');
    $type        = $request->input('type');
    $user_id     = $request->input('user_id');

    if (empty($request->input('path'))) {
        $path = serialize(array('data/def.png'));
    }else{
        $path = serialize(explode(',', $request->input('path')));
    }

    $data = array('title' => $title,
                  'content' => $content,
                  'keyword' => $keyword,
                  'description' => $description,
                  'prolog' => $prolog,
                  'path' => $path,
                  'sort' => $sort,
                  'status' => $status,
                  'type' => $type,
                  'user_id' => $user_id);
    // echo($kamar_id);
    $update = Kamar::where('kamar_id',$kamar_id)->update($data);
    if ($update) {
        $response['status'] = 1;
    }else{
        $response['status'] = 0;
    }

    return response()->json($response);
}

thanks for helping me

Upvotes: 2

Views: 4794

Answers (4)

PriyankMotivaras
PriyankMotivaras

Reputation: 740

Laravel Eloquent Update method returns true if anything updated in database from your query and return false if nothing is updated in database from your query. refer: https://laravel.com/docs/5.8/eloquent#updates

Upvotes: 2

gpanagiotis
gpanagiotis

Reputation: 74

You can force updated_at column to be updated (or you can create this column if you don't have). So the query will be always updated.

Upvotes: 0

Manish Gupta
Manish Gupta

Reputation: 105

Try using this

$kamarObj = new Kamar();
$kamarData = $kamarObj->find($kamar_id);

$result = $kamarData->update($data);

Upvotes: 0

Jithesh Jose
Jithesh Jose

Reputation: 1814

!nullTry

 $update = Kamar::where('kamar_id','=',$kamar_id)->first();

 if (!null($update)) 
 {
   $update->title = $title;
   $update->content = $content;
   $update->keyword = $keyword;
   $update->description = $description;
   $update->prolog = $prolog;
   $update->path = $path;
   $update->sort = $sort;
   $update->status = $status;
   $update->type = $type;
   $update->user_id = $user_id;
   $update->save();
   $response['status'] = 1;
 }
 else
 {
   $response['status'] = 0;
 }

Upvotes: 0

Related Questions