laravel 5 update in database -
i created row in 'movies' table called 'slug' want store slug of titles of movies.the problem have 250 movies , don't want manually enter slug each 1 of them trying make script automatically update slugs.but failed that:
this code in filmcontroller.php:
class filmcontroller extends controller { public function film() { $title = db::table('movies')->select('title'); $slug = str_replace(" ","-",$title); db::table('movies')->update(array('slug' => $slug)); } }
and in routes:
route::get('update','filmcontroller@film');
this error pops when go localhost/update:
object of class illuminate\database\query\builder not converted string
can tell me how change code can put in slug field in movies title of movie '-' insted of space between words?
$title object containing titles, , not string,so str_replace not work on can try using loop update each record according title
something like
$titles = db::table('movies')->select('title','id')->get(); foreach ($titles $title){ $slug = str_replace(" ","-",$title->title); db::table('movies')->where('id',$title->id)->update(array('slug' => $slug)); }
Comments
Post a Comment