I'm trying to save a multiple database records with image name and thumb(for now only text inputs). Every image has also a category/categories (made by multiselect and pivot table between image and categories with many to many relation). The question is: how to sync/attach those categories? I do not want to use foreach ::create, because it will generate unnecessary database requests. For now I have something like this:
my blade file (part in form)
<form action="/panel/images" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="row[1][title]">Image title</label>
<input type="text" name="row[1][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[1][thumb]">Thumb</label>
<input type="text" name="row[1][thumb]" class="form-control" />
</div>
<!-- date because insert methode don't create timestamps autimatically-->
<input type="hidden" name="row[1][created_at]" value="{{ $date }}" />
<input type="hidden" name="row[1][updated_at]" value="{{ $date }}" />
<div class="form-group">
<label for="multi[0][]">Categories</label>
<select name="multi[0][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<div class="form-group">
<label for="row[2][title]">Image title</label>
<input type="text" name="row[2][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[2][thumb]">Thumb</label>
<input type="text" name="row[2][thumb]" class="form-control" />
</div>
<div class="form-group">
<label for="multi[1][]">Categories</label>
<select name="multi[1][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<input type="text" name="row[2][created_at]" value="{{ $date }}" />
<input type="text" name="row[2][updated_at]" value="{{ $date }}" />
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
And the controller
public function store(Request $request)
{
$image = new Image;
$inputs = Input::get('row');
$multiSelect = Input::get('multi');
$image::insert($inputs);
$image->images_catetories()->sync($multiSelect);
return redirect()->route('newImage')->with('status', 'images added');
}
EDIT:
Right now it throws an error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into image_images_category (0, image_id, images_category_id, 1) values (18, , 1, 19))"
where "image_images_category" is my pivot table and values 18, 1, 19 are from multiselect.
Related
This question already has answers here:
Laravel 5.4 field doesn't have a default value
(10 answers)
Closed last year.
I've decided to not use image in my create menu page but then I got this error "SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value".
This is the MenuController:
public function store(Request $request, Menu $menu)
{
$data = array();
$data['name'] = $request->name;
$data['slug'] = $request->slug;
$data['price'] = $request->price;
$data['status'] = $request->status;
$menu = DB::table('menus')->insert($data);
return redirect()->route('admin.menu.index');
}
and the add.blade.php:
<form action="{{ route('admin.menu.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="name" required value="{{ old('name') }}">
</div>
</div>
<div class="form-group row">
<label for="staticprice" class="col-sm-2 col-form-label">Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" id="price" required value="{{ old('price') }}">
</div>
</div>
<div class="form-group row">
<label for="staticprice" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-10">
<input type="radio" name="status" value="1" /> Active
<input type="radio" name="status" value="0" /> Not Active
</div>
</div>
<input id="slug" name="slug" type="hidden" value="Menu-{{ rand(100,999)}}" />
<button type="submit" class="btn btn-secondary" style="margin-top: 20px; width: 100%">Submit</button>
</form>
I don't know where did I do wrong.
Actually this erorr arise because in your table named menus, the column named image is not set to NULL by default.
Either you have to add some value (can be empty) in it or you have to update its structure and set NULL as by default value.
See the below image to help.
As suggested above, setting "Default" to NULL will clear the error.
You can specify this when setting your migration table by
$table->string('image')->nullable();
My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.
I can't find solution to my problem...
I've created some kind of deck builder and when I choose cards to deck (which is empty on the begining $scope.deck = []) I would like to insert it to database.
I have form like this:
<form name="myform">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" ng-model="showCrudData.name" class="form-control" />
</div>
<div class="form-group">
<label for="type">Deck Type</label>
<input type="text" id="type" name="type" ng-model="showCrudData.type" class="form-control" />
</div>
<div class="form-group">
<label for="class">Class</label>
<input type="text" id="class" name="class" ng-model="showCrudData.class" class="form-control" />
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" id="author" name="author" ng-model="showCrudData.author" class="form-control" />
</div>
<label for="cards">Deck</label>
<p type="value" id="cards" name="cards" ng-model="showCrudData.cards">{{cards}}</p>
Add
<button type="reset" class="btn btn-default">Reset</button>
</form>
Im trying to insert {{cards}} into database as u can see, but its not working.
Here is my testing page where Im trying to solve the problem. The Deck label before choosing cards is looking like this []
And thats part of my PHP file where I insert values into table
case "add":
if(!empty($_POST['data'])){
$sql = "INSERT INTO decks values ('',:nm,:tp,:cl,:au,:cd)";
$insert = $query = $db->prepare($sql);
$query->execute(array(":nm"=>$_POST['data']['name'],":tp"=>$_POST['data']['type'],":cl"=>$_POST['data']['class'],":au"=>$_POST['data']['author'],":cd"=>print_r($_POST['data']['cards'])));
I tried to solve problem adding print_r, but it didnt work because its just adding number "1" into column "deck" in table.
I have this leftJoin in my Laravel project
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
Tables:
Product: id,name Destinations:id,product_id,destination,quantity,target_person
but i don't knwo how to display leftJoin,(i just want to display name form product and quantity,destination ,target_person from destinations)
and i want to add new record
This is my form :
<form action="." method="POST" id="formid">
{{ csrf_field() }}
<div class="form-group">
<label for="first_name">Product name</label>
<input class="form-control" name="name" >
</div>
<div class="form-group">
<label for="last_name">Quantity</label>
<input class="form-control" name='quantity'>
</div>
<div class="form-group">
<label for="last_name">Destination<label>
<input class="form-control" name='Destination'>
</div>
<div class="form-group">
<label for="last_name">Target Perosn</label>
<input class="form-control" name='target_person'>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
i never do this,please help me this,im beginner in this.
As you have mentioned, I just want to display name from product and quantity,destination ,target_person from destinations. You have to use table alias like:
products = DB::table('products as t1')
->leftJoin('destinations as t2','t1.id','=','t2.product_id')
->select('t1.name', 't2.quantity', 't2.destination', 't2.target_person')
->get();
I'm currently working on Laravel 5.2 and I'm finding difficult to fetch value from textarea inside controller.
I've tried $request->input just like we used to fetch values from inputs but it's not working in my case. I have done this far.
managecategories.blade.php
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
</form>
AdminAjaxController
public function addcategory(Request $request)
{
$category=$request->input('category');
$category_description=$request->input('description');
$insertcategory= DB::insert('insert into categories(category_name,description) values(?, ?)',[$category,$category_description]);
$fetch_category= DB::select('select category_name,category_id from categories');
return response()->json(array('add_category' => $category),200);
}
Oops Pity me! Both fields are named category. I need to change the name on the textarea to description caught by Aynber.
here is the edited and correct code
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="description" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
thanks mate! :)