i having troubles with my migration field default('No description'), i need to save a default value for my null fields on my form, but when i save, my form data stores empty fields.. why? i'm working with Laravel 5.2 and it is my code:
public function up()
{
Schema::create('combos', function (Blueprint $table) {
$table->increments('id');
$table->string('item_name');
$table->string('description')->nullable()->default('No description');
$table->decimal('price', 5, 2);
$table->decimal('buy_price', 5, 2);
$table->timestamps();
});
}
My view:
<div class="form-group">
{!! Form::label('item_name','Item: ',['class'=>'control-label col-md-2']) !!}
<div class="col-md-7" >
{!! Form::text('item_name',null,['class'=>'form-control','placeholder'=>'Enter a item name','required','min'=>5]) !!}<br/>
</div>
</div>
<div class="form-group">
{!! Form::label('price','Price: ',['class'=>'control-label col-md-2']) !!}
<div class="col-md-7" >
{!! Form::text('buy_price',null,['class'=>'form-control','placeholder'=>'Enter a price','required']) !!}<br/>
</div>
</div>
in your store function do this
$input = $request->all();
$input['description'] = $request->description;
Combos::create($input);
Related
So I have a ToDo List and really strange problem.
So I have a form where user writes the Title and task description, task description is done as textarea, after that the user presses the submit button and submits the Task, which then redirects user to the "Home page" and displays all pending tasks.
The problem is when user presses "ENTER" inside the texarea the text "breaks" and doesn't align with the HTML div tag.( As shown in the picture below (TASK 3 and TASK 2))
This is my "Display all tasks" code
> #foreach ($tasks as $task)
> <div class="Task">
> <h1>{{ $task->TaskName }}</h1>
> <div class="TaskDescription">
> <p>{{ $task->TaskDescription }}</p>
> </div>
> </div>
> #endforeach
,this is my "Create new task" form
<div class="FormContainer">
<form method="POST" action="/tasks" class="form_group_vertical">
#csrf
<input name="TaskName" placeholder="Task name..." class="lineInput">
<textarea name="TaskDescription" placeholder="Task description..." class="Form_TextArea"></textarea>
<button class="btn_submit" type="submit">Create new Task</button>
</form>
and this is how I store all my tasks
public function storeTasks(){
// return request()->all(); data displayed in JSON
Task::create([
'TaskName' => request('TaskName'),
'TaskDescription' => request('TaskDescription')
]);
return redirect('/tasks');
}
and this is my migration up function
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('TaskName');
$table->text('TaskDescription');
$table->timestamps();
});
}
So far I had tried, changing how the data is saved,
tried replacing the {{ }} with {!! !!}, tried changing databases, and tried completely rewriting the form - I couldn't find any fix with this aproach.
Use nl2br
#foreach ($tasks as $task)
<div class="Task">
<h1>{{ $task->TaskName }}</h1>
<div class="TaskDescription">
<!-- Change here -->
<p>{!! nl2br(trim($task->TaskDescription)) !!}</p>
<!-- OR -->
<p>{!! str_replace(array("\r\n", "\r", "\n"), "<br />", $task->TaskDescription) !!}</p>
</div>
</div>
#endforeach
Having the textarea inside <pre> works too.
SOLUTION !!!
To fix my issue I had to replace the $table->text('TaskDescription') and change it into $table->longText('TaskDescription')
Replaced text with longtext
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('TaskName');
$table->longText('TaskDescription');
$table->timestamps();
});
}
I'm using the laravel-users package to add user management functionality to my Laravel 5.7 application but I'm struggling to figure out how to add additional fields to the user create / user edit forms so that they match my user table schema.
Users table definition:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number', 13);
$table->boolean('is_super_admin')->default(false);
$table->rememberToken();
$table->timestamps();
});
Specifically I want to add the phone_number field to the forms. I've updated the blade template adding in the markup for the field:
<div class="form-group has-feedback row {{ $errors->has('phone_number') ? ' has-error ' : '' }}">
#if(config('laravelusers.fontAwesomeEnabled'))
{!! Form::label('phone_number', __('auth.phone_number'), array('class' => 'col-md-3 control-label')); !!}
#endif
<div class="col-md-9">
<div class="input-group">
{!! Form::text('phone_number', NULL, array('id' => 'phone_number', 'class' => 'form-control', 'placeholder' => __('auth.phone_number'))) !!}
<div class="input-group-append">
<label class="input-group-text" for="phone_number">
#if(config('laravelusers.fontAwesomeEnabled'))
<i class="fa fa-fw {!! __('laravelusers::forms.create_user_icon_username') !!}" aria-hidden="true"></i>
#else
{!! __('auth.phone_number') !!}
#endif
</label>
</div>
</div>
#if ($errors->has('phone_number'))
<span class="help-block">
<strong>{{ $errors->first('phone_number') }}</strong>
</span>
#endif
</div>
</div>
But it seems that the actual creation of the user is handled by the UsersManagementController that's part of the package itself. How can I override this so that I can have it store the new field I've added?
Without reading in detail the full explanation (since I have currently little reputation on the present website to just post a few comments to clarify the question, rather than an attempt an answer...)
here is some advice ;)
You probably need to go into the UsersManagementController and edit the update function to allow to save the newly added field "phone_number".
Most likely your controller would be located in:
app/Http/Controllers/UsersManagementController.php
the function you need to edit will look something like this
public function update(Request $request, $id) {
// Find the User that needs to be updated
$user = Post::find($id);
$user->phone_number = $request->input('phone_number');
//... some more code here
$user->save();
return //whatever is returned here... potentially redirected
}
To find all list of routes, in case this is the issue and consecutively adapt your form post to submit the data to the correct controller (this will list all you currently managed routes):
php artisan route:list
If there is no controller you should create one and make sure to add it into the:
routes/web.php
Hope this helps
I'm looking for solution to use dropzoneJs in laravel 5.5 while writing my products.
What I want to do is: upload my images and fill my product info then save all together just like WordPress/Joomla etc.
What do I have so far:
Images Table
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('image');
$table->integer('product_id')->nullable()->unsigned();
$table->timestamps();
});
Schema::table('images', function($table) {
$table->foreign('product_id')->references('id')->on('products');
});
}
My form in Products create.blade.php
<div class="row">
<div class="col-md-12">
{!! Form::open([ 'route' => [ 'dropzone.store' ], 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone', 'id' => 'image-upload' ]) !!}
{{ csrf_field() }}
<div>
<h4 style="text-align: center;color:#428bca;">Drop images in this area <span class="glyphicon glyphicon-hand-down"></span></h4>
<!-- Input code below Not working yet -->
<input type="text" name="product_id" value="" hidden>
</div>
{!! Form::close() !!}
</div>
</div>
My JavaScript code:
<!-- drozone -->
<script type="text/javascript">
Dropzone.options.imageUpload = {
maxFilesize: 5, //MB
acceptedFiles: ".jpeg,.jpg,.png,.gif"
};
</script>
My ImageController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Image;
use App\Product;
class ImageController extends Controller
{
public function dropzone()
{
return view('dropzone-view');
}
public function dropzoneStore(Request $request)
{
$image = $request->file('file');
$imageName = time().$image->getClientOriginalName();
$image->move(public_path('images'),$imageName);
return response()->json(['success'=>$imageName]);
}
}
And my routes:
Route::get('dropzone', 'ImageController#dropzone');
Route::post('dropzone/store', ['as'=>'dropzone.store','uses'=>'ImageController#dropzoneStore']);
Any Idea On That?
I have 2 models with Many-to-Many Relation when I create new Device and check multiple Checkboxes I want to save multiple voices_id with Device_id into the Pivot Table device_voice
Device Controller ( I am facing this error Undefined variable: selectedVoice in Edit function )
public function create()
{
$this->authorize("create", Device::class);
$voice_list = Voice::all();
return view('devices.create')
->with('voice_list', $voice_list);
}
public function store(CreateDeviceRequest $request)
{
$this->authorize("create", Device::class);
$input = $request->all();
$voices_checkbox = $input['voice'];
$device = $this->deviceRepository->create($input);
//Device Id and Selected voices is Saved Successfully
$device ->voices()->sync($voices_checkbox);
Flash::success('Device saved successfully.');
return redirect(route('devices.index'));
}
public function edit($id)
{
$device = $this->deviceRepository->findWithoutFail($id);
$this->authorize("update", $device);
$voice_list = Voice::all();
// Here I am getting Device's checked voices, but sinces I am sharing the same Fields Blade with Create and Edit I get this error
//Undefined variable: selectedVoice
$selectedVoice = $device->voices->pluck("id")->toArray();
if (empty($device)) {
Flash::error('Device not found');
return redirect(route('devices.index'));
}
return view('devices.edit')
->with('device', $device)
->with('voice_list', $voice_list)
->with('selectedVoice', $selectedVoice);
}
This is My Fields for Both Create and Edit Views
<!-- Device Number Field -->
<div class="form-group col-sm-6">
{!! Form::label('device_number', 'Device Number:') !!}
{!! Form::text('device_number', null) !!}
</div>
<!-- Batch Field -->
<div class="form-group col-sm-6">
{!! Form::label('device_name', 'Device Name:') !!}
{!! Form::text('device_name', null) !!}
</div>
<!-- Batch Field -->
<div class="form-group col-sm-6">
{!! Form::label('voices_id', 'Voices:') !!}
#foreach ($voice_list as $voice)
{!! Form::checkbox('voice[]', $voice->id, in_array($voice->id, $selectedVoice)) !!}
{!! Form::label('voice', $voice->name) !!}
#endforeach
</div>
<!-- Version Field -->
<div class="form-group col-sm-6">
{!! Form::label('version', 'Version:') !!}
{!! Form::text('version', null) !!}
</div>
Thanks!
Use the sync() method:
$device->voices()->sync($request->voices);
Just want to start by saying I have no clue what I'm doing...
I have a user_info table that looks like this
Schema::create('user_info', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('address');
$table->string('city');
$table->string('state');
$table->string('zip');
$table->text('description');
$table->text('experience');
$table->timestamps();
});
I'm having trouble creating the update controller which looks like this right now.
public function update(Request $request)
{
$user = $request->user();
$data['description'] = $request->input('description');
$data['experience']=$request->input('experience');
$user->user_info -> $data->save();
}
again...no clue what I'm doing...
and this be my form:
<div class='col-md-10 well form-well'>
{!! Form::open(['method' => 'PATCH', 'action'=> ['UserController#update', Request::user()->id]]) !!}
<div class='row'>
<div class='form-group'>
<div class='col-md-2'>
{!! Form::label('description', 'About You')!!}
</div>
<div class='col-md-7'>
{!! Form::textarea('description', null, ['class'=>'form-control', 'rows'=>'3'])!!}
</div>
</div>
</div>
<div class='row'>
<div class='form-group'>
<div class='col-md-2'>
{!! Form::label('experience', 'Experience and Skills')!!}
</div>
<div class='col-md-7'>
{!! Form::text('experience', null, ['class'=>'form-control'])!!}
</div>
</div>
</div>
<div class='form-group'>
{!! Form::submit('Save Changes',['class'=> 'btn btn-md btn-success']) !!}
{!! Form::close()!!}
</div>
Update: I was able to update it like this:
$user->user_info->description = $data['description'];
$user->user_info->experience = $data['experience'];
$user->user_info->save();
But is there a way I can do something like :
$user->user_info->$request::Input::all();
$user->user_info->save();
Try this:
public function update(Request $request, $id)
{
$User = User::with('user_info')->find($id);
if(!$User) {
return response('User not found', 404);
}
$UserInfo = $User->user_info;
if(!$UserInfo) {
$UserInfo = new UserInfo();
$UserInfo->user_id = $id;
$UserInfo->save();
}
try {
$values = Input::only($UserInfo->getFillable());
$UserInfo->update($values);
} catch(Exception $ex) {
return response($ex->getMessage(), 400);
}
}
also in Your UserInfo model add this:
protected $fillable = array('description', 'experience');
public function getFillable() {
return $this->fillable;
}