I using Ignited Datatables library but it dose not support WhereNotIn functionality, how can I add this feature to this library? Thanks in advance.
Patch the Datatables class like this:
class Datatables
{
//...
private $where_not_in = array();
//...
public function where_not_in($key_condition, $val = NULL, $backtick_protect = TRUE)
{
$this->where_not_in[] = array($key_condition, $val, $backtick_protect);
$this->ci->db->where_not_in($key_condition, $val, $backtick_protect);
return $this;
}
private function get_total_results($filtering = FALSE)
{
//...
foreach($this->where_not_in as $val)
$this->ci->db->where_not_in($val[0], $val[1], $val[2]);
//...
}
}
I'd like to create a route that takes a required ID, and optional start and end dates ('Ymd'). If dates are omitted, they fall back to a default. (Say last 30 days) and call a controller....lets say 'path#index'
Route::get('/path/{id}/{start?}/{end?}', function($id, $start=null, $end=null)
{
if(!$start)
{
//set start
}
if(!$end)
{
//set end
}
// What is the syntax that goes here to call 'path#index' with $id, $start, and $end?
});
There is no way to call a controller from a Route:::get closure.
Use:
Route::get('/path/{id}/{start?}/{end?}', 'Controller#index');
and handle the parameters in the controller function:
public function index($id, $start = null, $end = null)
{
if (!$start) {
// set start
}
if (!$end) {
// set end
}
// do other stuff
}
This helped me simplify the optional routes parameters (From Laravel Docs):
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
return $name;
});
Or if you have a controller call action in your routes then you could do this:
web.php
Route::get('user/{name?}', 'UsersController#index')->name('user.index');
userscontroller.php
public function index($name = 'John') {
// Do something here
}
I hope this helps someone simplify the optional parameters as it did me!
Laravel 5.6 Routing Parameters - Optional parameters
I would handle it with three paths:
Route::get('/path/{id}/{start}/{end}, ...);
Route::get('/path/{id}/{start}, ...);
Route::get('/path/{id}, ...);
Note the order - you want the full path checked first.
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Find more details here (Laravel 7) : https://laravel.com/docs/7.x/routing#parameters-optional-parameters
You can call a controller action from a route closure like this:
Route::get('{slug}', function ($slug, Request $request) {
$app = app();
$locale = $app->getLocale();
// search for an offer with the given slug
$offer = \App\Offer::whereTranslation('slug', $slug, $locale)->first();
if($offer) {
$controller = $app->make(\App\Http\Controllers\OfferController::class);
return $controller->callAction('show', [$offer, $campaign = NULL]);
} else {
// if no offer is found, search for a campaign with the given slug
$campaign = \App\Campaign::whereTranslation('slug', $slug, $locale)->first();
if($campaign) {
$controller = $app->make(\App\Http\Controllers\CampaignController::class);
return $controller->callAction('show', [$campaign]);
}
}
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
});
What I did was set the optional parameters as query parameters like so:
Example URL:
/getStuff/2019-08-27?type=0&color=red
Route:
Route::get('/getStuff/{date}','Stuff\StuffController#getStuff');
Controller:
public function getStuff($date)
{
// Optional parameters
$type = Input::get("type");
$color = Input::get("color");
}
Solution to your problem without much changes
Route::get('/path/{id}/{start?}/{end?}', function($id, $start=null, $end=null)
{
if(empty($start))
{
$start = Carbon::now()->subDays(30)->format('Y-m-d');
}
if(empty($end))
{
$end = Carbon::now()->subDays(30)->format('Y-m-d');
}
return App\Http\Controllers\HomeController::Path($id,$start,$end);
});
and then
class HomeController extends Controller
{
public static function Path($id, $start, $end)
{
return view('view');
}
}
now the optimal approach is
use App\Http\Controllers\HomeController;
Route::get('/path/{id}/{start?}/{end?}', [HomeController::class, 'Path']);
then
class HomeController extends Controller
{
public function Path(Request $request)
{
if(empty($start))
{
$start = Carbon::now()->subDays(30)->format('Y-m-d');
}
if(empty($end))
{
$end = Carbon::now()->subDays(30)->format('Y-m-d');
}
//your code
return view('view');
}
}
I used a code in my Basecontroller to share data with all the views ( for footer and header informations ) , it worked great but suddenly i was working on something totally different and i've got an error : No query results for model [Variable]
Can't understand why i didn't even modify my BaseController.
Here is my Base Controller :
public function __construct(){
$this->getVariable('horaire');
$this->getVariable('facebook');
$this->getVariable('contact');
$this->getVariable('twitter');
$main_slider = MainSlider::all();
View::share('main_slider',$main_slider);
}
public function getVariable($setting)
{
$variables[$setting] = Variable::where('name', $setting)->FirstOrFail();
$values[$setting] = $variables[$setting]->value;
View::share($setting, $values[$setting]);
}
And what I was working on
Class VariablesController extends BaseController {
public function settings(){
$settings['about'] = Variable::where('name','about')->FirstOrFail();
$settings['contact'] = Variable::where('name','contact')->FirstOrFail();
$settings['horaire'] = Variable::where('name','horaire')->FirstOrFail();
$settings['legals'] = Variable::where('name','legals')->FirstOrFail();
$settings['facebook'] = Variable::where('name','facebook')->FirstOrFail();
$settings['twitter'] = Variable::where('name','twitter')->FirstOrFail();
$settings['contact_email'] = Variable::where('name','contact_email')->FirstOrFail();
return View::make('admin.settings',compact('settings'));
}
public function update(){
$inputs['facebook'] = e(Input::get('facebook'));
$inputs['twitter'] = e(Input::get('twitter'));
$inputs['contact_email'] = e(Input::get('contact_email'));
$inputs['legals'] = e(Input::get('legals'));
$inputs['horaire'] = e(Input::get('horaire'));
$inputs['contact'] = e(Input::get('contact'));
$inputs['about'] = e(Input::get('about'));
foreach($inputs as $name => $input){
echo "$name => $input";
}
}
And my Variable model :
<?php
class Variable extends \Eloquent {
protected $guarded = ['id','created_at','protected_at'];
protected $table = 'variables';
}
Thank you for your helps
Working with the my_model & my_controller for the first time this month within CodeIgniter, I think I almost have this working.
I got an insert function working properly, now I'm trying to add an update to it if there is an ID.
Here's my code:
function upsert_client($client_id = 0)
{
load_model('client_model');
$this->insertMethodJS();
$this->fields['client'] = $this->_prototype_client();
$user_id = get_user_id();
$company_id = get_company_id();
if ($client_id)
{
$this->data['client'] = $this->client_model->get_record($client_id);
}
if (!$this->ion_auth->in_group(GROUP_NAME_MANAGER, $user_id))
{
redirect('members/dashboard');
}
if ($_POST)
{
$this->load->helper('string');
if ($this->_validate_client())
{
$fields = $this->input->post(null , TRUE);
$fields['user_id'] = $user_id;
$fields['company_id'] = $company_id;
$fields['active'] = 1;
if ($client_id)
{
$fields['id'] = $this->client_model->get_record($client_id);
unset($fields['billing']);
$this->client_model->update($client_id, $fields);
}
else
{
unset($fields['billing']);
$this->client_model->insert($fields);
redirect('members/clients/manage_clients');
}
}
}
$this->template->write_view('content',$this->base_path.'/'.build_view_path(__METHOD__), $this->data);
$this->template->render();
}
function _prototype_client()
{
$fields = array();
$fields['id'] = 0;
$fields['name'] = '';
return $fields;
}
And from my client_model:
class Client_model extends MY_Model {
function get_record($client_id)
{
$query = $this->db->select('id')
->where(array('id'=>$client_id))
->get('clients');
return $query->row_array();
}
}
Everytime I try to edit a "client", it just inserts a new one... All I'm currently trying to edit is the "name" field.
My edit button:
<td><button class="btn btn-inverse" style="float: right;" type="button">Edit</button></td>
Any help is appreciated, thanks! And let me know if you need any additional details...
I never worked with ion auth in particular but from what I see you have a couple of functions that are not referenced correctly.
load_model('client_model');
//Should be
$this->load->model('client_model');
also a few other functions should be referenced as
$this->function_name();
//Instead of just
function_name();
//Unless they are in another library
$this->lib_name->function_name();
I'm not sure if this well solve your problems but just a few things I noticed.
Your hyperlink points to 'add_client' whereas the function you are showing is called 'upsert' Are you calling the correct URL?
The domain I'm using is http://www.domainname.com/admin/users/edit/1
class Cal_Admin extends Controller {
function Cal_Admin()
{
parent::Controller();
$this->load->model('events/model_events');
$this->load->model('users/model_users');
}
function index()
{
$data['events'] = $this->model_events->get_home_entries();
$data['users'] = $this->model_users->_get_all();
$this->load->view('admin/admin_head');
$this->load->view('admin/admin_menu');
$this->load->view('admin/admin',$data);
$this->load->view('admin/admin_foot');
}
function users($action=NULL, $uid=NULL)
{
$this->load->view('admin/admin_head');
$this->load->view('admin/admin_menu');
$segments = $this->uri->total_segments();
switch ($action) {
case "add" :
$this->load->view('admin/users/form');
break;
case "edit" :
$data['user'] = $this->model_users->_get_user($uid);
$this->load->view('admin/users/form',$data);
break;
default:
$data['users'] = $this->model_users->_get_all();
$data['action'] = $action;
$this->load->view('admin/users',$data);
}
$this->load->view('admin/admin_foot');
}
I Fixed it myself.
I had the uri library redeclared in the autoload.php file. I know the cal_admin was the class and I was using it in the url, i took it out for some reason in the example.