我正在尝试处理表单以更新动物表中的数据,但是它没有显示任何错误,但是根本没有保存。
这是我的表格create.blade.php
@extends('layouts.app')
@section('title', 'Add Animal')
@section('content')
<div class="row">
<div class="col-12">
<h1>Farm</h1>
</div>
</div>
<h3>Welcome {{ $user->name }} Please Add an animal</h3>
<div class="row">
<div class="col-12">
<form action="/farm" method="POST">
<div class="form-group">
<label for="dateOfBirth">Date Of Birth: </label>
<input type="date" name="dateOfBirth" class="form-control" placeholder="dd/mm/yyyy">
</div>
<div class="pb-5">
{{ $errors->first('dateOfBirth') }}
</div>
<div class="form-group">
<label for="placeOfBirth">Place Of Birth</label>
<input type="text" name="placeOfBirth" class="form-control">
</div>
<div class="form-group">
<label for="gender">Gender: </label>
<select name="gender" class="form-control">
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Add Farm</button>
@csrf
</form>
</div>
</div>
@endsection
还有我的FarmController
<?php
namespace App\Http\Controllers;
use App\Animal;
use Auth;
use Illuminate\Http\Request;
class FarmController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$animal = Animal::all();
return view('farm.index', compact('animal'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$user = Auth::user();
$animal = new Animal();
return view('farm.create', compact('user', 'animal'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store()
{
$animal = Animal::create($this->validateRequest());
event(new NewAnimalRegisteredEvent($animal));
// Mail::to($customer->email)->send(new WelcomeNewUserMail());
return redirect('farm.show');
}
/**
* Display the specified resource.
*
* @param Animal $animal
* @return \Illuminate\Http\Response
*/
public function show(Animal $animal)
{
return view('farm.show', compact('animal'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
private function validateRequest()
{
return request()->validate([
'farmer_id' => 'required',
'dateOfBirth' => 'required|date',
'gender' => 'required',
'placeOfBirth' => 'required',
]);
}
}
我的动物模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Animal extends Model
{
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
public function clinicdetail(){
return $this->hasOne(ClinicDetail::class);
}
public function slaughterdetail(){
return $this->hasOne(SlaughterDetail::class);
}
}
用户模型
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'role_id',
];
public function role(){
return $this->belongsTo(Role::class);
}
public function animal(){
return $this->hasMany(Animal::class);
}
public function clinicdetail(){
return $this->hasMany(ClinicDetail::class);
}
public function slaughterdetail(){
return $this->hasMany(SlaughterDetail::class);
}
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
我的路线
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::middleware('admin')->group(function () {
// All your admin routes go here.
Route::resource('/admin', 'AdminController');
});
Route::middleware('farm')->group(function () {
// All your admin routes go here.
Route::resource('/farm', 'FarmController');
});
Route::middleware('clinic')->group(function () {
// All your admin routes go here.
Route::resource('/clinic', 'ClinicController');
});
Route::middleware('slaughter')->group(function () {
// All your admin routes go here.
Route::resource('/slaughter', 'SlaughterController');
});
Route::get('/home', 'HomeController@index')->name('home');
最后是我的活动
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewAnimalRegisteredEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $animal;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($animal)
{
$this->animal = $animal;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
请协助我。它在存储方法中带来了url分配,但没有保存动物
答案 0 :(得分:0)
遵循逻辑
public function store()
{
$animal = Animal::create($this->validateRequest());
event(new NewAnimalRegisteredEvent($animal));
// Mail::to($customer->email)->send(new WelcomeNewUserMail());
return redirect('farm.show');
}
是通过提交表单来调用的,并且正在调用一个事件。这是事件,但是没有必需的句柄功能:https://laravel.com/docs/5.8/events#defining-listeners
您的活动现在没有任何作用。似乎您尝试广播,但是它没有实现ShouldBroadcast
,因此您还应该检查是否符合all the requirements for broadcasting。我希望这会有所帮助,也许应该是评论,但是文本太长了,无法评论。提供更多信息后,我可以根据需要编辑此答案。