Laravel : Data not inserted on the table
Facing a problem on submit form data.
When I fill up all form data then it is inserted into the database. But when I fill only mandatory field data and leave other data as blank it is not working and it redirect to the same form.
On removing validation also not working.
My controller function code:
public function save(Request $request) {
try {
$validator = Validator::make($request->all(), Activity::rules());
$activity = Activity::saveOrUpdate($request);
if($activity !== false) {
return redirect()->route('lists-activity')->with('success', trans('activity data added successfully.!!'));
} else {
return back()->with('error', "Unable to save activity data.!!")->withInput();
}
} catch (Exception $ex) {
return back()->with('error', "Unable to save activity data.!!")->withInput()->withErrors($validator);
}
}
My model code :
namespace App;
use IlluminateHttpRequest;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesInput;
class Activity extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'category_id',
'title',
'description',
'country_id',
'city_id',
'latitude',
'longitude',
'addressOne',
'addressTwo',
'hours_recomended',
'hours_fixed',
'time_fixed',
'start_time',
'end_time',
'file_type',
'flag_image'
];
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
public static function rules() {
return [
'category_id' => 'required',
'title' => 'required|string|max:255',
'country_id' => 'required',
'city_id' => 'required',
'hours_fixed' => 'required',
'start_time' => 'required',
'end_time' => 'required'
];
}
public static function saveOrUpdate(Request $request) {
try {
$id = $request->get('id', false);
$activity = false;
DB::transaction(function () use ($request, &$activity, $id) {
$activity = $id ? Activity::findOrFail($id) : new Activity();
$activity->fill($request->all());
try {
$activity->save();
} catch (Exception $ex) {
throw $ex;
}
});
return $activity;
} catch (Exception $ex) {
throw $ex;
}
} }
Form view :
Don’t know what I am doing wrong?
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-data-not-inserted-on-the-table/
via Lzo Media
No comments:
Post a Comment