QueryException (23000) SQLSTATE[23000]: Integrity constraint v1452 - mysql

I am sucked by this error since this week could any one help me please !
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (practice2.posts, CONSTRAINT posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id)) (SQL: insert into posts (author_id, updated_at, created_at) values (2, 2017-11-13 05:48:53, 2017-11-13 05:48:53))
my codes are:
Blog controller :
public function store(Requests\PostRequest $request)
{
$request->user()->posts()->create($request->all());
redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
**migration:**
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('tittle');
$table->string('slug')->unique;
$table->text('excerpt');
$table->text('body');
$table->string('image')-> nullable();
$table->timestamps();
});
}
alter_post_migration :
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
//
});
}
create post form:
<div class="box-body ">
{!! Form::model($post, [
'method' => 'POST',
'route' => 'back.store'
]) !!}
<div class="form-group {{ $errors->has('tittle') ? 'has-error' : '' }}">
{!! Form::label('tittle') !!}
{!! Form::text('tittle', null, ['class' => 'form-control']) !!}
#if($errors->has('tittle'))
<span class="help-block">{{ $errors->first('tittle') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
{!! Form::label('slug') !!}
{!! Form::text('slug', null, ['class' => 'form-control']) !!}
#if($errors->has('slug'))
<span class="help-block">{{ $errors->first('slug') }}</span>
#endif
</div>
<div class="form-group">
{!! Form::label('excerpt') !!}
{!! Form::textarea('excerpt', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
{!! Form::label('body') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
#if($errors->has('body'))
<span class="help-block">{{ $errors->first('body') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('published_at') ? 'has-error' : '' }}">
{!! Form::label('published_at', 'Publish Date') !!}
{!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s']) !!}
#if($errors->has('published_at'))
<span class="help-block">{{ $errors->first('published_at') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : '' }}">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', App\Category::pluck('tittle', 'id'), null, ['class' => 'form-control', 'placeholder' => 'Choose category']) !!}
#if($errors->has('category_id'))
<span class="help-block">{{ $errors->first('category_id') }}</span>
#endif
</div>
<hr>
{!! Form::submit('Create new post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
</div>

The error:
Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (practice2.posts, CONSTRAINT
posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES
categories (id))
comes when two table share a relationship of foreign key and you are trying to add some data in child table and its associated record does not exist in the parent table. So check the data accordingly and try again.

Related

Laravel 6 - How to create a table with dynamic rows using LaravelCollective?

I want to make a dynamic table using LaravelCollective, where you can add/remove rows.
I already tried to follow a few tutorials, but I keep failing. I don't know what I'm doing wrong.
Html Code for the table/form
{{ Form::open(['action' => 'TransactionsINController#store', 'method' => 'POST']) }}
<section>
<div class="panel panel-header">
<div class="form-group">
{{ Form::label('supplier_name', 'Supplier Name') }}
{{ Form::select('supplier_name', $supplierList->pluck('name', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick one supplier...']) }}
</div>
<div class="form-group">
{{ Form::label('transaction_in_date', 'Transcation Date') }} <br>
{{ Form::date('transaction_in_date', \Carbon\Carbon::now()->format('d M Y')) }}
</div>
</div>
<div class="panel panel-footer">
<table class="table table-hover table-bordered" id="">
<thead align="center">
<tr>
<th>Device Type</th>
<th>Spec</th>
<th>Price</th>
<th><a href="#" class="btn btn-success addRow">
<i class="fa fa-plus"></i>
</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="form-group">
{{ Form::select('device_type_name[]', $deviceTypeList->pluck('name_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}
</div>
</td>
<td>
<a href="#" class="btn btn-danger remove">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</section>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{{ Form::close() }}
The Script i use to add/remove Rows
<script type="text/javascript">
$('.addRow').on('click', function(){ \\Uncaught ReferenceError: happen in this line.
addRow();
});
function addRow(){
var tr = '<tr>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::select('device_type_name[]', $deviceTypeList->pluck('nama_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<a href="#" class="btn btn-danger remove">'+
'<i class="fa fa-times"></i>'+
'</a>'+
'</td>'+
'</tr>';
$('tbody').append(tr);
}
</script>
When I press the + button to add more rows it doesn't do anything and when I inspected the code in the browser, I found this error:
Uncaught ReferenceError: $ is not defined
$ refers to jQuery, have you added it to your project and is it available in this scope?
Note: it needs to be present before your script
jQuery may not have loaded when that script runs, you can try wrapping your script with $(document).ready({ }) so it runs it when jQuery has loaded

Treeview with --- select option

I'm building a treeview inside my Laravel application but I have a problem with the view. I'm currently getting everything under the same select box, for example:
demo1
demo2
demo3
However, I want this inside select options like:
demo1
--- demo2
------ demo3
Controller:
$categories = Pool::where('parent_id', '=', 0)->get();
$allCategories = Pool::pluck('title','id')->all();
return view('admin.accept', compact(['categories','allCategories']));
View:
<div class="form-group {{ $errors->has('parent_id') ? 'has-error' : '' }}">
{!! Form::label('Pool:') !!}
{!! Form::select('parent_id',$allCategories, old('parent_id'), ['class'=>'form-control', 'placeholder'=>'Kies uw Pool']) !!}
<span class="text-danger">{{ $errors->first('parent_id') }}</span>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
{!! Form::label('Naam:') !!}
{!! Form::text('title', old('title'), ['class'=>'form-control', 'placeholder'=>'Enter Title']) !!}
<span class="text-danger">{{ $errors->first('title') }}</span>
</div>
How can I do this?
How can I build a treeview inside my Laravel application?

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.

How to define hyperlink in Laravel Blade template?

There is a button,which will redirect to a new page after being clicked.
I want to define a hyperlink for this in laravel blade template.
{!! Form::button('<span class="fa fa-arrow-circle-right"></span> Start',
array('class' => 'btn btn-next next-step pull-right'))
!!}
Wrap your button with a form:
<form action="http://example.com">
// button code
</form>
Or:
{!! Form::open(['url' => 'http://example.com']) !!}
// button code
{!! Form::close() !!}
{!! Form::open(['route' => 'route.name']) !!}
// button code
{!! Form::close() !!}
{!! Form::open(['action' => 'Controller#action']) !!}
// button code
{!! Form::close() !!}

laravel 5 and mysql relations SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I am trying to insert values into my services table and I am getting a error. Its saying that I can not add or update child row .
message:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (adrenaline.services, CONSTRAINT services_id_produit_foreign FOREIGN KEY (id_produit) REFERENCES produits (id_produit)) (SQL: insert into services (description, updated_at, created_at) values (sqslqslk, 2016-04-11 21:02:35, 2016-04-11 21:02:35))
my controller
public function create()
{
return view('services');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store(serviceRequest $request)
{
$input=$request->all();
// dd($input);
\App\Service::create($input);
}
my view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Ajouter une service</div>
<div class="panel-body">
{!! Form::open(['url'=>'service/store']) !!}
<div class="form-group">
{!! Form::label('titre', 'Titre:') !!}
{!! Form::text('titre',null) !!}
</div>
<div class="form-group">
{!! Form::label('produit', 'Id produit:') !!}
{{ Form::input('number', 'produit',null) }}
</div>
<div class="form-group">
{!! Form::label('axe_p', 'Id axe principale:') !!}
{{ Form::input('number', 'axe_p') }}
</div>
<div class="form-group">
{!! Form::label('description', 'Description:') !!}
{!! Form::textarea('description',null) !!}
</div>
<div class="form-group">
{!! Form::submit('Nouveau service') !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
#endsection