Store specific value in PHP - php

I am trying to store specific value in laravel. Here Select User not required field but in foreach loop hidden field proj_id must has value. Suppose there are 5 rows and I want to store only 2nd and 5th user along with proj_id from hidden field. Here I want to mention that in controller I also kept delete operation so that I can remove previously inserted SAME PROJECT ID related record. For example If I want to store 2nd and 3rd user, that time only 2nd and 3rd users record will remove first then insert. In my code there are logical error but could not find solution. Thanks in advance
<form action="{{ url('/save-project') }}" method="POST">
<tr>
#foreach($projects as $val)
<td>
<input type="hidden" name="proj_id[]" value="{{$val->id}}">
<select name="user_id[]">
<option value="">Select User</option>
<option value="2">x</option>
<option value="4">y</option>
</select>
</td>
#endforeach
</tr>
<input type="submit">
</form>
Controller
$countUserID = count($user_id);
assign_project::where('flag','Y')
->WhereIn('proj_id',$request->proj_id)
->delete();
for($i=0;$i<$countUserID;$i++){
$assign_project = new assign_project();
$assign_project->proj_id = $request->proj_id[$i];
$assign_project->user_id = $request->user_id[$i];
$assign_project->save();
}

JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn’t we?
To create a JSON field, all we need to do in Laravel migration is use ->json() method:
$table->json('array_data');
Next, you need to tell your model to cast that column from JSON to an array automatically:
class Product extends Model
{
protected $casts = [
'array_data' => 'array'
];
}
This way, you will receive $array_data as array and don’t need to do json_decode() at all.

Related

Undefined variable: data (View: C:\cygwin64\home\hp\AddressBook\resources\views\Update\edit.blade.php)

My dynamic drop down list is not working when I am trying to navigate to update page by using url id and then its showing errors.The result suppose to be a name of city that is associated with id in the database.
controller class
public function updateAddress($edit_id)
{
$data=[];
$data['edit_id']=$edit_id;
$address_data = $this->address->find($edit_id);
$data['first_name']=$address_data->first_name;
$data['last_name']=$address_data->last_name;
$data['street']=$address_data->street;
$data['zip_code']=$address_data->zip_code;
$data['city']=$address_data->city;
return view('update/edit',$data);
}
Here is my blade file. The rest of the fields are working properly except drop down list. I am using foreach loop to display data into the drop down list.
<select id="city" name="city" >
#foreach($data as $cities)
<option id="city" value="{{$cities->edit_id}}" selected="selected">{{$cities->city}}</option>
#endforeach
</select>
Update the code in the controller so that:
return view('update/edit',$data);
Is changed to:
return view('update/edit', ['data' => $data]);
change your controller code to
return view('update/edit',compact('data'));
and change your view page like
{{ $data['edit_id'] }}. you dont want to use #foreach.

'QueryException Array to string conversion' in Laravel application

The error says 'QueryException Array to string conversion'.
Find my code below:
CreateProject.php
<label for="client_id">Select Client*</label>
<select name="client_name_array" id="">
#foreach ($clientsArray as $option)
<option value="{{$option}}">{{$option}}</option>
#endforeach
</select>
controller:
$projects = new Project([
'client_name' => $request->input('client_name_array'),
]);
$projects->save();
return redirect('/projects')->with('status', 'Project updated!');
Please let me know if you need any further info regarding this query. Thanks in advance.
I am not sure but this could be the answer.
In CreateProject.php you have used
#foreach ($clientsArray as $option)
<option value="{{$option}}">{{$option}}</option>
#endforeach
Here $option is also an array which is being passed as a value to the name client_name_array.
When you submit the form, at the controller, $request->input('client_name_array') returns an array. But, the variable client_name seems to be of string type in the database. The array type cannot be changed to string type this way. Hope you got my point.
You can try Attribute Casting eloquent model feature.
In your Project class add
protected $casts = [
'client_name_array' => 'array',
];
So the array data will be automatically transformed to string on save and to array on retrieve.
See Laravel: Attribute Casting
I have added this to previous answer but then decided to make my own.
Also change this:
<select name="client_name_array" id="">
to
<select name="client_name_array[]" id="">

Laravel/Blade Drop Down List - For more than one field

I'm trying to use blade to display a dropdown list from table data. The problem I have, is that I want to display the results of two fields in the table concatenated, not just one.
So I want something to render something like;
<select id="agreement_type" name="agreement_type">
<option value="1">Agreement Field 1 - Agreement Field 2</option>
<option value="2">Agreement Field 1 - Agreement Field 2</option>
<option value="4">Agreement Field 1 - Agreement Field 2</option>
</select>
My controller currently looks like this;
$agreements = Agreement::lists('agreement_type','id');
return View::make('client_agreements.create')
->with('agreements', $agreements);
My blade view currently looks like this;
<div class="form-group">
{{ Form::label('agreement_type', 'Agreement type') }}
{{ Form::select('agreement_type', $agreements) }}
</div>
I have tried to amend the view and controller in various ways to get the desired output. But can't get it to work.
I want to display agreement_type and level and have the id set as the value so I tried;
$agreements = Agreement::lists('agreement_type'.'level','id');
But this just displays level as the value and completely ignores id.
This is the simplest method. Just use a foreach loop to build the options array:
$agreements = Agreement::all();
$agreementOptions = array();
foreach($agreements as $agreement){
$agreementOptions[$agreement->id] = $agreement->agreement_type.' '.$agreement->level;
}
return View::make('client_agreements.create')
->with('agreements', $agreementOptions);
However you can also define an attribute accessor in your model. You can use that to create new properties that can be accessed normal, but you can run logic to generate them (like combining to attributes)
public function getSelectOptionAttribute(){
return $this->attributes['agreement_type'].' '.$this->attributes['level'];
}
And then you can use lists:
$agreements = Agreement::all()->lists('select_option', 'id');
(Laravel converts SelectOption (studly case) from the method name to select_option (snake case) for the attribute, so don't get confused by that)

Laravel update form - get correct selected state

Within my update profile form, I have a select list for title. Admittedly, the items should be derived from the database however for the moment, I would like this particular select list to do the following..
Show the selected item on initial page load. So, if the $user['title'] is a 'Mrs', this is displayed.
Should laravel validation bring up errors on other form fields, and the user has changed the title to 'Mr', the selected state should be on the item 'Mr'.
I have the following so far, but it isn't working correctly.
<select name="title" id="title">
<option value="Mr" #if(($user['title']) == 'Mr') selected #else #if((Input::old('title')) == 'Mr') selected #endif #endif>Mr</option>
<option value="Mrs" #if(($user['title']) == 'Mrs') selected #else #if((Input::old('title')) == 'Mrs') selected #endif #endif>Mrs</option>
....
</select>
You should pass the data for the select from your Controller and also uset the Form component to build the select in the View, for example:
In Controller method:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
In the View:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
If you are using Blade then:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
Read the documentation properly. You are using Laravel with old school PHP coding.

PHP: How to organise queries and tables to generate HTML selection box

I am attempting to create a drop down selection box for various attributes of a product which the user would select on a product page, such as weight or flavour or colour. My database for the products is currently as follows:
products(id,productname,productprice)
productattributes(id,productid,attributename,value)
I'm using Laravel. So far in my controller I have:
public function show($id)
{
$product = DB::table('products')->where('slug', $id)->take(1)->get();
$prodid = DB::table('products')->where('slug', $id)->pluck('id');
$prodattr = DB::table('productattributes')->where('prodid', $prodid)->get();
return View::make('product/singleproduct', ['product' => $product, 'prodattr' => $prodattr]);
}
And in the view I have:
#foreach ($prodattr as $prodattribute)
{{ $attrname = $prodattribute->attributename }}
{{ $attrvalue = $prodattribute->value }}
#endforeach
This gives me the output:
Flavour Unflavoured
Flavour Strawberry
Weight 1kg
So I have the data I need, but my problem is how to get this into a select box for each attribute. I won't always know what the attribute names will be (colour may be added, for example) or how many there will be, so I don't want to hard code 'Flavour' etc in. I essentially want to generate HTML like this:
<select name="flavour">
<option value="Unflavoured">Unflavoured</option>
<option value="Strawberry">Strawberry</option>
</select>
<br>
<select name="weight">
<option value="1kg">1kg</option>
</select>
Does anyone have any ideas? I'm thinking a loop within the loop, or maybe my table layout is less than ideal? I thought about adding another query within the foreach loop, but this seems counterproductive as I think I have all the data I need. I also wondered if it would be possible to combine or merge the arrays somehow? Any help is much appreciated!
You could create a third table in order to seperate custom attributes and available values for each attributes.
Example of structure I would use:
products(id, name, price)
product_meta_name(id, name)
product_meta_value(id, product_meta_name_id, product_id, value)

Categories