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();
});
}
Related
I am new developer, and I seem to be stuck on handling arrays in laravel. I am using Laravel 8, and I cant seem to solve this situation.
I am building an internal recruitment site, where once a manager posts a job, employees will be able to apply to those specific jobs. I have defined the table in the database to have the "applicants" as an array consisting of the user_id's. However, I seem to not be able to add more than one array to it.
Below is my Recruitment Model
class Recruitment extends Model
{
use HasFactory;
protected $fillable = [
'title',
'salary',
'term_start',
'term_end',
'deadline',
'details',
'status',
'applicants',
];
public function user(){
return $this->belongsTo("\App\Models\User");
}
protected $casts = [
'applicants' => 'array'
];
}
Next is my migration (I am using text format, because the DB on the server is older and does not support json)
public function up()
{
Schema::create('recruitments', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->decimal('salary', 10, 2);
$table->date('term_start');
$table->date('term_end');
$table->date('deadline');
$table->longText('details');
$table->string('status');
$table->text('applicants')->nullable();
$table->timestamps();
});
}
Here is my blade
<div class="container">
<div class="row">
<div class="card col-sm-12 py-3">
<div class="card-header border d-flex justify-content-between align-items-center">
<h3 class="w-75">{{ $job->title }}</h3>
<div class="w-25">
<p class="my-0 my-0">Created at: <span class="text-info">{{ $job->created_at }}</span></p>
<p class="my-0 my-0">Last updated at: <span class="text-primary">{{ $job->updated_at }}</span></p>
</div>
</div>
<div class="card-body">
// display job details here
<form action="{{ route('add-applicant', ['id' => $job->id ]) }}" method="POST" class="col-sm-12 d-flex justify-content-center align-items-center">
#csrf
<input type="text" name="user_id" id="user_id" value="{{ Auth::user()->id }}" hidden>
<button type="submit" class="btn btn-success w-25">Apply</button>
</form>
</div>
</div>
</div>
</div>
and lastly my controller
public function addApplicant($id, Request $reqst){
$job = Recruitment::find($id);
$user[] = $reqst->user_id;
$job->applicants = $user;
$job->save();
return redirect()->back();
}
While this controller will be able to save an array, it unfortunately overwrites the already existing one (let's say a second user applied). When I try to use an array_push, it does nothing, and I still end up with just one value in the array.
Sorry this was a bit of a read, but I appreciate any help I cen get with this. Thanks
Try in
public function addApplicant($id, Request $reqst){
$job = Recruitment::find($id);
$job->applicants = $reqst->user_id;
$job->save();
return redirect()->back();
}
and in Recruitment model should have
public function user()
{
return $this->belongsTo(User::class, 'applicants');
}
and migration file
$table->integer('applicants')->nullable();
I'm working with Laravel 5.1
I have a template for a page that was previously working fine. I could hit my route and the page would be rendered as expected. At some point the page started just displaying the text contained in the blade template rather than rendering my page. To my knowledge nothing had changed in my routes file, controller or blade. Is there anything that is known to cause this to happen? I've tried updating permissions, using a different method to call the view and creating a new view file all together.
The route:
Route::group(['prefix' => '/admin'], function() {
Route::group(['prefix' => '/reactor-rules'], function() {
Route::get('/visual-rules/{track_id?}', [
'as' => 'broker_visual_reactor_rules',
'uses' => 'ReactorRulesController#visualRules'
]);
});
});
ReactorRulesController#visualRules:
public function visualRules(IReactorTrackDAO $reactorTrackDAO, $id = 0)
{
$export = new ReactorExport();
$tracks = $export->getReactorTracks();
if ($id == 0) {
$eventsArray = [];
}
else {
$eventsArray = $export->getArrayForTrack($id);
}
return $this->renderView('enterpriseBroker::reactor-rules.visual-rules', compact('eventsArray','tracks','reactorTrackDAO'));
}
My blade:
#extends($layout)
{{ SEO::setPageTitle(trans('enterpriseBroker::reactorRules.title'), false) }}
#section('content')
<br>
<br>
#if(empty($eventsArray))
<p>Please select a track below to view/export rules.</p>
#foreach($tracks as $track)
{{$track->name}}<br>
#endforeach
#else
#foreach($eventsArray as $track => $events)
<h4>{{$track}}</h4>
<a href="{{url().'/admin/visual-rules/'.$reactorTrackDAO->findByName($track)->id.'/download'}}" class="btn btn-primary" download>Export</a><br>
#foreach($events as $id => $event)
#if (count($event['rules']) > 0)
<div id="{{$track.'-'.str_replace(' ','_',$event['name'])}}">
<div class="col-lg-12">
<hr/>
<div class="col-lg-2">
<h4>{{$event['name']}}</h4>
</div>
<div class="col-lg-10 border border-primary">
#foreach($event['rules'] as $ruleId => $rule)
<h5>{{$rule['name']}}</h5>
<div class="col-lg-6">
<h6>Conditions to be met:</h6>
#foreach($rule['conditions'] as $condition)
<p>{{$condition}}</p>
#endforeach
</div>
<div class="col-lg-6">
<h6>Actions to run:</h6>
#foreach($rule['actions'] as $action)
<p>{!!$action!!}</p>
#endforeach
</div>
#endforeach
</div>
</div>
</div>
#endif
#endforeach
#endforeach
#endif
#endsection
#section('scripts')
#endsection
This is how it originally appeared:
This is how it currently appears:
It seems to be error with $layout
Try to test it using a hard coded value as below:
#extends('layouts.app')
and run below command
php artisan view:clear
I resolved the issue by creating a new blade file, pasting the contents of the old one in and pointing my controller to the new view. I'm not sure how this resolved the issue.
I am quite new to Laravel
I have two views
Book
Read
The Book View displays a single book
<section class="cont-readingone">
<div class="container">
<div class="row row-grid">
<div class="col-md-6">
<div class="row">
<div class="col-md-6">
<div class="cont-reading-image">
<img src="{{ $book->image_url }}" alt="trending image" />
</div>
</div>
<div class="col-md-6">
<div class="out-box">
<h2>{{ $book->name }}</h2>
<h3>{{ $book->author->name }}</h3>
<br>
Start Reading<br><br>
<img src="\images\cart-buy.png" width="13px"/> Buy
</div>
</div>
</div>
</div>
In my controller, I was able to achieve it using
public function show(Book $book) {
$relatedBooks = Book::where('author_id', $book->author_id)
->where('id', '!=', $book->id)
->get();
return view('book')->with('book', $book)->with('relatedBooks', $relatedBooks);
}
In my web.php
Route::get('/books/{book}', [BooksController::class, 'show'])->name('book');
What I am trying to achieve is that, when I click Start Reading on
the Single Book Page, it takes me to another view page (Read) but it takes the book id that I clicked.
In the Read View I have this code,
<script>
"use strict";
document.onreadystatechange = function () {
if (document.readyState == "complete") {
window.reader = ePubReader("{{ $book->epub_url }}", {
restore: true
});
}
};
</script>
My problem is that I don't know how to take the id of the book that I
click and Pass it to the Read View
I will be glad if someone can explain the logic to me as I am confused.
To do via POST
//Book View
//change Start Reading to
<form action="{{ route("your.route.to.read") }}" method="POST">
#csrf
<input name="book_id " value ={{$book->id}} hidden>
<button type="submit">Start Reading</button>
</form>
//your Route will be
Route::get('/read',YourReadController#yourFunction)->name('your.route.to.read');
//your controller will be
public function yourFunction(Request $request)
{
//book id is in $$request->book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
To do via GET
//Book View
//change Start Reading<br><br> to
Start Reading
//route for get will be
Route::get('/read/{book_id}',YourReadController#yourFunction)->name('your.route.to.read');
//your countroller will be
public function yourFunction($book_id)
{
//book id is in $book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
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 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);