laravel the best way to check the contents of a variable - laravel

I have a few questions, the first one is:
What is the best way to check if the variable has any records from the database. e.g when I want get single row from collection:
$variable->sth->first()->url
I get reply:
Call to a member function first() on null.
So if I will use something like this:
#if($variable->sth->count() > 0)
code here
#endif
is this correct ? and what's the difference between
#if($variable->sth->count() > 0) and #if(count($variable->sth)>0 #endif because both work, but not always
second question is: Why when I want get one row from collection and when collection is empty an error appears? but when I use foreach to get all rows from db and when collection whether row is empty, nothing is not appears.
the third question is: when should I check that, the returned data isn't empty?
I'am newbie, sorry.

What is the best way to check if the variable has any records from the database.
You can use laravel methods like dd() or use var_dump() to see the content of a variable.
I get reply: Call to a member function first() on null.
That happens because property sth in $variable is null so it doesn't contains a function or member called first()
Why when I want get one row from collection and when collection is empty an error appears? but when I use foreach to get all rows from db and when collection whether row is empty, nothing is not appears.
Foreach evaluates first if there is data on the collection before doing whatever. You are getting an error because you are accessing a row that maybe doesn't exist. You need to evaluate first if the collection is null or not before accessing its data
when should I check that, the returned data isn't empty?
Before attempting to use it

Related

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

Delete query builder in laravel/eloquent using slim3 returning integer 0

I am using slim3 eloqent/laravel and am trying to build a query to delete an entry from the database using multiple where clauses.
According to laravel's documentation this query should delete correctly;
$deleteGalleryItem = Home_Page::where("ul_id",$ul)
->where("ul_update_no",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
I also tried;
$deleteGalleryItem = Home_Page::where("ul_id","=",$ul)
->where("ul_update_no","=",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
However each time I run the var_dump I get returned integer 0
Is this the correct way to structure a mysql delete statement in eloquent/laravel in slim3?
Or should I first select the data then delete?
There is nothing wrong with the way you've constructed your query.
The number returned is how many rows were removed with that query, so the reason you'll be getting 0 is simply because you didn't have any rows in the database with those constraints.
Hope this Helps!

laravel lists is giving wrong ids for the column values

I am fetching a list of rosters
$rostersList = Roster::where('school_id', $this->schoolId)->get()->lists('id', 'name');
$rostersList->prepend('Select Roster');
but the ids always start from 0,1,2 why is that? However according to the where condition the returned list should not be starting from 0 or 1 but from 4. What can be the possible issue here?
When you prepend a single value without assigning a key with it, the collection gets re-keyed.
Laravel prepend lets you pass a second parameter to use as the key.
So you would want something like:
$rostersList->prepend('Select Roster', '');

How to get value from a specific row and column in MS ACCESS database?

First, I don't have unique value to directly point out a value I want to retrieve, so there for, I cannot use the WHERE Statement. The reason for that, because I have a database where I constantly updated or deleted, so if I use WHERE statement I have to edit the code again.
Then do apply a unique value - add a field of data type AutoNumber or you will never get out of this mess.

Yii ActiveRecord Primary Key retrieval

I have looked around and have not found anything similar to what I am asking.
Short of extending the CActiveRecord class is there a way to query just the primary key values (or any column's values) of a table and have an array of those values returned instead of a collection of activerecord objects?
ie
ModelName::model()->getColumnValues('column_name');
I keep having to get a list of record that match a certain condition and then run through the results to pull out the column values I need. I would like to cut out the last step and just get the values instead of the entire record.
Any ideas?
Using a CDbCommand you can execute a query and fetch the results from one column with the queryColumn method. queryColumn returns the results from the first column.
Example:
$command = Yii::app()->db->createCommand("SELECT column FROM table WHERE ...");
$result = $command->queryColumn();

Resources