请问我该如何解决?
下面是我的代码。
laravel控制器:
public function postCreateTicket (Request $request)
{
$validate = $this->validate($request, [
'subject' => 'required',
'message' => 'required'
]);
if ($validate)
{
$new_message = TicketMessage::create([
'ticket_subject_id' => $request->subject,
'investor_id' => $request->$this->investorID(),
'message' => $request->message,
]);
$send_status = $new_message->save();
if($send_status)
{
$message = 'Your ticket was successfully sent! We will get back to you in no time.';
return response()->json($message);
}else{
echo 'Error1';
}
}
}
HTML代码:
<form method="post" id="form-data">
@csrf()
<div class="form-group text-left x3-margin-top">
<label for="subject" class="grey normal x4-margin-left">Subject</label>
<select name="subject" id="subject" class="form-control input-lg x4-margin-left x9-width no-radius input grey">
<option value="" selected>Select Subject</option>
@foreach($ticket_subject as $ticket_subjects)
<option value="{{$ticket_subjects->id}}">{{strtoupper($ticket_subjects->subject)}}</option>
@endforeach
</select>
@if($errors->has('subject'))
<span class=" x4-margin-left x10-font-size maroon">{{$errors->first('subject')}}</span>
@endif
</div>
<div class="form-group text-left x3-margin-top">
<label for="message" id="message-label" class="grey normal x4-margin-left">Message</label>
<textarea name="message" id="message" cols="30" rows="15" class="form-control x4-margin-left x9-width"></textarea>
@if($errors->has('message'))
<span class="x4-margin-left x10-font-size maroon">{{$errors->first('message')}} </span>
@endif
</div>
<div class="form-group text-right x4-margin-top x6-margin-right">
<button class="btn btn-info btn-lg no-radius" id="submit" type="button">
Send Ticket <i class="fas fa-mail-bulk"></i>
</button>
</div>
</form>
Ajax代码:
<script type="text/javascript">
$(document).on('click', '#submit', function (e){
$.ajax({
type:'POST',
url : "{{route('ticket')}}",
data : {
_token: "{{csrf_token()}}",
ticket_subject_id: $('select[name=subject]').val(),
investor_id: "{{Auth::user()->investor_id}}",
message: $('textarea[name=message]').val()
},
dataType: 'text',
beforeSend: function(){$('#message-label').text('Sending..')},
success : function(data)
{
$('#message-label').text('Sent!');
$('.subject').text('');
$('.message').text('');
},
errors : function(data)
{
alert('failed');
}
});
});
</script>
路线:
Route::get('/investor/dashboard/ticket/create/', array(
'as' => 'ticket',
'uses' => 'InvestorTicketController@getCreateTicket'
));
Route::post('/investor/dashboard/ticket/create/', [
'as' => 'ticket',
'uses' => 'InvestorTicketController@postCreateTicket'
]);
答案 0 :(得分:0)
更新模型
Future<? extends Response> future = service.apply(request).toJavaFuture();
Future<Response> futureResponse = (Future<Response>) future;
您必须插入所有字段<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class TicketMessage extends Model {
protected $fillable = [
'ticket_subject_id',
'investor_id',
'message'
];
public function ticket_subject () {
return $this->belongsTo('App\TicketSubject', 'ticket_subject_id');
}
public function ticket_response () {
return $this->hasMany('App\TicketResponse');
}
}
数组。
我可以建议一个更好的功能
protected $fillable
Ajax代码
public function postCreateTicket (Request $request)
{
try {
// Validate stop the execution is fields are empty
$validate = $this->validate($request, [
'ticket_subject_id' => 'required',
'message' => 'required'
]);
$new_message = TicketMessage::create([
'ticket_subject_id' => $request->ticket_subject_id,
'investor_id' => Auth::user()->investor_id,
'message' => $request->message,
]);
if (!empty($new_message)){
$message = 'Your ticket was successfully sent! We will get back to you in no time.';
return response()->json(['message' => $message]);
}else{
return response()->json([
"error" => "Ticket message didn't create",
]);
}
}catch (\Exception $e) {
// If there is any error it return a error response
return response()->json([
"error" => $e->getMessage(),
]);
}
}