How to declare the result from another function in Laravel - ajax

The first 2 functions are for the dropdown, and the last function filter function is to fetch the data into datatable. I want to use the results from function FirstName ($FirstName= [];) and LastName ($LastName= [];) for this purpose.
public function FirstName(Request $request)
{
$FirstName= [];
if($request->has('q')){
$search = $request->q;
$FirstName= DB::table("user")
->select("id","First_Name")
->where('First_Name','LIKE',"%$search%")
->get();
}
return response()->json($FirstName);
}
public function LastName(Request $request)
{
$LastName= [];
if($request->has('q')){
$search = $request->q;
$LastName= DB::table("user")
->select("id","Last_Name")
->where('Last_Name','LIKE',"%$search%")
->get();
}
return response()->json($Last_Name);
}
public function filter()
{
if(!empty($FirstName)) //catch from other function
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->where('First_Name', $FirstName)
->where('Last_Name', $LastName)
->get();
}
else
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->get();
}
return datatables()->of($data)->make(true);
$data = DB::table('user')
->select('Last_Name','First_Name')
->groupBy('First_Name')
->groupBy('Last_Name')
->orderBy('First_Name', 'ASC')
->orderBy('Last_Name', 'ASC')
->get();
return response()->json($data);
}
What I did above is put the array from function first and last name to the filter function. But i got it wrong. What should be done to fix this?

Related

How to search from relationship data ? In laravel

public function scopeSearch($query, $value)
{
$searchValues = explode(' ', $value);
if (!$value) return $query;
return $query->where(function ($q) use ($searchValues) {
foreach ($searchValues as $token) {
$q->orWhere('name', 'like', "%{$token}%");
$q->orWhere('street', 'like', "%{$token}%");
}
});
}
I want to search the data. This model also has
public function brands()
{
return $this->belongsToMany(Brand::class, 'dealer_brands');
}
public function province()
{
return $this->belongsTo(Province::class);
}
How can I get data from the relastionship. Like Dealer(model) has data Nmae = josh , brand_id = 1 {brand.name = samsung} , province_id = 2 (province.name = "aligora"). When I search Josh Samsung Alogora, I want to ge the data. When I only search aligora, I want to get the data of model having province aligora. hOW CAN I MODIFY CODE?
looking at your model relationships. This may work for you
public function scopeSearch($query, $value)
{
if (!$value) return $query;
$searchValues = explode(' ', $value);
return $query->where(function ($q) use ($searchValues) {
foreach ($searchValues as $token) {
$q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%")
->orWhereHas('brands', function ($sub_q) use ($searchValues) {
$sub_q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%");
})
->orWhereHas('province', function ($sub_q) use ($searchValues) {
$sub_q->where('name', 'like', "%{$token}%")
->orWhere('street', 'like', "%{$token}%");
});
}
});
}
I have no idea about column names in related tables so repeating name and street. you can change as per requirement

Laravel WhereHas Query

I wanna get data which is pwra_dt = $month && $year. What can I do? For now, it shows all records!!
Query:
$purchaseOrder = PurchaseOrder::whereHas('pwra', function (Builder $query) use ($year, $month) {
$query->whereYear('pwra_dt', $year)
->whereMonth('pwra_dt', $month);
})
->get();
PurchaseOrder(Model)
public function pwra()
{
return $this->hasMany('App\Models\Pwra', 'purchase_order_uuid', 'purchase_order_uuid');
}
PWRA (Model)
public function purchaseOrder()
{
return $this->belongsTo('App\Models\PurchaseOrder', 'purchase_order_uuid', 'purchase_order_uuid');
}

How can I combine both of these conditions? in laravel

I'am new with laravel. How can I combine both of these conditions?
public function student($classroom_id)
{
$members = Classroom::findOrFail($classroom_id,)->members->load('role');
$members = User::where([
['role_id', 2]
])->get();
return response()->json($members);
}
Try this:
public function student($classroom_id)
{
$members = Classroom::whereHas('members.role', function ($query) {
$query->where('id', '2');
})->findOrFail($classroom_id)->members->load('role');
return response()->json($members);
}

How can i check request comes or not in laravel?

I want to get $data['student'] if request get (like: student-guardian?reg_no=0001 in URL).
if no any parameter on url $data['student'] don't needed.
My StudentGuardian function Like:
public function studentGuardian(Request $request)
{
$data = [];
if(!empty($request)){
$data['student'] = Student::select('id','reg_no','reg_date', 'first_name', 'middle_name', 'last_name','faculty', 'semester','status')
->where(function ($query) use ($request) {
if ($request->has('reg_no')) {
$query->where('students.reg_no', 'like', '%'.$request->reg_no.'%');
$this->filter_query['students.reg_no'] = $request->reg_no;
}
})
->get();
}
return view(('studentguardian.index'), compact('data'));
}
This is what you need:
public function studentGuardian(Request $request)
{
$data = [];
// check request has any parameter
if($request->all()){
// query and get list student
$data['student'] = Student::select('id','reg_no','reg_date', 'first_name', 'middle_name', 'last_name','faculty', 'semester','status')
->where(function ($query) use ($request) {
if ($request->has('reg_no')) {
$query->where('students.reg_no', 'like', '%'.$request->reg_no.'%');
$this->filter_query['students.reg_no'] = $request->reg_no;
}
})
->get();
} else {
//do something else
}
// return data to view
return view('studentguardian.index', compact('data'));
}
First, your if(!empty($request)) is useless, $request is always a Request object, not empty.
Second, you can use just if($request->all()) { ... } to check if request array is empty.

Laravel - passing array of columns in 'where' clause

I have a search query like this:
$data = User::where('first_name', 'like', '%'.$query.'%')
->orWhere('last_name', 'like', '%'.$query.'%')
->get();
Now, I have many models, each with different column names. Instead of defining a search() function into every controller, I want to do this:
// User
public static function searchableFields()
{
return ['first_name', 'last_name'];
}
// Some other model
public static function searchableFields()
{
return ['name', 'description'];
}
And put the search logic in a shared controller, something like this:
$data = $classname::
where($classname::searchableFields(), 'like', '%'.$query.'%')
->get();
How can I achieve this?
Thanks a lot.
You can loop over the fields and add them to your Eloquent query one by one.
$data = $classname::where(function ($query) use ($classname) {
foreach ($classname::searchableFields() as $field)
$query->orWhere($field, 'like', '%' . $query . '%');
})->get();
I would use scope for that.
You can create base model that all the models should extend (and this model should extend Eloquent model) and in this model you should add such method:
public function scopeMatchingSearch($query, $string)
{
$query->where(function($q) use ($string) {
foreach (static::searchableFields() as $field) {
$q->orWhere($field, 'LIKE', '%'.$string.'%');
}
});
}
Now you can make a search like this:
$data = User::matchingSearch($query)->get();
Just to avoid confusion - $query parameter passed to matchingSearch becomes $string parameter in this method.
You can try something like this.
// Controller
function getIndex(Request $request)
{
$this->data['users'] = User::orderBy('first_name','asc')->get();
if ($request->has('keyword')) {
$results = User::search($request->keyword);
$this->data['users'] = collect([$results])->collapse()->sortBy('first_name');
}
}
// Model
function search($keyword)
{
$results = [];
$search_list = ['first_name','last_name'];
foreach ($search_list as $value)
{
$search_data = User::where($value,'LIKE','%'.$keyword.'%')->get();
foreach ($search_data as $search_value) {
$exist = 0;
if (count($results)) {
foreach ($results as $v) {
if ($search_value->id == $v->id) {
$exist++;
}
}
if ($exist == 0) {
$results[] = $search_value;
}
} else{
$results[] = $search_value;
}
}
}
return $results;
}

Resources