Is that possible to use variable in the onCondition?
Here is the relation:
public function getMybook()
{
return $this->hasOne(Book::className(), ['book_id' => 'idbook'])->onCondition(['user_id' => $user_id]);
}
I want to use it in joinWith like below:
$books = Books::find()->joinWith('myBook')->all();
But how do I send the user_id parameter into the onConditon? Thanks!
You may use closure, for example:
return $this->hasOne(Book::className(), ['book_id' => 'idbook'])->andWhere('user_id = :user_id');
$user_id = 123;
$books = Books::find()->joinWith([
'myBook' => function ($q) use ($user_id) {
$q->addParams([':user_id' => $user_id]);
}
])->all();
Can not use dynamic param in relation function.
But why don't you use where condition:
$books = Books::find()->joinWith('myBook')->where(['myBook.user_id'=>$user_id])->all();
Related
I have 2 parameters that I put in my url. What I want is how to get these 2 parameters, so it can be read from my controller.
I've try this so the controller can read the parameter:
$id = Input::get('id');
but when I check with dd, the parameter is null
Here is my code:
The link a href on view:
{{ URL('/lap_spd/tambahspd/'.$items->nosurat.'/'.$items->id )}}
web php :
Route::get('lap_spd/tambahspd/{id}/{nosurat}', function($id, $nosurat){
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
lapspdController php:
public function tambahuwong($id, $nosurat) {
$id = Input::get('id');
$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat', 'nosurat')
->toSql();
dd($data3);
so the output that I want - is the parameter can pass to variable on controller.
Thanks for respone before and sorry for my bad english.
Pass the parameters in a single array in your web.php file
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
In lapspdController php:
public function tambahuwong($id, $nosurat) {
//remove these lines and use the variables from the parameters directly
//$id = Input::get('id');
//$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat',$nosurat)
->toSql();
dd($data3);
in view:
in route:
Route::get('lap_spd/tambahspd/{id}/{nosurat}','HomeController#tambahuwong')
->name('check');
in controller:
public function tambahuwong($id,$nosurat) {
$check_id=$id;
$nosurat_name=$nosurat;
dd($check_id);
dd($nosurat_name);
}
Does it work if you change
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
to
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
});
?
I am new to CakePHP but I have been using PHP for a while. I am trying to create a helper that would provide the level of access of a user (ACL).
Here is my ACLHelper.php so far
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\ORM\TableRegistry;
class ACLHelper extends Helper{
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id]);
return $acl->first();
}
}
I call this function in my view this way
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
And this is the output
{ "is_items": "4" }
What I need is the function to return true or false if the value of the field equals or is higher then the value of $level provided to the function. Now if I do this :
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
it will return just the value. My problem is that I do not want to specify the field twice.
Thanks in advance for any help
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
// Get the first ACL record right here
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id])->first();
// Compare the requested field against the provided level
return $acl->$acl_field >= $level;
}
I have two different queries which are saved in two variables.I want to pass the variables to view page from controller.
public function getApprovalList(){
// $users = select query..
// $request = select query..
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
solution:
controller
return view('travelerHome',['users'=>$users,'requestList'=>$request]);
view
#foreach ($requestList as $req)
{{$req->traveler_name }}
#endforeach
return view('travelerHome')->with(array('users'=>$users,'request'=>$request));
You can use return view('travelerHome', compact('users', 'request')); too
Do like this
return view('travelerHome')->with('users'=>$users)->with('request'=>$request);
Code with you have written that is correct, you need to remove the comment only, I am also using the same method for pass the variable to View. Here is the example.
return view('admin.product.product.edit', ['product' => $product,
'attribute_set' => $productAttributes,
'category' => $cates,
'images' => $images,
'status' => $status,
'countries' => $countries,
'crane_manufacture' => $crane_manufacture,
'product_category' => $productCategory]);
}
And it's also working
So, in your code you need to
public function getApprovalList(){
$users = 'select query..';
$request = 'select query..';
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
I'm new to Laravel and at the moment I have a piece of code in a Controller which without the while loop it works, it retrieves my query from the database.
public function dash($id, Request $request) {
$user = JWTAuth::parseToken()->authenticate();
$postdata = $request->except('token');
$q = DB::select('SELECT * FROM maps WHERE user_id = :id', ['id' => $id]);
if($q->num_rows > 0){
$check = true;
$maps = array();
while($row = mysqli_fetch_array($q)) {
$product = array(
'auth' => 1,
'id' => $row['id'],
'url' => $row['url'],
'locationData' => json_decode($row['locationData']),
'userData' => json_decode($row['userData']),
'visible' => $row['visible'],
'thedate' => $row['thedate']
);
array_push($maps, $product);
}
} else {
$check = false;
}
return response()->json($maps);
}
I am trying to loop through the returned data from $q and use json_decode on 2 key/val pairs but I can't even get this done right.
Don't use mysqli to iterate over the results (Laravel doesn't use mysqli). Results coming back from Laravel's query builder are Traversable, so you can simply use a foreach loop:
$q = DB::select('...');
foreach($q as $row) {
// ...
}
Each $row is going to be an object and not an array:
$product = array(
'auth' => 1,
'id' => $row->id,
'url' => $row->url,
'locationData' => json_decode($row->locationData),
'userData' => json_decode($row->userData),
'visible' => $row->visible,
'thedate' => $row->thedate
);
You're not using $postdata in that function so remove it.
Do not use mysqli in Laravel. Use models and/or the DB query functionality built in.
You're passing the wrong thing to mysqli_fetch_array. It's always returning a non-false value and that's why the loop never ends.
Why are you looping over the row data? Just return the query results-- they're already an array. If you want things like 'locationData' and 'userData' to be decoded JSON then use a model with methods to do this stuff for you. Remember, with MVC you should always put anything data related into models.
So a better way to do this is with Laravel models and relationships:
// put this with the rest of your models
// User.php
class User extends Model
{
function maps ()
{
return $this->hasMany ('App\Map');
}
}
// Maps.php
class Map extends Model
{
// you're not using this right now, but in case your view needs to get
// this stuff you can use these functions
function getLocationData ()
{
return json_decode ($this->locationData);
}
function getUserData ()
{
return json_decode ($this->userData);
}
}
// now in your controller:
public function dash ($id, Request $request) {
// $user should now be an instance of the User model
$user = JWTAuth::parseToken()->authenticate();
// don't use raw SQL if at all possible
//$q = DB::select('SELECT * FROM maps WHERE user_id = :id', ['id' => $id]);
// notice that User has a relationship to Maps defined!
// and it's a has-many relationship so maps() returns an array
// of Map models
$maps = $user->maps ();
return response()->json($maps);
}
You can loop over $q using a foreach:
foreach ($q as $row) {
// Do work here
}
See the Laravel docs for more information.
I want to make a relation table in table pelatih and table absen. But when I try to make a relation id_pelatih it always input number "3".
This is the controller:
function absen()
{
$this->load->library('user_agent');
$nim = $this->uri->segment(4);
$id_user = $this->session->userdata('user_id');
$id_pelatih = $id_pelatih;
$this->load->model('mabsensi');
$data = array(
'id_pelatih' => $this->mlogin->get_data_pelatih_by_id_user($id_user)->id_pelatih,
'nim' => $nim,
'tanggal' => date('Y-m-d H:i:s'),
'keterangan' => 'Hadir'
);
$this->mabsensi->insert_absensi($data);
redirect($this->agent->referrer());
}
This is the model
function get_data_pelatih_by_id_user($id_user)
{
$this->db->select('*')
->from('pelatih as a, user as u')
->where('a.id_user = u.id_user')
->limit(1);
return $this->db->get()->row();
}
Please tell me how to solved this.
Your model isn't correct. If you want to get data from different tables you should use JOIN. And you have to say which record you need. So, add a WHERE statement to your query.
function get_data_pelatih_by_id_user($id_user)
{
$this->db->SELECT('*')
->FROM('user')
->WHERE('id_user', $id_user) // your parameter, you did not use it
->JOIN('pelatih', 'pelatih.id_user = user.id_user');
return $this->db->get();
}
Also have a look at the documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html