Skip to content

Instantly share code, notes, and snippets.

@moradi-morteza
Last active December 22, 2019 17:54
Show Gist options
  • Save moradi-morteza/3c9c1ddbe0716f55492aa5a2dbb41ff7 to your computer and use it in GitHub Desktop.
Save moradi-morteza/3c9c1ddbe0716f55492aa5a2dbb41ff7 to your computer and use it in GitHub Desktop.
[model] #LaravelT
$post = new Post();
$post->title = "hello";
$post->des = "des"
$post->save();
// if send extra data that is not in table it give you error
//---------------------------
// if you set fillable variable in model then send extra data it just get needed data if you use :
$post = Post::create(
[
'title'=>'hello',
'des' => 'des',
'extra'=>'extra'
]
);
class Post extends Model {
protected $fillable = ['title','des']; // this parameter should be filled by me
}
// search ---------------------------
$post=Post::find(2);
// update ---------------------------
$post=Post::find(2);
$post->title = 'new title';
$post->save();
//or
$answer= Post::where('id',1)->update(['body'=>'new body']); //return 0 or 1
// delete -------------------------
$post=Post::find(1);
// first check if finded or not!
if($post){$post->delete();}
// another way without error
$count_deleted=Post::where('id',2)->delete(); // return 1 if delete and return 0 if not exist or can not delete
$count_deleted=Post::destroy(3); // this method input is id
$count_deleted=Post::destroy([5,3,5,1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment