How add custom field in laravel Voyager . How to save data ?
Does anyone uses the Laravel Voyager? So i have been working on the project that uses the Voyager in laravel for admin.. By default it has the pages table and it's BREAD/CRUD.
So I want to add the custom field so I have added overriden the blade file and add my custom field code.
After that i want to save that in database. I have created the another column in pages table.
I am now confused in the Voyager controller. How do i save my custom field value to the database pages table?
Here is the Controller Code Create/ Update Code..
public function create(Request $request)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Check permission
$this->authorize('add', app($dataType->model_name));
$dataTypeContent = (strlen($dataType->model_name) != 0)
? new $dataType->model_name()
: false;
foreach ($dataType->addRows as $key => $row) {
$dataType->addRows[$key]['col_width'] = $row->details->width ?? 100;
}
// If a column has a relationship associated with it, we do not want to show that field
$this->removeRelationshipField($dataType, 'add');
// Check if BREAD is Translatable
$isModelTranslatable = is_bread_translatable($dataTypeContent);
// Eagerload Relations
$this->eagerLoadRelations($dataTypeContent, $dataType, 'add', $isModelTranslatable);
$view = 'voyager::bread.edit-add';
if (view()->exists("voyager::$slug.edit-add")) {
$view = "voyager::$slug.edit-add";
}
return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable'));
}
/**
* POST BRE(A)D - Store data.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Check permission
$this->authorize('add', app($dataType->model_name));
// Validate fields with ajax
$val = $this->validateBread($request->all(), $dataType->addRows)->validate();
$data = $this->insertUpdateData($request, $slug, $dataType->addRows, new $dataType->model_name());
event(new BreadDataAdded($dataType, $data));
if (!$request->has('_tagging')) {
if (auth()->user()->can('browse', $data)) {
$redirect = redirect()->route("voyager.{$dataType->slug}.index");
} else {
$redirect = redirect()->back();
}
return $redirect->with([
'message' => __('voyager::generic.successfully_added_new')." {$dataType->getTranslatedAttribute('display_name_singular')}",
'alert-type' => 'success',
]);
} else {
return response()->json(['success' => true, 'data' => $data]);
}
}
public function edit(Request $request, $id)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
if (strlen($dataType->model_name) != 0) {
$model = app($dataType->model_name);
$query = $model->query();
// Use withTrashed() if model uses SoftDeletes and if toggle is selected
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$query = $query->withTrashed();
}
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$query = $query->{$dataType->scope}();
}
$dataTypeContent = call_user_func([$query, 'findOrFail'], $id);
} else {
// If Model doest exist, get data from table name
$dataTypeContent = DB::table($dataType->name)->where('id', $id)->first();
}
foreach ($dataType->editRows as $key => $row) {
$dataType->editRows[$key]['col_width'] = isset($row->details->width) ? $row->details->width : 100;
}
// If a column has a relationship associated with it, we do not want to show that field
$this->removeRelationshipField($dataType, 'edit');
// Check permission
$this->authorize('edit', $dataTypeContent);
// Check if BREAD is Translatable
$isModelTranslatable = is_bread_translatable($dataTypeContent);
// Eagerload Relations
$this->eagerLoadRelations($dataTypeContent, $dataType, 'edit', $isModelTranslatable);
$view = 'voyager::bread.edit-add';
if (view()->exists("voyager::$slug.edit-add")) {
$view = "voyager::$slug.edit-add";
}
return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable'));
}
// POST BR(E)AD
public function update(Request $request, $id)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Compatibility with Model binding.
$id = $id instanceof \Illuminate\Database\Eloquent\Model ? $id->{$id->getKeyName()} : $id;
$model = app($dataType->model_name);
$query = $model->query();
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$query = $query->{$dataType->scope}();
}
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$query = $query->withTrashed();
}
$data = $query->findOrFail($id);
// Check permission
$this->authorize('edit', $data);
// Validate fields with ajax
$val = $this->validateBread($request->all(), $dataType->editRows, $dataType->name, $id)->validate();
// Get fields with images to remove before updating and make a copy of $data
$to_remove = $dataType->editRows->where('type', 'image')
->filter(function ($item, $key) use ($request) {
return $request->hasFile($item->field);
});
$original_data = clone($data);
$this->insertUpdateData($request, $slug, $dataType->editRows, $data);
// Delete Images
$this->deleteBreadImages($original_data, $to_remove);
event(new BreadDataUpdated($dataType, $data));
if (auth()->user()->can('browse', app($dataType->model_name))) {
$redirect = redirect()->route("voyager.{$dataType->slug}.index");
} else {
$redirect = redirect()->back();
}
return $redirect->with([
'message' => __('voyager::generic.successfully_updated')." {$dataType->getTranslatedAttribute('display_name_singular')}",
'alert-type' => 'success',
]);
}
My custom field name is backgroundcolor. How can i save the custom field value in database table column as well along with this page data..
Currently I have checked via $request->all() it receives the values for the backgroundcolor field.
Please or to participate in this conversation.