Laravel 4 - Find item by column - laravel

I was wondering if i could get a bit of help.
Is there any way to change the 'find' parameters to search in another column?
If i use
$test = Model::find($id)
This searches the ID field of a table, but what if i want to search by the token column instead.
Is there any way of doing that?
Cheers,

You can use:
$test = Model::where($column, '=', $value)->first()
The find method is attached to the primary key. You could maybe override it in your model, but you have other ways to achieve your goal without overriding it.

Related

Laravel 5: Grabbing a single record from a query using `take`

Right now, I am using Eloquent like this: MobileAppUsers::where('store', '=', 'store_name')->first(); It turns out I don't need to check the store_name because the entire table is within that store's database, so I just want the single record that will be there (there will only be 1).
Without changing any other logic, would the equivalent of MobileAppUsers::where('store', '=', 'store_name')->first(); (ignoring the store_name) be :
MobileAppUsers::take(1)->get(); ?
you can use this
MobileAppUsers::first();
Regards, I hope this help you

query to get only one item from target column in Laravel

Is there any query to get only one item from target column? below didn't working?
App\Job::whereNotNull('deleted_at')->pluck('customer_name')->first()
I wish to get just one item name from 'customer_name'
Now Laravel 5.6 using...
When using eloquent, using first will give you a single item, the first record matching your query. You can then access the property on that item:
// This will give you an instance of App\Job (or null)
$job = App\Job::whereNotNull('deleted_at')->first();
// You can then access the customer_name property on the object
$job->customer_name;
If you only want to retrieve that single column when you run your query, you can pass an array of columns to first.
App\Job::whereNotNull('deleted_at')->first(['customer_name']);
Assuming you're using the SoftDeletes trait in your model, your query will automatically have an additional check added to all of your queries to ensure that deleted_at is null. So when you're doing ->whereNotNull('deleted_at') you're adding an additional clause to ensure records are not null as well, so you won't have any records being returned.
If you want to look only at deleted records, you can use onlyTrashed():
App\Job::onlyTrashed()->first();
There is a built-in method for this: value('field')
App\Job::whereNotNull('deleted_at')->value('customer_name');
Docs: https://laravel.com/docs/5.6/queries, look for "Retrieving A Single Row / Column From A Table"
If you meant only one field but still as rows, you can use ->select('field') instead.
And since you want only trashed items, you can use ->onlyTrashed() instead of the not null check.
Soft deleting documentation: https://laravel.com/docs/5.6/eloquent#deleting-models
Try like this:
$job = \App\Job::selectRaw('customer_name')
->whereNotNull('deleted_at')
->first();
And use it like $job->customer_name

Qlikview: Matching columns of two indirectly link tables does not work

Following is the data model of the dashboard I am facing problem in:
http://blob:http://stackoverflow.com/f3e40cfe-e009-4d03-bcf5-b7b4305c18c4
Now, what i want to achieve is that in Case there is a filed named Manufacturing_Date. And in MWODefetcs there is a field named Defect_Date. What i want is that when ever a record is selected from a table containing cases from Case corresponding records are shown in another table based on the exact match of Manufacturing_Date=Defect_Date.
As simple as it sounds, i can not seem to accomplish it. I have tried the following expressions to no avail:
Count({<[Defect_Date_text]=p([Manu_text]),FaultID=,DEFECT_CODE=>}MFG_BARCODE_NUM)
sum({$<Defect_Date ={"=$(Manufacturing_Date__c)"}>}Defect_Date)
Do the 2 tables need to be directly linked. Is it the intermediary iFaults table that is preventing me to accomplish it?
Please help.
you should use the P() set expression like this:
sum({$<Defect_Date =P(Manufacturing_Date__c) >}Defect_Date)

How to stop Doctrine from returning a primary key for every query

I am kind of annoyed at Doctrine for returning primary keys in each and every query even though I don't want it to. Is there anyway to stop this ? coz I don't really want those damn primary keys along with my doctrine query results.
A query for instance that I have is:
$getAllDatesForUserQuery = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_ARRAY) ;
In this situation, it retrieves all the datenames as it should, but also happily returns the primary key column value. I DON"T WANT IT.
Is it me? or is it Doctrine ?
In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:
$q = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_SINGLE_SCALAR);
You should find that the query will return a simple one-dimensional array containing only the value(s) you wanted.

asp.net find datatable item

hi i have a datatable with 2 columns one for id(unique) and another for "description". now i have a dropdownlist with "description" from the same datatable mentioned above. when users selects a particular item from description, i need to pick the id corresponding to the selected value. what is the easiest way to achieve this, should i use a foreach or datatable find method.
You should set the Dropdownlist's DataTextField to the Description and its DataValueField to the ID.
MyDropDownList.DataSource = MyDataTable
MyDropDownList.DataTextField = "Description"
MyDropDownList.DataValueField = "ID"
MyDropDownList.DataBind()
Then you can get the ID via MyDropDownList.SelectedValue.
firstly, you might want to give some more information- what programming language you're using (I'm assuming C#)
now, regarding what you've described, you probably want to use the datatable's find method, or, preferrably- use LINQ on the datatable.
table.First(x=> x.description == desc).Id;
(note that this code assumes that an element with a matching description exists. If such an element may not exist- use FirstOrDefault and check the return result for null value).
good luck.
A quick example of this can be found here
http://forums.asp.net/p/1590755/4029475.aspx
Using the answer provided above yoyuu would then use dropdownlist.selecteditem.value, hope this helps.

Resources