laravel 5.3 — ErrorException в строке IncomesController.php 45: неопределенная переменная: type_id

Это ошибка, которую он возвращает мне, когда я пытаюсь отправить новый доход.
Я новичок в 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

Решение

/**
* 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);
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]