62

I have a page model. It has following columns in database table:

  • id
  • title
  • image
  • body

I want to update only "image" column value.

Here's my code:

public function delImage($path, $id) {
    $page = Page::find($id);
    $page->where('image', $path)->update(array('image' => 'asdasd'));
    \File::delete($path);
}

it throws me an error, that i am trying to use where() on a non-object. How can i correctly update my "image" column value?

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157

5 Answers5

109

You may try this:

Page::where('id', $id)->update(array('image' => 'asdasd'));

There are other ways too but no need to use Page::find($id); in this case. But if you use find() then you may try it like this:

$page = Page::find($id);

// Make sure you've got the Page model
if($page) {
    $page->image = 'imagepath';
    $page->save();
}

Also you may use:

$page = Page::findOrFail($id);

So, it'll throw an exception if the model with that id was not found.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • $page = Page::find($id); $page->image = 'imagepath'; $page->save(); This doesn't works, it throws me an error: "Creating default object from empty value" – Alexander Kim Jun 08 '14 at 18:49
  • Don't you have an `image` field in the `Page` model and make sure that you get an `object/Model` in `$page` when you use `$page = Page::find($id)`, so `$page = Page::find($id); if($page) { $page->image = 'imagepath'; $page->save(); }` – The Alpha Jun 08 '14 at 18:58
  • I do have the image field, don't i need to mention all other fields in order to successfully save my page? – Alexander Kim Jun 08 '14 at 18:59
  • No, you don't need to mention all the fields, only that one you are trying to update. – The Alpha Jun 08 '14 at 18:59
  • With findOrFail() i am getting: "No query results for model [Page]". But i have the page with id 1. Mystic lol – Alexander Kim Jun 08 '14 at 19:04
  • 1
    Well, its an issue with my ajax request again. Thanks anyway :) You're my saver. – Alexander Kim Jun 08 '14 at 19:09
24

I tried to update a field with

$table->update(['field' => 'val']);

But it wasn't working, i had to modify my table Model to authorize this field to be edited : add 'field' in the array "protected $fillable"

Hope it will help someone :)

S. Guy
  • 241
  • 2
  • 5
  • It did, thank you! I knew there was a one-liner for easy model updating, but this was reporting 'true' in tinker but not updating the db. Thank you. :) – Vaughany Apr 03 '19 at 16:38
  • also have to put the column name in to fillable . Also sometimes if the type of the value doesnt match one in db it wont through exception at runtime. Just nothing. E.g. from js ajax if there is a number to be sent it comes up as string. E.g. "0". So that double check types – CodeToLife Mar 14 '21 at 12:02
8

Version 1:

// Update data of question values with $data from formulay
$Q1 = Question::find($id);
$Q1->fill($data);
$Q1->push();

Version 2:

$Q1 = Question::find($id);
$Q1->field = 'YOUR TEXT OR VALUE';
$Q1->save();

In case of answered question you can use them:

$page = Page::find($id);
$page2update = $page->where('image', $path);
$page2update->image = 'IMGVALUE';
$page2update->save();
Cubiczx
  • 1,005
  • 11
  • 10
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Mar 02 '18 at 11:08
  • It gets then saves the entire model, so it may race for concurrent http queries – odiszapc Nov 18 '21 at 00:45
4

Try this method short and clean :

Page::where('id', $id)
  ->update(['image' => $path]);

An example from Laravel doc

Flight::where('active', 1)
      ->where('destination', 'San Diego')
      ->update(['delayed' => 1]);
Rachid Loukili
  • 623
  • 6
  • 15
-2
       DB::table('agents')
       ->where('id', $agentid)
       ->update(['status' => '0']);
  • 1
    See "[answer]" and [Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. – the Tin Man Nov 28 '22 at 19:36