这是我尝试提交新收入时它给我的错误。我是 Laravel 5.3 的新手,所以我在 FK 和关系方面有很多错误;可以自由指出。并且要指出我已经有 3 个类型的条目,我可以在视图中看到和选择它们。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('types', function (Blueprint $table) {
$table->increments('id');
$table->string('type_name');
$table->timestamps();
});
Schema::table('incomes', function (Blueprint $table) {
$table->foreign('type_id')->references('id')->on('types')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('incomes', function (Blueprint $table) {
$table->dropColumn('type_id');
});
Schema::dropIfExists('types');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateIncomesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('incomes', function (Blueprint $table) {
$table->increments('id');
$table->decimal('quantity', 15,2);
$table->date('date');
$table->integer('type_id')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('incomes');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Type;
use DB;
use App\Income;
class IncomesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$income = Income::orderBy('id', 'ASC')->paginate(10);
return view('admin.incomes.index')->with("incomes", $income);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$types = DB::table('types')->pluck('type_name', 'id');
var_dump($types);
return view('admin.incomes.create')->with('types', $types);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$income = new Income($request->all());
$income->types()->save($type_id);
$income->save();
return redirect()->route('admin.incomes.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$income = Income::find($id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$income = Income::find($id);
return view('admin.incomes.edit')->with('incomes', $income);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$income = Income::find($id);
$income->quantity = $request->quantity;
$income->save();
Flash::info("El ingreso ha sido editado con exito!");
return redirect()->route('admin.incomes.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$income = Income::find($id);
$income->delete();
Flash::error('El ingreso de ' . $income->quantity . ' ha sido eliminado con exito!');
return redirect()->route('admin.incomes.index');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Type;
class TypesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$type = Type::orderBy('id', 'ASC')->paginate(5);
return view('admin.types.index')->with("types", $type);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.types.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//$type = new Type($request->all());
$type = new Type;
$type->type_name = $request->get('type_name');
$type->save();
return redirect()->route('admin.types.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$type = Type::find($id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$type = Type::find($id);
return view('admin.types.edit')->with('types', $type);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$type = Type::find($id);
$type->type_name = $request->type_name;
$type->save();
Flash::info("El tipo " . $type->type_name . " ha sido editado con exito!");
return redirect()->route('admin.types.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$type = Type::find($id);
$type->delete();
Flash::error('El tipo ' . $type->type_name . ' ha sido eliminado con exito!');
return redirect()->route('admin.types.index');
}
}
这是我的收入观点:
@extends('admin.template.main')
@section('title', 'Nuevo ingreso')
@section('content')
<?php
use App\Type;
use App\Income;
?>
{!! Form::open(['route' => 'admin.incomes.store', 'method' => 'POST']) !!}
<div class="form-group">
{!! Form::label('quantity', 'Cantidad') !!}
{!! Form::text('quantity', null, ['class' => 'form-control', 'placeholder' => 'Cantidad de ingreso', 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('type_id', 'Tipo') !!}
{!! Form::select('type_id', $types, null) !!}
</div>
<div class="form-group">
{!! Form::label('date', 'Fecha') !!}
{!! Form::date('date', null, ['class' => 'form-control', 'placeholder' => 'Fecha de ingreso', 'required']) !!}
</div>
<div class="form-group">
{!! Form::submit('Registrar', ['class' => 'btn btn-primary']) !!}
</div>
dd($request->all());
{!! Form::close() !!}
@endsection