In Kohana V3 is it possible to return result set as an array() or any method exists?
For example:
$user = DB::select('*')->from("users")->where('username', '=', $username);
If method is there,then it is possible to get password like
echo $user->password;
Is it possible without ORM? Please suggest.
I think the following would give you all results:
$user = DB::select('*')->from("users")->where('username', '=', $username)->as_object()->execute();
Whereas the following here, would give you the first item:
$user = DB::select('*')->from("users")->where('username', '=', $username)->as_object()->execute()->current();
Try: KO3 Database Wiki
You just need to add a ->current() to the end of your query:
$user = DB::select('*')->from("users")->where('username', '=', $username)->execute()->current();
Related
I want to join multiple tables in laravel with query builder. My problem is that my code only works if I specify the id myself that I want like this:
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=','1')
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
But I would want something like this(which I just can't seem to figure out)
public function showuser($id)
{
$userid = User::findOrFail($id);
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$userid)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
}
Am I making a syntax mistake? When I check the page for my json response in second page it just returns empty brackets, but when I specify the id it fetches me the right data
The findOrFail method will return the entire user model, with all its properties, since you already have the user id. You dont need to get the entire user model for that, you could just use the $id you receveid as a parameter like this:
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$id)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
public function showuser($id)
{
$getUserByID = User::findOrFail($id); //not used
$userData = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$id)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($userData);
}
But the best way is to have relations set on models
public function showuser($id)
{
$userData = User::where('id', $id)->with(['activitates','taga_cars','clients'])->first();
return response()->json($userData);
}
I'm retrieving the rows from user table and I'm using below code.
<?php $user = User::get(); ?>
I want to add array limit for $user data. I don't want to use paginate();. To add limit I'm using below code but it's not working
$users = array_slice($users, 0,2);
But it's showing below error message
exception 'ErrorException' with message 'array_slice() expects
parameter 1 to be array, object given' in........
How to I add limit to $user?
<?php
$user = User::get();
$user = $user ->limit(10);
?>
try limit or
<?php
$user = User::get();
$user = $user ->take(10);
?>
In recent Laravel versions you can also use:
User::limit(10)->offset(0)->get();
Note that User model must extend Eloquent.
Do you mean to use:
$users = array_slice($user, 0,2);
You are getting the error because $users is a collection, not an array.
You could use the take method
$users = User::get()->take(3);
or the slice method
$users = User::get()->slice(3);
Here are some ways you can achieve this.
Applying the offset and limit directly to the query builder allows you to fetch only the needed rows.
$users = User::skip(5)->take(10)->get();
$users = User::offset(5)->limit(10)->get();
If you insist on working with complete result, then this approach is what to you need. You need to use collection slice method not array_slice since the result is a collection.
$users = User::all();
// Collection slice(offset, limit)
$users = $users->slice(5, 10);
I am trying to get the records from a database where players have either played as player 1 or 2 in a game to calculate the gross profit for a player on a profile. I have the following Advanced Eloquent code:
$user = User::with('profile')->where('name', $username)->first();
$user_id = $user->id;
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
$games = $games_model->limit(3)->get();
// get gross profits
$gross_profits = $games_model->sum('bet');
dd($games_model->get()->toArray());
But when I try to take a look at the results dumped, I see a blank array. I am sure my user's ID is set correctly. The funny thing is it was only returning one result before instead of all the resulkts in the database. I'm confused. Thanks for the help in advance!
Something like this should work, assuming the database and naming conventions are follow.
Users::class
public function games()
{
return $this->hasMany(Games::class);
}
Query:
Users::where('name', $username)
->has('games')
->with('profile')
->limit(3)
->get();
Haven't tested but this should give you an idea.
here's the solution.
global dosen't work every time depending on variable scope. I changed
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
to
$user_id = $user->id;
$games_model = Games::where(function ($q) use ($user_id) {
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
and it works perfectly every time.
I'm trying to use fill() to update a current row in the database. However, it seems to be creating a new row instead of updating each time.
Does anyone know what could be wrong?
Here's my code:
$reserve->where('cookie_id', 'idCode')->first();
$reserve->fill($request->all())->save();
return Redirect::to('checkout');
Try something similar to this
$user = User::where ('cookie_id', 'idCode');
$new_user_data = $request->all();
$user->fill($new_user_data);
$user->save();
You can also try using update()
$affectedRows = User::where('votes', '>', 100)->update($request->all());
Judging by your code you've misunderstood how to fetch an instance out the database. See http://laravel.com/docs/5.1/eloquent#retrieving-single-models
Try the following
$reserve = Reserve::where('cookie_id', $id)->first();
$reserve->fill($request->input())->save();
return redirect()->to('checkout');
I have a problem that I can't fix :
this request works in PHPMYADMIN
SELECT cronState.name, command.name, parameter.keyword, eventParameterValue.value
FROM cronState, commandInstance, command, eventParameterValue, parameter
WHERE cronState.id = commandInstance.cronState_id
AND commandInstance.command_id = command.id
AND eventParameterValue.commandInstance_id = commandInstance.id
AND eventParameterValue.parameter_id = parameter.id
AND cronState.id =32
it returns :
name name keyword value
on20 DigitalPinSet State 1
on20 PwmPinSet DutyCycle 20
and it's ALL I want.
LITTLE EDIT : I use an AJAX Request(with a state_name) to this method which should returns the query result.
Now, when I try to implement it with the Laravel Query Builder, it returns some errors.
My code:
$cronState_id = DB::table('cronState')
->where('name', Input::get('state_name'))
->first();
$cronEvent = DB::table('cronState')
->join('commandInstance', 'commandInstance.cronState_id', '=', 'cronState.id')
->join('command', 'commandInstance.command_id', '=', 'command.id')
->join('eventParameterValue', 'eventParameterValue.commandInstance_id', '=', 'commandInstance.id')
->join('eventParameterValue', 'eventParameterValue.parameter_id', '=', 'parameter.id')
->where('cronState.id', '=', $cronState_id->id)
->select('cronState.name', 'command.name', 'parameter.keyword', 'eventParameterValue.value');
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
The error for this code :
{"error":{"type":"ErrorException","message":"Undefined index:
keyword","file":"/var/www/stage/app/controllers/EventController.php","line":48}}
if I change
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
to
return $cronEvent;
(just delete the foreach loop and rename the returning variable), I have this error :
{"error":{"type":"ErrorException","message":"Object of class
Illuminate\Database\Query\Builder could not be converted to
string","file":"/var/www/stage/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php","line":361}}
So, how can access to my data in the object ? Maybe my Query Builder implementation doesn't works...
Thanks for any help !
There is missing get() at the end of that query builder, thus you don't run the query at all. That's it.