When using laravel built in laravel model methods returns a list of result array. But when querying the same table getting different object. Please check the below code - how can I get the same result set?
$users = User::all();
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 10
[email] => amirtha#gmail.com
)
[1] => stdClass Object
(
[id] => 12
[email] => renjith#123.com
)
)
But when using the sql query like the below:
$result = DB::table('users as u')
->select('u.id','u.email','u.role','u.created_at')
->join('roles as r','r.id','=','u.role')
->where('u.role', '!=', 1 )
->orderBy('u.name','asc')
->get();
print_r($result);
I get:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\User Object
(
[fillable:protected] => Array
(
[0] => name
[1] => email
[2] => password
[3] => lastname
[4] => mobile
[5] => role
)
[hidden:protected] => Array
(
[0] => password
[1] => remember_token
)
[casts:protected] => Array
(
[email_verified_at] => datetime
)
[connection:protected] => mysql
[table:protected] => users
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[name] => amritha
[email] => amritha#gmail.com
)
I think your problem is when you want to use the table alias so what about this:
$result = User::query()->from('User as u')
->select('u.id','u.email','u.role','u.created_at')
->join('roles as r','r.id','=','u.role')
->where('u.role', '!=', 1 )
->orderBy('u.name','asc')
->get();
.
This query is the same with "SELECT * FROM USERS"
$users = User::all();
While this one is different from the earlier query
$result = DB::table('users as u')
->select('u.id','u.email','u.role','u.created_at')
->join('roles as r','r.id','=','u.role')
->where('u.role', '!=', 1 )
->orderBy('u.name','asc')
->get();
print_r($result);
Related
I have this query, that produces next result
$result = DB::table('contagenshora')
->select(DB::raw('post_id as post_id'), DB::raw('product_id'), DB::raw('max(val) as maxHour'), DB::raw('hour'))
->whereBetween('date', array($dt->toDateTimeString(), $dt2->toDateTimeString()))
->where('hour', '=', $actHour->hour)
->groupBy(DB::raw('post_id'), DB::raw('product_id'))
->get();
Result: Array ( [0] => stdClass Object ( [post_id] => 1 [product_id] => 1 [maxHour] => 4 ) [1] => stdClass Object ( [post_id] => 1 [product_id] => 2 [maxHour] => 3 ) [2] => stdClass Object ( [post_id] => 4 [product_id] => 0 [maxHour] => 6 ) ) )
Now, I need the sum of "maxHour", but grouped by "post_id", I need something like this:
(For example, for post_id = 1, I need the sum of 4+3)
Result: Array ( [0] => stdClass Object ( [post_id] => 1 [maxHour] => 7 ) [1] => stdClass Object ( [post_id] => 4 [maxHour] => 6 ) ) )
How can I do this?
Just remove the product_id from group by clause and select then. Also no need to use DB::raw() for direct selectable columns.
$result = DB::table('contagenshora')
->select('post_id', DB::raw('max(val) as maxHour'))
->whereBetween('date', array($dt->toDateTimeString(), $dt2->toDateTimeString()))
->where('hour', $actHour->hour)
->groupBy('post_id')
->get();
code
$data = Event::with(['favourite' => function ($q){
$q->select('*');
$q->groupBy(function ($date) {
return Carbon::parse($date->created_at)->format('M'); **// this line have error**
});
}])
->select('id', 'title')
->get()
->where('id', $eventid)
->toArray();
//if not group by the data then the output will become
[0] => Array
(
[id] => 3
[title] => Everything You Need to Know About Business From Start-up to IPO, Straight From the VC's Mouth
[favourite] => Array
(
[0] => Array
(
[created_at] => 2020-09-10T17:54:06.000000Z
)
[1] => Array
(
[created_at] => 2020-09-10T18:21:00.000000Z
)
[2] => Array
(
[created_at] => 2020-09-28T15:05:38.000000Z
)
[3] => Array
(
[created_at] => 2020-08-28T15:05:38.000000Z
)
)
)
Final data should be
[0] => Array
(
[id] => 3
[title] => Everything You Need to Know About Business From Start-up to IPO, Straight From the VC's Mouth
[favourite] => Array
(
[Aug] => Array
(
[total] => 1
)
[Sep] => Array
(
[total] => 3
)
)
)
Anyone facing this error before? When I try to separate the data as a group. If will return error stripos() expects parameter 1 to be string, object given. The above code will return all the data from database.
I want to shuffel an array. But it will not work. This is my Query in Laravel
$spielerArray = Spieler::join('PlanungSpieler', 'PlanungSpieler.Player_ID', '=', 'Spieler.Player_ID')
->select('Spieler.Player_ID')->get();
And in the next step I want only shuffle the column Spieler.Player.
$finalShuffleResult = shuffle($spielerArray->Player_ID)
I get Player_ID Instance does not exist on this collection
Here is a part of my collection
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Spieler Object
(
[table:protected] => Spieler
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[Player_ID] => 101
)
How can I get Player_ID with the goal to shuffle them?
use this
$finalShuffleResult = $spielerArray->pluck('Player_ID')->shuffle()->all();
I have been trying to pull data from cake. But I keep on getting the object data when I view via print_r
$userDetails = $this->UserDetails->find()->where(['UserDetails.user_id' => $id])->all();
print_r($userDetails);
Result
Cake\ORM\ResultSet Object ( [items] => Array ( [0] => App\Model\Entity\UserDetail Object ( [id] => 6 [user_id] => 10 [firstName] => Rey Norbert [lastName] => Besmonte [address] => [contact] => [age] => [created] => Cake\I18n\FrozenTime Object ( [time] => 2017-05-24T03:10:55+00:00 [timezone] => UTC [fixedNowTime] => ) [modified] => Cake\I18n\FrozenTime Object ( [time] => 2017-05-24T03:10:55+00:00 [timezone] => UTC [fixedNowTime] => ) [[new]] => [[accessible]] => Array ( [*] => 1 [id] => ) [[dirty]] => Array ( ) [[original]] => Array ( ) [[virtual]] => Array ( ) [[errors]] => Array ( ) [[invalid]] => Array ( ) [[repository]] => UserDetails ) ) )
I am not sure why ake\ORM\ResultSet Object ( [items] => Array ( [0] => App\Model\Entity\UserDetail Object is showing.
In php cake 2.x I have no problem with this.
Cakephp 3.x outputs object instead of array().
If you want array you can use toArray();
Example:-
$userDetails = $this->UserDetails->find('all')->where(['UserDetails.user_id' => $id])->toArray();
print_r($userDetails);
And if you want to return only single row then you can use first()
Example:-
$userDetails = $this->UserDetails->find('all')->where(['UserDetails.user_id' => $id])->first();
print_r($userDetails);
The query’s execute() method is called. This will return the
underlying statement object, and is to be used with
insert/update/delete queries.
The query’s first() method is called. This will return the first
result in the set built by SELECT (it adds LIMIT 1 to the query).
The query’s all() method is called. This will return the result set
and can only be used with SELECT statements.
The query’s toArray() method is called.
Use any of this according to your requirment :
$userDetails = $this->UserDetails->find()->where(['UserDetails.user_id' => $id])->all();
I am working with laravel 5 pagination and getting some problems.
Here is my code DB::table('users')->paginate(2) and it returns an object of LengthAwarePaginator Like this :
Illuminate\Pagination\LengthAwarePaginator Object
(
[total:protected] => 4
[lastPage:protected] => 2
[items:protected] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 3
[Name] => shivani
[email] => shivani.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
[1] => stdClass Object
(
[id] => 4
[Name] => shivani
[email] => shivani2.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
)
)
[perPage:protected] => 2
[currentPage:protected] => 1
[path:protected] => http://localhost/shivani/public/check/user-list
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
My problem is that I want to iterate through this object and retrieve items object that holds database data i.e. name , email etc.
But I don't know how to access protected members from the returned object.
$data = DB::table('users')->paginate(2)->toArray()
Try this.. it might work for you
You can just use the returned object like it where an array:
$users = DB::table('users')->paginate(2);
foreach($users as $user){
echo $user->email;
}
You can access paginated objects same way you can access any other collection. For example
foreach(DB::table('users')->paginate(2) as $object) {
echo $object->name;
}