Abdel Maimouni Abdel Maimouni - 6 months ago 30
PHP Question

Add foreign key to request Laravel

what i am trying to achieve is :
User create a Delivery by a title input,

When the user send the request i want to get the users_id to add it to delivery table as a foreign key.
Here is the deliveries table

public function up()
{
Schema::create('deliveries', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('users_id')->unsigned();
$table->timestamps();
});

Schema::table('deliveries', function ($table) {


$table->foreign('users_id')->references('id')->on('users');
});
}


here is the DeliveryController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\User;

class DeliveryController extends Controller
{
public function index()
{
return view('delivery.index');
}

public function create()
{
return view('delivery.create');
}

public function store(Request $request)
{
$this->validate($request, [
'title' => 'required'
]);

$input = $request->all();

$input['users_id'] = Auth::user()->id;

Delivery::create($input);

return redirect()->back();
}

}

Answer

After weeks of researches i found That my Controller Missing This Line :

    use Illuminate\Support\Facades\Input; 
Comments