Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update column value in laravel

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?

like image 707
Alexander Kim Avatar asked Jun 08 '14 18:06

Alexander Kim


People also ask

How do you UPDATE a record in laravel?

We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.

How do I change the value of one column to another column?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

How do you UPDATE a column based on another column in SQL?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.


4 Answers

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.

like image 101
The Alpha Avatar answered Oct 11 '22 11:10

The Alpha


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 :)

like image 20
S. Guy Avatar answered Oct 11 '22 11:10

S. Guy


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();
like image 8
Cubiczx Avatar answered Oct 11 '22 11:10

Cubiczx


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]);
like image 3
Rachid Loukili Avatar answered Oct 11 '22 09:10

Rachid Loukili