Laravel 5 get select option value - laravel-5

in my create function, I do
$clients = Client::lists('clientName');
return View::make('projects.create', compact('clients'));
In my View, I then populate the select by doing this
{!! Form::select('clientName', $clients, Input::old('clients'), ['class' => 'form-control']) !!}
When I view the html, I see something like the following
<select name="clientName" id="clientName" class="form-control">
<option value="0">John Dandy</option>
</select>
Now in my store function, I need to get the value John Dandy, not 0. If I do
$clientName = Input::get('clientName');
I get 0. How can I set the value to the clientName or get the option in my store function?
Thanks

If you specify only one argument for the lists method, it will return an array of values automatically mapped to a counter key part starting with 0 (that's why you're getting that 0).
However, using Laravel's Query builder method lists, you can specify a custom key column as the second parameter (Documentation).
So if you were to get a key value pair where the value acts as the key itself, you could do the following:
$clients = Client::lists('name', 'name');
Or, if you wanted an id => client array:
$clients = Client::lists('name', 'id');

Related

laravel validation: unique two fields and array

Using laravel validation, I would like to ensure that a field is unique, but in an array context. (I have seen this and this but they don't address the array context.)
Let's suppose I have this html:
<input name="sites[1][id]"><input name="sites[1][site_mrn]">
<input name="sites[2][id]"><input name="sites[2][site_mrn]">
<input name="sites[3][id]"><input name="sites[3][site_mrn]">
In my validation rule, I want to ensure that each site's id is valid, and that the site_mrn is not blank, so I have:
public function rules()
{
return [
'sites.*.site_mrn' => 'required|min:1',
'sites.*.id' => 'exists:sites,id'
];
}
So that part works. My problem is that I want to ensure that each pair of site site_id and site_mrn are unique in the mpi_sites table, but I don't know how to access each id/site_mrn pair in the input. I want to do something like this:
'sites.*' => Rule::unique('mpi_sites')->where(function ($q) {
$q->where('site_id', $xxxxx)->where('site_mrn', $yyyyy);
})

Show latest data from row based on custom date_field in laravel

I have 2 tables and I need to show the latest single data from row based based on due_date field in my blade file.
blade
#foreach(Auth::user()->statements->orderBy('due_date','desc')->get() as
$statement)
<p> Your unpaid bill is: {{ $statement->billamount }} </p>
#endforeach
I get this error
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
(View:...\home.blade.php)
EDIT
I edited the code based on the comments below:
#foreach(Auth::user()->statements()->latest('due_date')->first() as
$statement)
{{ $statement->billamount }} //I only need the latest record to be shown
#endforeach
and it throws this error:
Trying to get property 'billamount' of non-object.
change Auth::user()->statements to Auth::user()->statements()
orderBy is a method of Query Builder. Auth::user()->statements is a collection and Auth::user()->statements is a query builder
to show latest row data only you can use orderBy and first method. example:
Auth::user()->statements()->orderBy('created_at','desc')->first()
another way use latest method:
Auth::user()->statements()->latest('created_at')->first()
#foreach(Auth::user()->statements()->latest('due_date')->first()) as
$statement)
when you look closely there is an extra bracket after first() so remove it and test it
#foreach(Auth::user()->statements()->latest('due_date')->first() as
$statement)
like above
also when using first you dont need to give foreach use get() rather than first()
get() return s a collection while first() returns a single record details

CFWheels: Turn off validatesPresenceOf() temporarily

I am have a model where I have using validatesPresenceOf() to check the presence of id column in my table.
<cfscript>
component extends="Model"
{
public function init()
{
validatesPresenceOf(property="id", message="error msg");
table(mytable);
}
}
</cfscript>
Now the id column in my table is a auto generated field via a trigger, so I don't really need to see if the id is NOT NULL except in the below situation.
<select name=data>
<option value=""></option>
<option value=data.id></option>
</select>
Above, I am using cfwheel validation to check if the use submits a option value with an id and not value that is null. So here I am using the validatesPresenceOf() function to see if a id is submitted.
But in another location I have to save data to this table and my code is as below:
data = model(tbl.others).new();
data.name = name;
data.save();
The above code doesn't insert the record to the table because of validatesPresenceOf() on the id column, and as you can see I am not setting the id on my insert data because it is a auto generate field in my table.
My question is there a work around this, maybe I can temporary turn off that particular id validation or may I can temporary validate the id and then remove that validation condition afterwards.
I tried the following and it doesn't work:
public function custom_save(name)
{
automaticValidations(false);
data = model(mytable).new();
data.name = name;
automaticValidations(true);
return data;
}
you could try not validating at all (using the model)
http://docs.cfwheels.org/v1.4/docs/save
Model Class Functions using save() such as create() and updateAll() use a validate parameter that defaults to true. You could set it to false and do other validations manually.

how to insert multiple data into a single column into the database

Am learning laravel and I encountered a problem saving data into my database from my form.
My instance
when a user tries to make a multiple purchase of products ie.when a user purchases more than one product,i wanted to save the names of products that belongs to the purchase user made into my 'PURCHASES' table having an 'ID' of '1'.
Names of product to be save;
1.productA
2.productB
3.productC
Codes
FORM IN MY VIEW
<input type='hidden' name='product_name'
#foreach($order as $order)
value='{{$order->product_name}}'
#endforeach >
MY purchase CONTROLLER
Saving the names;
$purchase = new Purchase;
$purchase->product_name = $posted['product_name'];
$purchase->save();
When i initiate the function i get an error exception reading 'Trying to get property of non-object' from my view from the line;
#foreach($order as $order)
value='{{$order- >product_name}}'
#endforeach >
How do i go about this problem?
There are a lot of strange things in your code unless I'm reading it wrong, but most likely when you're seeing that error in a view it's because you didn't pass that data into the view. Take this example:
Controller
public function showProducts() {
// assuming $order_array is a set of product IDs that is part of an order
$orders = array('products' => Products::whereIn('id', $order_array);
return View::make('your/view' compact('orders'));
}
I don't quite understand what you're trying to do but passing the object array 'orders' to the view allows you to then call your line:
#foreach($orders as $order)
code here
#endforeach

Laravel 4 query inside view

In my previous question, I got to perform a raw SQL query.
I've declared an public function in my controller:
public function deaths()
{
$getdeaths = DB::statement('SELECT * FROM player_deaths ORDER BY time DESC LIMIT 10');
return View::make('main.latestdeaths')->with('getdeaths', $getdeaths);
}
When retrieving data, I want to display players name, so inside the view in a foreach, is tried to run a SQL query.
#foreach($getdeaths as $getdeath)
<tr>
<? $name = DB::select( DB::raw('SELECT name FROM players WHERE id = '$getdeath->player_id'') ) ?>
<td>{{ $getdeath->player_id }}</td>
<td></td>
<td></td>
</tr>
#endforeach
When I try to echo $name, the variable is not defined.
Can I parse it some other way?
here you go :
$getDeaths = DB::table('player_deaths')->orderBy('time','desc')->take(10)->get();
return View::make('main.latestdeaths')->with('getDeaths', $getDeaths);
That will give you an object of all your player_deaths ordered by time.
You are using DB::select so you have to do a get() to retrieve the records, by the way, is not good make SQL queries on the views, don't respect the MVC arquitecture pattern.
look at view composers. basically a filter or class method that gets called when certain view names are rendered and assigns a view variable automatically.
tell the composer to run the query and set the deaths variable for the views you want the information available in.
you can then loop over the variable as if you had assigned it in your view.

Resources