Ich habe Tabelle mit dem Namen companies
und andere Tabelle mit dem Namen ads
, ich versuche, Firma ID in Anzeigen Spalte namens company_id
.Laravel Fremdschlüssel spart Problem
Dies ist meine Anzeigen Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id')->unsigned();
$table->string('title')->unique();
$table->string('slug')->unique();
$table->string('image')->nullable();
$table->string('description');
$table->timestamps();
});
Schema::table('ads', function($table) {
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ads');
}
}
dies wird mir Anzeigen erstellen Tabelle kein Problem, aber wenn ich versuche, Anzeigen zu speichern es gibt mir diese Fehlermeldung:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`jobid`.`ads`, CONSTRAINT `ads_company_id_foreign` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`)) (SQL: insert into `ads` (`title`, `slug`, `description`, `image`, `updated_at`, `created_at`) values (first ad test, first-ad-test, <p>rwv R4QF Q4R</p>, 1494998776.png, 2017-05-17 12:26:17, 2017-05-17 12:26:17))
Wie kann ich das beheben Das?
UPDATE
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->string('company_name');
$table->string('manager_name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('image')->nullable();
$table->string('password');
$table->text('about')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
Store-Funktion
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:ads,slug',
'image' => 'sometimes|image',
'description' => 'required'
));
$ad = new Ad;
$ad->title = $request->input('title');
$ad->slug = $request->input('slug');
$ad->description = $request->input('description');
if ($request->hasFile('image')) {
$avatar = $request->file('image');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('ads/');
$request->file('image')->move($location, $filename);
$ad->image = $filename;
}
$ad->save();
Session::flash('success', 'Your ad published successfully!');
return redirect()->route('company.adslist', $ad->id);
}
Buchen Sie Ihr Firmenschema –
@SapneshNaik bereits – djhru
Sie müssen sicherstellen, dass 'company_id' an die Tabelle gesendet wird. Sie übergeben alles außer 'company_id'. _ ** Einfügen in 'ads' (' title', 'slug',' description', 'image',' updated_at', 'created_at') Werte (erster Ad-Test, erster Ad-Test,
rwv R4QF Q4R
, 1494998776.png, 2017-05-17 12:26:17, 2017-05-17 12:26:17)) ** _ – Priya