Insert query in Laravel 5 using Model

Let see How to insert data in to database using Model in Laravel. You may also generate Eloquent models using the make:model command: php artisan make:model

Here is sample code that you can use.

Model in your controllers.

use App\SearchLog;

Insert data to database table using model.

$searchlog = new SearchLog;
$searchlog->user_id = Auth::user()->id;
$searchlog->min_carat = $request->min_carat;
$searchlog->max_carat = $request->max_carat;
$searchlog->min_depth = $request->min_depth;
$searchlog->max_depth = $request->max_depth;
$searchlog->min_price = $request->min_price;
$searchlog->max_price = $request->max_price;
if(!empty($request->shape))
{	
	$searchlog->shape = $request->shape;
}
if(!empty($request->color))
{	
	$searchlog->color = $request->color;
}
if(!empty($request->clarity))
{	
	$searchlog->clarity = $request->clarity;
}
if(!empty($request->cut))
{	
	$searchlog->cut = $request->cut;
}
if(!empty($request->polish))
{	
	$searchlog->polish = $request->polish;
}
if(!empty($request->symmetry))
{	
	$searchlog->symmetry = $request->symmetry;
}
if(!empty($request->flourescence))
{	
	$searchlog->flourescence = $request->flourescence;
}
$searchlog->save();

Leave a Reply

Your email address will not be published. Required fields are marked *