I want to make dropdown menu where you choose company and it shows you owners that are in that company but when i request data from my blade.php in my controller i get NULL.
This is my blade.php
<form>
<label>Company:</label>
<select id="test" name="test" class="form-control input-sm">
#foreach($companies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
</form>
and this is my controller
public function index(Request $request)
{
$companies = Company::all();
$company_id = $request->get('company');
$owners = Owner::where('company_id',$company_id)->get();
return view ('owners', compact('owners','companies'));
}
and it's not showing any user but if I put any number manually like this
$company_id = 1;
then it shows owners from company where ID is 1.
UPDATE 2
My form looks like this now
<form action="{{ action('OwnerController#index') method="get"}}">
and my route is this
Route::get('owners', 'OwnerController#index');
Its still same.
Not sure what you want. But here is something for you.
File => create.blade.php
<form action='/item' method='POST'>
<label>Item Name:</label>
<input type="text" name="name" class="form-control input-sm">
<label>Company:</label>
<select id="company" name="company" class="form-control input-sm">
#foreach($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
<button class="btn btn-primary">Create</button>
</form>
File => ItemController.php
public function store(Request $request)
{
try {
$this->validate($request, [
'company_id' => 'required',
'name' => 'required|unique:items,name'
]);
$obj = new Item();
$obj->company_id = $request->input('company_id');
$obj->name = $request->input('name');
$obj->save();
return redirect()->route('item.index')
->with('success', 'Create Successful');
} catch (\Exception $ex) {
Log::error($ex->getMessage());
return redirect()->route('item.create')
->with('fail', $ex->getMessage());
}
}
As I said before I was missing some script. This solved my problem.
<form method="get" id="company_change">
<label for="company">Company:</label>
<select id="company" name="company" onchange="document.getElementById('company_change').submit()">
<option value="">----Select Company-----</option>
#foreach($companies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
</form>
My route is
Route::resource('owners', 'OwnerController');
You dont need a form to fill your dropdown;
You create a relationship in your model between Company and Owner and you dont even need a where,simply use eloquent like this
$owners = Owner::get();
And then in your view simply get the company using eloquent:
$owner->company->name;
That's it done!
Related
I need a little help..
In case of validation errors, when the page is reloaded I lose the information entered previously.
This is my code:
Controller:
public function create()
{
$nationalities = array('Italian','Brazilian','Spanish','Romanian');
return view('guest.create', ['nationalities'=>$nationalities);
}
public function store(Request $request)
{
$request->validate([
'nationality' => 'required'
]);
Guest::create([
'nationality' => $request->nationality
]);
return redirect(route('guest.index'));
}
View:
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}">{{$nationality}}</option>
#endforeach
</select>
</div>
How can I modify the code so that this doesn't happen?
How can you select back what failed if you did not even ask for the old value?
This may or not work depending on how you are validating, but you did not want to show that...
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}" {{ old('nationality') === $nationality) ? 'selected="selected"' : '' }}>{{$nationality}}</option>
#endforeach
</select>
</div>
<option value="{{$nationality}}" #selected(old('nationality')==$nationality)>
{{$nationality}}
</option>
add the #selected directive in blade.
I have form where I am planning to insert data into units table .. it has fields course_id | unit_number | unit_title
relationship defined : course has many units .. units belongs to course.
the form is working fine, in case I'm inserting only one unit. but when I insert multiple rows it only stores the last row.
#include('admin.messages')
<form action="{{ route('adminSendCreateUnits')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="row col-12">
<div class="col-sm">
<div class="form-group">
<label style="width:100%; background-color:grey; color:white; padding:15px;" for="course_id">Course</label>
<select name="course_id" class="form-control">
<option>Select Course</option><!--selected by default-->
#foreach ($courses as $course)
<option value="{{$course->id}}">
{{ $course->id}}-{{ $course->crs_title}}
</option>
#endforeach
</select>
</div>
</div>
</div><br>
<label for="unit_number">Unit Number</label>
<select name="unit_number[]" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="10">10</option>
</select>
<label for="unit_title">Unit Title</label>
<input type="text" class="form-control" name="unit_title[]" id="unit_title" placeholder="Unit Title" required>
my unitscontroller.php [ I'm sure this is wrong .. since I was trying stuff and i lack the knowledge]
public function adminSendCreateUnit(Request $request){
$course_id = $request->input('course_id');
$unit_number = $request->input('unit_number', []);
$unit_title = $request->input('unit_title', []);
$newCategoryArray = array(
"course_id"=>$course_id,
"unit_number"=>$unit_number,
'created_at' => \Carbon::now(),
"unit_title"=> $unit_title);
$created = DB::table("units")->insert($newCategoryArray);
if($created){
return redirect()->route("adminAllUnits")->withSuccess('Unit Created successfully!');
}else{
return "Unit was not Created";
}
}
I would like to have the course ID same in all rows .. only Unit number and titles change. would appreciate any input :)
Solved by trying this out.
public function adminSendCreateUnit(Request $request){
$course_id = $request->input('course_id');
$unit_number = $request->input('unit_number', []);
$unit_title = $request->input('unit_title', []);
$units = [];
foreach ($unit_number as $index => $unit) {$units[] = [
"course_id" => $course_id, // change this
"unit_number" => $unit_number[$index],
'created_at' => \Carbon::now(),
"unit_title" => $unit_title[$index]
];
}
$created = Unit::insert($units);
if($created){
return redirect()->route("adminAllUnits")->withSuccess('Unit Created successfully!');
}else{
return "Unit was not Created";
}
}
hi m trying to save data into db using Eloquent ORM one-to-one but it is not saving and not showing any error to fix it (m unable to find any solution about it because it is not showing any error),
public function store(Request $request)
{
$request->validate([
'owner_id' => 'required',
'phone_name' => 'required|unique:phones'
]);
$phone = new Phone;
$phone->owner_id = $request->owner_id;
$phone->phone_name = $request->phone_name;
$phone->save();
return redirect()->route('phone.index')->with('flash_messages_success', 'Phone has been added successfully');
}
protected $fillable = [
'pphone_name', 'owner_id'
];
public function owner()
{
return $this->belongsTo('App\Owner');
}
<form method="POST" action="{{ route('phone.store') }}">
#csrf
<div class="form-group">
<label for="">Phone Title</label>
<input type="text" name="phone_name" class="form-control" id="" placeholder="Phone Name">
</div>
<div class="form-group">
<label>Select Owner</label>
<select class="form-control" name="category_id">
<option selected="">Under Owner</option>
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->owner_name }}</option>
#endforeach
</select>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
The issue in select name you named category_id so the view should be:
<select class="form-control" name="owner_id">
When i try to insert data into my table this error occurs
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\xampp\htdocs\Portal\vendor\laravel\framew...
view
<form method="post" action="{{ route('notice.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="Select Group to Post Notice">Select Group to Post Notice </label>
<select class="bg-white text-danger form-control " name='GroupID[]' multiple>
#foreach ($users as $user)
<option value="{{ $user->GroupID }}">{{ $user->GroupID }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="Enter Notice">Enter Notice</label>
<input class="bg-white text-danger p-2 form-control form-control-sm" type="text" name="Notice" placeholder="Enter Notice">
</div>
<input class="btn btn-danger btn-lg px-5" type="submit" name="submit">
</form>
controller
public function store(Request $request)
{
$member = $request->input('GroupID');
foreach($member as $value) {
$storeInfo = new notice();
$storeInfo->GroupID = $request->input('GroupID');
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}
I would imagine the reason you're getting this error is because of:
$storeInfo->GroupID = $request->input('GroupID');
$request->input('GroupID') will return an array (name='GroupID[]') and not an individual id.
Since you're already looping through the group ids you can instead use the value for the GroupId:
public function store(Request $request)
{
foreach ($request->input('GroupID') as $groupId) {
$storeInfo = new notice();
$storeInfo->GroupID = $groupId; //<--here
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('notice');
}
try changing controller logic
public function store(Request $request)
{
//
$member=$request->input('GroupID');
foreach($member as $value){
$storeInfo = new notice();
$storeInfo->GroupID = $value;
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}
Hello everyone and thank you in advance, first of all sorry if my code is so confused I am still learning ^^.
I am trying to make a product with different attribute plus 3 different images(making the product gallery) and a feature image,
I have succeed to create the attributes and feature image.
The problem is with the 3 image for the gallery I cant store (neither in DB or image folder)
I have Two tables (product and productgallery) the relation between them is hasMany (I am using Laravel 5.4)
This the error I got after run the code
SQLSTATE[HY000]: General error: 1364 Field 'product_id' doesn't have a default value (SQL: insert into `product_galleries` (`updated_at`, `created_at`) values (2017-05-13 20:58:20, 2017-05-13 20:58:20))
(sorry for my English which for sure will make it harder to understand my question)
ProductController
public function store(UploadRequest $request)
{
$product = new Product();
$product->product_name = $request->product_name;
$product->product_description = $request->product_description;
if($request->hasFile('product_preview')) {
$file = Input::file('product_preview');
$filename = time(). '-' .$file->getClientOriginalName();
$product->product_preview = $filename;
$file->move(public_path().'/images/product-feature', $filename);
}
$product->category_id = $request->category_id;
$product->color_id = $request->color_id;
$product->size_id = $request->size_id;
$product->material_id = $request->material_id;
// $product->fantasia_id = $request->fantasia_id;
$productgallery = new ProductGallery();
if($request->hasFile('fileToUpload[]')) {
$files = Input::file('fileToUpload[]');
foreach ($request->$files as $photo) {
$filename = time(). '-' .$photo->getClientOriginalName();
$productgallery->product_images = $filename;
$photo->move(public_path().'/images/product-gallery', $filename);
$productgallery->product_id = 1;
}
}
$product->save();
$productgallery->save();
return $this->create()->with('success', 'Uploaded Successfully');
}
Upload request (FormRequest)
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UploadRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [
'product_name' => 'required|max:120',
'category_id' => 'required|integer',
'product_preview' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
];
$fileToUpload = count($this->input('fileToUpload'));
foreach(range(0, $fileToUpload) as $index) {
$rules['fileToUpload.' . $index] = 'image|mimes:jpeg,bmp,png|max:2000';
}
return $rules;
}
}
the view for the form
#extends('layouts.backend-master')
#section('styles')
<link rel="stylesheet" href="">
#endsection
#section('content')
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h1>Add a new product</h1>
<form action="{{route('products.store')}}" method="post" enctype="multipart/form-data">
<div class="input-group">
<label for="product_name">Name of the product</label>
<input type="text" name="product_name" id="product_name"/>
</div>
<div class="input-group">
<label for="product_description">Product Description</label>
<textarea type="text" name="product_description" id="product_description" rows="8"></textarea>
</div>
<div class="input-group">
<label for="product_preview">Feature Image:</label>
<input type="file" name="product_preview" id="file">
</div>
<div class="input-group">
<label for="category_id">Category</label>
<select name="category_id" id="category_id">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->category_name }}</option>
#endforeach
</select>
</div>
<div class="input-group">
<label for="color_id">Color</label>
<select name="color_id" id="color_id">
#foreach($colors as $color)
<option value="{{ $color->id }}">{{ $color->color_name }}</option>
#endforeach
</select>
</div>
<div class="input-group">
<label for="size_id">Size</label>
<select name="size_id" id="size_id">
#foreach($sizes as $size)
<option value="{{ $size->id }}">{{ $size->size_name }}</option>
#endforeach
</select>
</div>
<div class="input-group">
<label for="material_id">Material</label>
<select name="material_id" id="material_id">
#foreach($materials as $material)
<option value="{{ $material->id }}">{{ $material->material_type }}</option>
#endforeach
</select>
</div>
<div class="input-group">
<label for="fileToUpload">Product Gallery:</label>
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple >
</div>
<button type="submit" class="btn">Add</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
#endsection
#section('scripts')
#endsection
thank you
You'll need to look at Laravel relationships API documentation. Here's a link to read more about it Laravel 5.4 Eloquent Relationships
Quick tip to fix that
You should add a foreign key in your database/migration file
For a migration you can simply add
$table->foreign('product_id')->references('id')->on('products');
And add this method to your Product model
public function productgallery(){
return $this->hasMany(Productgallery::class);
}
and this to your Productgallery model
public function product(){
return $this->belongsTo(Product::class);
}
Note: You'll still need to populate the product_id field within your request
Package solution
I'd recommend you use a 3rd party package for that matter, as it will be less work, cleaner and more solid to use.
You can look more into spatie's medialibrary for Laravel
I think this is what you want.
This will create a new Product and create a new ProductGallery for each image passed in $request->file('fileToUpload[]'):
$product = new Product();
$product->product_name = $request->product_name;
$product->product_description = $request->product_description;
if($request->hasFile('product_preview')) {
$file = Input::file('product_preview');
$filename = time(). '-' .$file->getClientOriginalName();
$product->product_preview = $filename;
$file->move(public_path().'/images/product-feature', $filename);
}
$product->category_id = $request->category_id;
$product->color_id = $request->color_id;
$product->size_id = $request->size_id;
$product->material_id = $request->material_id;
// $product->fantasia_id = $request->fantasia_id;
// First save the product (so that you have it's ID)
$product->save();
// Then create galleries
if($request->hasFile('fileToUpload[]')) {
$files = Input::file('fileToUpload[]');
foreach ($files as $photo) {
$productgallery = new ProductGallery();
$filename = time(). '-' .$photo->getClientOriginalName();
$productgallery->product_images = $filename;
$photo->move(public_path().'/images/product-gallery', $filename);
$productgallery->product_id = $product->id; // Save it to the newly created product
$productgallery->save();
}
}
return $this->create()->with('success', 'Uploaded Successfully');