I want to pass the arguments as String type. But it shows me an error and i can see that it was not passed as a string. Is there a way to pass by string. Following is my function in the controller. (Look on to tag's onclick method from there i need to pass this as a string)
public function show(Request $request)
{
$output="";
if ($request->ajax()){
$models=DB::table('models')->where ('modelName','LIKE','%'.$request->search.'%')
->orWhere('brandName','LIKE','%'.$request->search.'%')->get();
if($models){
foreach ($models as $key=>$model){
$Mid=$model->modelName;
$output.='<tr>'.
'<td>'.$model->countryMade.'</td>'.
'<td>
<a class=" btn btn-success btn-sm" onclick="EditModel('.$model->id.','.$model->modelName.')" >Edit </a>
<a onclick="DeleteModel('.$model->id.')" class=" btn btn-danger btn-sm" >Delete </a>
</td>'.
'</tr>';
}
}
}
return response($output);
}
You can do it as ,
return response($output->__toString());
Related
I am using datatables from https://datatables.net/examples/server_side/simple.html and I am able to display all the information to the tables.
Now I have a column which stores foreign key as shown below.
The other table which is being pointed by source_of_member is
so here i have these kind of relationship and in frontend datatables my data are visualized as
Howeber here instead of foreign keys in Member Source column I want those Name to appear from another table which matches the id
my member model is as:
class Member extends Model
{
protected $fillable = ['name','mobile_number','organization_name','source_of_member','relationship_manager','referred_by'];
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'id');
}
}
and my memberSource model is as:
class MemberSource extends Model
{
protected $fillable = ['name'];
public function member()
{
return $this->hasOne('App\Member', 'id');
}
}
what i tried is:
public function getdata()
{
$members = Member::all();
return Datatables::of($members)
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
}
i dont know how to do this any help would be greatly appreciated.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In Controller:
$members = Member::with('memberSource')->get();
return Datatables::of($members)
->addColumn('source_of_member', function ($member) {
$member_name = '';
if(!empty($member->memberSource->name)){
$member_name = $member->memberSource->name;
}
return $member_name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
With the relationships working properly, you can simply edit the column in the datatable:
$members = Member::with('memberSource');
return Datatables::of($members)
->editColumn('source_of_member', function ($member) {
return $member->memberSource->name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
However, your relationship configuration seems wrong, because your foreign key is called source_of_member and therefore laravel can't automatically detect it.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In your MemberSource model:
public function member()
{
return $this->belongsTo('App\Member', 'source_of_member');
}
I am working on a project using php codeigniter,i am using mysql select query to fetch all data from database where isactive field is equal to 1.but i am unable to do it.below is the function where i have writtten the query.
Code:
public function displayinfo3()
{
$user_id = $this->session->userdata('user_id');
if($user_id)
{
$this->load->library('datatables');
$this->datatables
->select("user_type.user_type as user_type,user.user_name as user_name,user.first_name as first_name,user.middle_name as middle_name,user.last_name as last_name,user.user_id as Actions")
->from("user")
->where('isactive'==1)
->join("user_type","user_type.user_type_id=user.user_type_id","left")
->edit_column("Actions",
"<center><div class='visible-md visible-lg hidden-sm hidden-xs'><div align='center'><div class='col-md-2'><form action='".base_url()."index.php/Registrationc/editpatient3' method='post'><input type='hidden' id='editp3' name='editp3' value='$1'><button title='EDIT PATIENT' class='tip btn btn-primary btn-xs' type='submit' value='Submit' ><i class='fa fa-edit'></i></button></form></div><div class='col-md-2'><a class=\"tip btn btn-danger btn-xs\" title='DELETE PATIENT' href='#' onclick=\"delete_patient('$1')\" ><i class=\"fa fa-trash-o\"></i></a></div></div></div></center>", "Actions");
// $this->datatables->unset_column('Sno.patient_id');<form action=""><div class='btn-group'>
//<a class=\"tip btn btn-primary btn-xs\" onclick=\"edit_patient('$1')\" title='EDIT MAPPING' href='#' return false;\"><i class=\"fa fa-edit\"></i></a>
echo $this->datatables->generate();
}
else
{
redirect(base_url('Registrationc/extra1'));
}
}
Replace ->where('isactive'==1) with ->where('isactive = 1')
The first passes the result of the comparison of 'isactive' with 1 to the function. As 'isactive' is not equal to 1 you just pass a false value to your where statement.
The second call passes the correct query string to be put in the where statement of the query.
You need to change the ->where('isactive'==1) to ->where('isactive = 1') and returns a bool result.
I was using ajax calls. when I click on the edit link it would pop up a modal with all the information. I now want to change it, so that it goes to an edit page instead. The part I need changed is the onclick on the edit link bellow in the code from edit_record to instead call a function in my controller called edit. Any help would be appreciated I spent quit a long time trying different ways and keep getting errors.
Here is the controller function code called ajax_list
public function ajax_list()
{
$list = $this->your_table->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $your_table) {
$no++;
$row = array();
$row[] = $your_table->name;
$row[] = $your_table->title;
$row[] = $your_table->body;
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="javascript:void()" title="Edit" onclick="edit_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->your_table->count_all(),
"recordsFiltered" => $this->your_table->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
Here is the code in my your_controller called edit
//loads a single record with the data dispalyed for editing
function edit($id)
{
$data['r']=$this->your_table->get_by_id($id);
$this->load->view('pages/edit', $data);
}
//updates data in the database from the edit function
function save_edit()
{
$id=$this->input->post('txtid');
$data=array(
'name'=>$this->input->post('txtname'),
'title'=>$this->input->post('txttitle'),
'body'=>$this->input->post('txtbody'));
$this->db->where('id', $id);
$this->db->update('your_table', $data);
redirect('your_controller/index');
}
If your controller is called "edit" and your method has the same name, just change to:
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="/edit/edit/'.$your->table->id.'" title="Edit"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
My view is like this :
...
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create.year', [$year]) !!}">
Add New
</a>
...
...
#foreach($testArray as $key)
...
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$key['display'], $year]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>
...
#endforeach
...
My routes is like this :
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
My controller is like this :
public function create($year, $display = null)
{
echo $display.' : display <br>';
echo $param_thang. ' : year';die();
...
}
When I call url like this : http://localhost/mysystem/public/users/create/2016, it works.
There result is like this :
: display
2016 : year
When I call url like this : http://localhost/mysystem/public/users/create/14144499452111901/2016
The result is like this :
2016 : display
14144499452111901 : year
It looks strange, the result is reversed
Why did it happen?
It's because your route is:
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
So the first parameter is always going to be the year, and the 2nd parameter is always going to be the display variable.
You need to change your <a> as #farkie suggested:
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$year,$key['display']]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>
I have a link that looks like this:
<a class="btn btn-primary edit"
href="Personas/details/<?php echo $persona['Persona']['id']; ?>"
data-original-title="Editar">
<i class="icon-pencil icon-white"></i>
</a>
As you can see I'm manually writing in the controller and action for the link. Is there some way to make this not as brittle?
Something like:
<a href="Url.Action("Personas", "Details", array('id', 1);" >Asdf</a>
Use: Html->url, does exactly what you want.