Laravel 我如何处理拉威尔的形式
我试图处理表单来更新动物表中的数据,但它并没有显示任何错误,但根本并没有保存 这是我的表单create.blade.phpLaravel 我如何处理拉威尔的形式,laravel,Laravel,我试图处理表单来更新动物表中的数据,但它并没有显示任何错误,但根本并没有保存 这是我的表单create.blade.php @extends('layouts.app') @section('title', 'Add Animal') @section('content') <div class="row"> <div class="col-12"> <h1>Farm</h1> <
@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
@extends('layouts.app'))
@章节(‘标题’、‘添加动物’)
@节(“内容”)
农场
欢迎{{$user->name}}请添加动物
出生日期:
{{$errors->first('dateOfBirth')}
出生地
性别:
男性
女性
添加农场
@csrf
@端部
还有我的农场管理员
<?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',
]);
}
}
遵循您的逻辑
public function store()
{
$animal = Animal::create($this->validateRequest());
event(new NewAnimalRegisteredEvent($animal));
// Mail::to($customer->email)->send(new WelcomeNewUserMail());
return redirect('farm.show');
}
通过提交表单调用,并且正在调用事件。但此事件不具有所需的句柄功能:
你现在的活动没有任何作用。看起来您试图广播,但它没有实现
ShouldBroadcast
,您还应该检查是否满足要求。我希望这有帮助,也许应该是一个评论,但文本太长,不能作为评论。如果提供更多信息,我可以根据需要编辑此答案。提交表单时实际会发生什么?store
方法的重定向将是一个有趣的URL,就像什么都没有发生一样,没有错误可能在EventServiceProvider
中为它定义了一个侦听器,但是谁呢knows@lagbox很好,我希望Jimmyjbk能说明情况是否如此。目前,我试图通过分析当前提供的信息来提供帮助。我只是想帮助新用户尽最大努力提供这一级别的信息。:)我不明白这辆车到底出了什么毛病code@Jimmyjbk为什么不从简单的开始,不使用事件,直接保存
<?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');
}
}
public function store()
{
$animal = Animal::create($this->validateRequest());
event(new NewAnimalRegisteredEvent($animal));
// Mail::to($customer->email)->send(new WelcomeNewUserMail());
return redirect('farm.show');
}