Eloquent Flashcards
First thing before CRUD eloquent in laravel?
declare protected $fillable=[ …] in Model
Before CRUD eloquent in Laravel,what do you have to do?
declare protected $fillable=[..] in Model.php file!!
Create Method
Example::: Post::create([ 'title'=>'post two', 'description'=>'post two desc' ]);
Read all or Select all
$posts= Post::all();
foreach ($posts as $post){
echo $post->title;
}
Select one
$post = Post::find(1);
echo $post;
Update Method
$post = Post::find(1)->update([
‘title’=>’post one one’,
‘description’=>’post oneeee desc’
]);
Delete Method
Post::destroy(3);
Get by limit number
$posts= Post::take(number)->get();
foreach ($posts as $post){
echo $post->title;
}
Get latest data
$posts= Post::latest->get();
foreach ($posts as $post){
echo $post->title;
}
inserting image
$image->$request->file(‘image’)
$name_gen=hexdec(uniqid( ));
$img_ext = strtolower($brand_image->GetClientOriginalExtension( ) );
$img_name = $name_gen . ‘ . ‘ . $img_ext;
$up_location =’/image/’;
$last_img = $up_location.$img_name;
$image->move($up_location,$img_name)
save last_image