IncomesController.php第45行中的ErrorException:未定义的变量:type_id

时间:2016-12-05 17:48:37

标签: php laravel-5.3 relationships laravelcollective laravel-views

当我尝试提交新的收入时,这是它给我的错误。 我是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

1 个答案:

答案 0 :(得分:0)

/**
 * 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());
    /* There you have a variable $type_id
     * but you do not assign it anywhere.
     */
    $income->types()->save($type_id /* <- there */);
    $income->save();
    return redirect()->route('admin.incomes.index');
}

您尝试使用不存在的变量$type_id。我假设您希望从此变量下的表单中获取值,然后您需要从Request对象获取值:

$type_id = $request->input('type_id', 'default_value');
$income->types()->save($type_id);