I'm trying to get the ticket data to save in the database and when I submit the form I get no error, but it does not insert the data into the database. Added my Route just adding random text because the post
Controller Code
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'title' => 'required|min:15',
'category' => 'required',
'priority' => 'required',
'message' => 'required|min:100',
]);
$ticket = new Ticket([
'user_id' => Auth::user()->id,
'category_id' => $request->input('category'),
'ticket_id' => strtoupper(str_random(10)),
'name' => $request->input('name'),
'title' => $request->input('title'),
'priority_id' => $request->input('priority'),
'message' => $request->input('message'),
]);
$ticket->status_id = '1';
$ticket->save();
return 'Success';
}
Model Code
class Ticket extends Model
{
protected $fillable = [
'user_id', 'category_id', 'ticket_id', 'name', 'title', 'priority_id', 'message', 'status_id',
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function priority()
{
return $this->belongsTo(Priority::class);
}
public function status()
{
return $this->belongsTo(Status::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
Routes
Route::get('/', 'HomeController#index')->name('home');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/compliance', 'HomeController#Compliance')->name('compliance');
Route::get('/messages', 'HomeController#Messages')->name('messages');
Route::get('/tickets', 'TicketsController#userTickets')->name('tickets');
Route::get('/tickets/create', 'TicketsController#create')->name('tickets/create');
Route::post('/tickets/store', 'TicketsController#store');
Thanks for all the help I figured it I had two mistakes in the controller this took me 2 days to figure it out LOL
$this->validate($request, [
'name' => 'required', // I removed this
'title' => 'required|min:15',
'category' => 'required',
'priority' => 'required',
'message' => 'required|min:100',
]);
and in the $ticket
$ticket = new Ticket([
'user_id' => Auth::user()->id,
'category_id' => $request->input('category'),
'ticket_id' => strtoupper(str_random(10)),
'name' => $request->input('name') // I replaced this with 'name' => Auth::user()->name,
'title' => $request->input('title'),
'priority_id' => $request->input('priority'),
'message' => $request->input('message'),
]);
Related
i'am newbie and i need help..
I have created_by and updated_by field in my post database, how to fill that fields automatically after we add new post?
Here's my database:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('post_title', 60);
$table->string('post_slug')->unique();
$table->integer('post_seq');
$table->string('post_thumbnail');
$table->string('post_excerpt', 500);
$table->text('post_description');
$table->string('post_meta_title');
$table->string('post_meta_description');
$table->string('post_meta_keyword');
$table->boolean('is_active')->default(false);
$table->timestamps();
$table->string('created_by')->nullable();
$table->string('updated_by')->nullable();
});
Here's my PostController
public function store(Request $request)
{
$request->validate([
'post_title' => 'required|string|max:60',
'post_slug' => 'required|string|unique:posts,post_slug',
'post_thumbnail' => 'required',
'post_excerpt' => 'required|string|max:500',
'post_seq' => 'required',
'post_description' => 'required|string',
'post_meta_title' => 'required|string|max:60',
'post_meta_description' => 'required|string|max:250',
'post_meta_keyword' => 'required|string',
'is_active' => 'required'
]);
Post::create([
'post_title' => $request->post_title,
'post_slug' => $request->post_slug,
'post_seq' => $request->post_seq,
'post_thumbnail' => parse_url($request->post_thumbnail)['path'],
'post_excerpt' => $request->post_excerpt,
'post_description' => $request->post_description,
'post_meta_title' => $request->post_meta_title,
'post_meta_description' => $request->post_meta_description,
'post_meta_keyword' => $request->post_meta_keyword,
'created_by' => User::where('name')->get(),
'is_active' => $request->boolean('is_active'),
'user_id' => $request->user_id
]);
Alert::success('Add Post', 'Added Post Success');
// dd($request->all());
return redirect()->route('posts.index');
}
Here's my post model
class Post extends Model
{
use HasFactory;
protected $table = 'posts';
protected $fillable = ['post_title', 'post_slug', 'post_thumbnail', 'post_seq', 'post_excerpt', 'post_description', 'post_meta_title', 'post_meta_description', 'post_meta_keyword', 'is-active', 'created_by'];
public function scopeSearch($query, $title)
{
return $query->where('post_title', 'LIKE', "%{$title}%");
}
}
Please, i need help..
try this:
$model = new Model();
$model->created_at = Carbon::now();
$model->save(['timestamps' => false]);
I am trying to update the title as a single database task entry. My function currently updates the titles of all the tasks in the table.
public function update(User $user, Task $task, Request $request) {
$data = $this->validate($request, [
'title' => 'required|string|max:255',
]);
Auth::user()->tasks()->update([
'title' => $data['title'],
'is_complete' => 0,
'updated_at' => '2020-01-30 10:39:33',
'created_at' => '2020-01-30 10:39:33'
]);
session()->flash('status', 'Task Completed!');
return redirect('/profile/' . auth()->user()->id);
}
Use where('id', $task->id) on Task eloquent builder:
Auth::user()->tasks()->where('id', $task->id)->update([
'title' => $data['title'],
'is_complete' => 0,
'updated_at' => '2020-01-30 10:39:33',
'created_at' => '2020-01-30 10:39:33'
])
Try this
public function update(User $user, Task $task, Request $request)
{
$data = $this->validate($request, [
'title' => 'required|string|max:255',
]);
Auth::user()->tasks()->find($task->id)->update([
'title' => $data['title'],
'is_complete' => 0,
'updated_at' => '2020-01-30 10:39:33',
'created_at' => '2020-01-30 10:39:33',
]);
session()->flash('status', 'Task Completed!');
return redirect('/profile/' . auth()->user()->id);
}
add find($task->id)
I have the method store() to create a new registration type for the conference. But it appears an error "BadMethodCallException
Method [validateEquired] does not exist." in the file "Illuminate/Validation/Validator.php".
Do you know why?
public function store(Request $request, $id)
{
$rules = [
'registration_type_name' => 'required',
'registration_type_capacity' => 'required|integer|min:0',
'registration_type_price' => 'required|integer|min:0',
'registration_type_minimum' => 'equired|integer|min:1',
'registration_type_maximum' => 'rgt:registration_type_minimum|required|integer|min:1',
];
$customMessages = [
'registration_type_name.required' => 'The field name is rqeuired.',
'registration_type_capacity.integer' => 'The field capacity needs to be a positive integer.',
....
];
$this->validate($request, $rules, $customMessages);
$conference = Conference::find($id);
RegistrationType::create([
'name' => $request->registration_type_name,
'description' => $request->registration_type_description,
'capacity' => $request->registration_type_capacity,
'price' => $request->registration_type_price,
'min_participants' => $request->registration_type_minimum,
'max_participants' => $request->registration_type_maximum,
'conference_id' => $conference->id
]);
Session::flash('success', 'Registration type created.');
return redirect()->back();
}
You're missing an r here:
'registration_type_minimum' => 'equired|integer|min:1',
^
I can't seem to find the variable itinerary from my update function in my controller. What seems to be the problem for this?
Error says: Undefined variable: itinerary (View: C:\xampp\htdocs\project_name\resources\views\Agent\edit.blade.php)
AgentsController update function
public function update(Request $request, $p_id){
$this->validate($request, [
'packageName' => 'required',
'adultPrice' => 'required',
'childPrice' => 'required',
'infantPrice' => 'required',
'excessPrice' => 'required',
'type' => 'required',
'inclusions' => 'required',
'additionalInfo' => 'required',
'reminders' => 'required',
'photo' => 'required',
'tags' => 'required',
'noOfDays' => 'required',
'day' => 'required',
'time' => 'required',
'destination' => 'required'
]);
$packages = Packages::find($p_id);
$packages->packageName = $request->input('packageName');
$packages->adultPrice = $request->input('adultPrice');
$packages->childPrice = $request->input('childPrice');
$packages->infantPrice = $request->input('infantPrice');
$packages->excessPrice = $request->input('excessPrice');
$packages->type = $request->input('type');
$packages->inclusions = $request->input('inclusions');
$packages->additionalInfo = $request->input('additionalInfo');
$packages->reminders = $request->input('reminders');
$packages->photo = $request->input('photo');
$packages->tags = $request->input('tags');
$packages->save();
$itinerary = Itinerary::find($p_id);
if($itinerary->p_id == $packages->p_id){
$itinerary->noOfDays = $request->input('noOfDays');
$itinerary->day = $request->input('day');
$itinerary->time = $request->input('time');
$itinerary->destination = $request->input('destination');
$itinerary->save();
return redirect('Agent/Packages')->with('success', 'Updated');
}
}
edit.blade.php where the error is located
{{!!Form::open(array('class' => 'form-horizontal', 'method' => 'post', 'action' => array('AgentsController#update', $packages->p_id, $itinerary->p_id)))!!}}
Itinerary.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Itinerary extends Model{
public $fillable = [
'p_id',
'noOfDays',
'day',
'time',
'destination'
];
protected $primaryKey = 'i_id';
}
?>
Edit function
public function edit($p_id){
$packages = Packages::find($p_id);
$itinerary = Itinerary::find($p_id);
return View::make('\Agent\Edit', ['packages' => $packages, 'itineraries' => $itinerary]);
}
The way you pass the data is wrong.
Documentation
There are few ways you can pass. Examples :
1 -
return View::make('Agent.edit', ['packages' => $packages, 'itinerary' => $itinerary]);
2 - return View::make('Agent.edit')->with(['packages' => $packages, 'itinerary' => $itinerary]);
3 - return View::make('Agent.edit')->with('packages', $packages)->with('itinerary', $itinerary);
In your edit.blade.php, try remplacing your $itinerary variable by $itineraries (plural).
I'm using form requests class. Work fine:
class EventFormRequest extends FormRequest
{
public function rules()
{
return [
'event' => 'required|min:10|max:255',
'event_description' => 'required|min:3|max:255',
'url' => 'url',
'date' => 'required|date',
'start_time' => 'required',
'location.street' => 'required|max:255',
'location.house_number' => 'required|min:1|max:5',
'location.place' => 'required|max:255'
];
}
}
But now, I have to add complexer rules, such as combined with Validator. Below the new rules() method of my EventFormRequest class:
public function rules()
{
$v = \Validator::make($this->request->all(),
[
'event' => 'required|min:10|max:255',
'event_description' => 'required|min:3|max:255',
'url' => 'url',
'date' => 'required|date',
'start_time' => 'required',
'location.street' => 'required|max:255',
'location.house_number' => 'required|min:1|max:5',
'location.place' => 'required|max:255'
]);
$v->sometimes('category_id', 'required|numeric', function($input) {
return $input->event_type == 'known';
});
return ($v->fails() ? $v->messages() : []); // validator validates the rules, but returns the messages
}
You see, category_id is required if the event type is 'known'. In the form request rules() method, I cannot return the applied rules as array (see example 1) from the validator, but only the messages().
I'm inspired from here: http://laravel.com/docs/5.1/validation#conditionally-adding-rules
class EventFormRequest extends FormRequest
{
public function rules()
{
$rules = [
'event' => 'required|min:10|max:255',
'event_description' => 'required|min:3|max:255',
'url' => 'url',
'date' => 'required|date',
'start_time' => 'required',
'location.street' => 'required|max:255',
'location.house_number' => 'required|min:1|max:5',
'location.place' => 'required|max:255'
];
if ($this->request->get('event_type') == 'known') {
$rules['category_id'] = 'required|numeric';
}
return $rules;
}
}