I have 2 functions(index and dateRange) .
In index.blade.php I want to use if statement to choose which function it will run.
But when I type :
index{ if($request['from'] != null ) { //run this}
but this time the indexController not runs
request()->ajax() this if statement }..
How can i handle it?
Here is my whole function:
public function index(Request $request)
{
if (!Gate::allows('follow_access')) {
return abort(401);
}
if (request()->ajax()) {
$query = Follow::query()->where('qr_approved', 1)->where('qr_non_approved', 0);
$query->with("competitor_product");
$query->with("product");
$query->with("user");
$template = 'actionsTemplate';
if ($request['From'] != null || $request['To'] != null) {
dd("try me");
}
if (request()->ajax()) {
// NOT WORK
}
}
}
Here updated below code
public function index(Request $request)
{
if (!Gate::allows('follow_access')) {
return abort(401);
}
if ($request->ajax()) {
$query = Follow::query()->where('qr_approved', 1)->where('qr_non_approved', 0);
$query->with("competitor_product");
$query->with("product");
$query->with("user");
$template = 'actionsTemplate';
if ($request['From'] != null || $request['To'] != null) {
dd("try me");
}
if ($request->ajax()) {
// NOT WORK
}
}
}
Related
table:-
1.articles
column:= id, title, description
2.article_likes
column:= id, user_id, article_id
ArticleController.php
public function index (Request $request)
{
$articles = Article::join('article_likes', 'articles.id', '=','article_likes.article_id');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
??
}
$articles = $articles->get();
dd($articles);
}
public function index (Request $request) {
$articles = Article::join('article_likes', 'articles.id', '=','article_likes.article_id');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
$articles->max('like_column_name')
}
$articles = $articles->get();
dd($articles);
}
Let me know if it helps
public function index(Request $request)
{
$articles = Article::with('articleLike');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
$articles->withCount('articleLike')
->orderBy('article_like_count', 'desc');
}
$articles = $articles->get();
return $this->sendResponse("Article Sorted", $articles);
}
This is my function in controller
public function ajaxResponse (Request $request) {
if ($request->ajax()) {
$mat_id = $request->input('mat');
$cat_id = $request->input('cat');
$met_id = $request->input('met');
if ($cat_id != null) {
$products = Product::whereHas("categories", function ($query) use ($cat_id) {
$query->whereIn('category_id', explode(',', $cat_id));
})->get();
}
if ($mat_id != null) {
$products = Product::whereHas("productMaterial", function ($query) use ($mat_id) {
$query->whereIn('product_material_id', explode(',', $mat_id));
})->get();
}
if ($met_id != null) {
$products = Product::whereHas("productionMethod", function ($query) use ($met_id) {
$query->whereIn('production_method_id', explode(',', $met_id));
})->get();
}
if ($cat_id == null && $mat_id == null && $met_id == null) {
$products = Product::all();
}
$prod = view('partials.ajaxProducts', ['products' => $products])->render();
}
return response()->json(['prod' => $prod]);
}
all the record filtered according to $cat_id, $mat_id,$met_id
here categories and productMaterial have Many to many relations with product and productionMethod have one to many relations with the product
i want to filter data via ajax request in combination with all these three relationships
You can use:
public function ajaxResponse(Request $request) {
if ($request->ajax()) {
$mat_id = $request->input('mat');
$cat_id = $request->input('cat');
$met_id = $request->input('met');
$products = Products::query();
if ($cat_id != null) {
$products = $products->whereHas("categories", function ($query) use ($cat_id) {
$query->whereIn('category_id', explode(',', $cat_id));
});
}
if ($mat_id != null) {
$products = $products->whereHas("productMaterial", function ($query) use ($mat_id) {
$query->whereIn('product_material_id', explode(',', $mat_id));
});
}
if ($met_id != null) {
$products = $products->whereHas("productionMethod", function ($query) use ($met_id) {
$query->whereIn('production_method_id', explode(',', $met_id));
});
}
$products = $products->get();
$prod = view('partials.ajaxProducts', ['products' => $products])->render();
}
return response()->json(['prod' => $prod]);
}
public function ajaxResponse(Request $request){
if ($request->ajax()) {
$mat_id = $request->input('mat');
$cat_id = $request->input('cat');
$met_id = $request->input('met');
$products = Product::query();
if ($cat_id != null) {
$products->whereHas("categories", function ($query) use ($cat_id) {
$query->whereIn('category_id', explode(',', $cat_id));
})->get();
}
if ($mat_id != null) {
$products->whereHas("productMaterial", function ($query) use ($mat_id) {
$query->whereIn('product_material_id', explode(',', $mat_id));
})->get();
}
if ($met_id != null) {
$products->whereHas("productionMethod", function ($query) use ($met_id) {
$query->whereIn('production_method_id', explode(',', $met_id));
})->get();
}
$products = $products->get();
$prod = view('partials.ajaxProducts', ['products' => $products])->render();
}
return response()->json(['prod' => $prod]);
}
I have two relashionships in laravel v4.2. When I merge these two relashion into third function and then I call third function then I receive Uncought exception. Below is my code
public function following_friend() {
return $this->hasOne('Friend', 'following_id', 'id');
}
public function follower_friend() {
return $this->hasOne('Friend', 'follower_id', 'id');
}
public function mutual_friends() {
return $this->following_friend->merge($this->follower_friend);
}
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
$users = User::where(function($sql) use($login_id, $users_arr) {
$sql->whereIn('id', $users_arr);
})
->with('mutual_friends')
->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
}
I don't know that where is the problem in merging these two relashionships.
Try to write like below :-
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
$users = User::with('mutual_friends')
->where(function($sql) use($login_id, $users_arr) {
$sql->whereIn('id', $users_arr);
})
->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
}
Since I have used like this for one of my Project :-
public function getShipmentDetails($shipmentId = null) {
$response = Shipment::with('pdDetails','shipmentDocuments')
->where('id', $shipmentId)
->first();
if($response) {
return $response->toArray();
}
}
It works for me.
I keep on getting this error in my codeigniter micro app restful api. When I post an item only the first letter is get saved with status code 400 being displayed.
here is my model file:
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->set($this->setCity($city, null))->insert('cities');
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id;
}
return null;
}
public function update($id, $city)
{
$this->db->set($this->setCity($city))->where('id')->update('cities');
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}
As you can see setCity function treat $city variable as array. So you need to pass array to setCity function.
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->insert('cities',$this->setCity(array('name'=>$city,'id'=> null)));
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$city,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}
another thing is, Codeignitor having method insert_id() to know last insert id.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
class Cities extends REST_Controller{
public function __construct() {
parent::__construct();
$this->load->model('cities_model');
}
public function index_get(){
$cities=$this->cities_model->get();
if(!is_null($cities))
{
$this->response(array('response'=>$cities),200);
}
else
{
$this->response(array('error'=>'cities cannot be found...'),404);
}
}
public function find_get($id){
if(!$id)
{
$this->respose(null,400);
}
$cit=$this->cities_model->get($id);
if(!is_null($cit))
{
$this->response(array('response'=> $cit),200);
}
else{
$this->response(array('error'=> 'data could not be found...'),404);
}
}
public function index_post(){
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$id=$this->cities_model->save($this->post('city'));
if(!is_null($id))
{
$this->response(array('response'=> $id),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_put(){
// for put you need to pass id as parameter
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('id','ID','trim|required|integer');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$update=$this->cities_model->update($this->post('id'),$this->post('city'));
if(!is_null($update))
{
$this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_delete($id){
if(!$id)
{
$this->response(null,400);
}
$del=$this->cities_model->delete($id);
if(!is_null($del))
{
$this->response(array('response'=> 'item successfully deleted'),200);
}
else{
$this->response(array('error'=> 'delete operations could not be done...'),400);
}
}
}
here is the model file:
<?php
class Cities_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get($id=null)
{
if(!is_null($id))
{
$query=$this->db->select('*')->from('cities')->where('id',$id)->get();
if($query->num_rows()===1)
{
return $query->row_array();
}
return null;
}
$sql=$this->db->select('*')->from('cities')->get();
if($sql->num_rows()>0)
{
return $sql->result_array();
}
return null;
}
public function save($city)
{
$this->db->insert('cities', array('name'=>$city));
if($this->db->affected_rows()>0)
{
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$city,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array('id'=>$city['id'],
'name'=>$city['name']
);
}
public function delete($id)
{
$this->db->where('id',$id)->delete('cities');
if($this->db->affected_rows()===1)
{
return true;
}
return false;
}
}
I want to do something I don't know if it is feasible.
here's my routes.php
Route::get('mockups/user/skills/{skill}', 'MockupsController#user');
Route::get('mockups/user/tours/{tour}', 'MockupsController#user');
Route::get('mockups/user', 'MockupsController#user');
and my MockupsController#user function
public function user($skill=null,$tour=null){
if($tour ==null && $skill != null)
return View::make('demo/mockups/user/public',array('skill'=>$skill));
if($tour!=null && $skill ==null)
return View::make('demo/mockups/user/public',array('tour'=>$tour));
return View::make('demo/mockups/user/public');
}
if I get the /mockups/user/tours/tour1 url, the controller calls the demo/mockups/user/public view without sending the $tour variable. how to make it works?
EDIT
public function user($skill=null,$tour=null){
echo var_dump($skill);
echo var_dump($tour);
die();
if($tour ==null && $skill != null)
return View::make('demo/mockups/user/public',array('skill'=>$skill));
if($tour!=null && $skill ==null)
return View::make('demo/mockups/user/public',array('tour'=>$tour));
return View::make('demo/mockups/user/public');
}
displays
string(5) "tour1" NULL
I dont understand why you are making it so complicated. Why not just do
Route::get('mockups/user/skills/{skill}', 'MockupsController#skills');
Route::get('mockups/user/tours/{tour}', 'MockupsController#tours');
Route::get('mockups/user', 'MockupsController#user');
public function skills($skill)
{
return View::make('demo/mockups/user/public',array('skill'=>$skill));
}
public function tours($tour)
{
return View::make('demo/mockups/user/public',array('tour'=>$tour));
}
public function user()
{
return View::make('demo/mockups/user/public');
}