Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I was using data table template for frontend. It already have per page and pagination and search. So, how should I do for query. I did like this. I also wanna use search and paginate from template datatable.
public function index()
{
$users = User::all();
return view('users.list', compact('users'));
}
You can use the laravel built-in pagination:
public function index()
{
$users = User::paginate(15);
return view('users.list', compact('users'));
}
Then in your view you can loop through your user list and use the pagination links like this:
{{ $users->links() }}
You should customised your code to do so and here you go:
public function index(Request $request)
{
$search = $request->query('search');
$customers = customers::where('name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%")
->paginate(10);
return view('App.Customer.customerList', compact('customers'));
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
intelephense is giving me a syntax error in when writing this code, clearly $category was passed so what is the problem?
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? false, function ($query, $search) {
$query->where('title', 'like', '%' . $search . '%')->orWhere('body', 'like', '%' . $search . '%');
});
$query->when(
$filters['category'] ?? false,
function ($query, $category) {
$query
->whereExists(
function ($query) {
$query->from('categories')
->whereColumn('categories.id', 'posts.category_id')
->where('categories.slug', $category);
}
);
}
);
}
You can pass the necessary variables from the parent scope into the closure with the use keyword.
$query->when(
$filters['category'] ?? false,
function ($query, $category) {
$query
->whereExists(
function ($query) use ($category) {
$query->from('categories')
->whereColumn('categories.id', 'posts.category_id')
->where('categories.slug', $category);
}
);
}
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to get the data of a Model and also the data of the foreign key in the Model
For Example:
A Post Model has a user_id as a foreign key. How can I write an Eloquent query so that I can get the data of a Post and in place of the user_id, I get the data of the user_id ?
Use with method when querying your post to include the user.
$post = Post::with('user')->first();
// or
$posts = Post::with('user')->get();
At Post model
public function user(){
return $this->belongsTo(User::class,'user_id');
}
Now at your PostController
Guess you have a method which is use for showing post
public function show($id){
$post = Post::query()->findOrFail($id);
}
Now if you want to access which user posted then there has two ways.
Way 1 :
public function show($id){
$post = Post::query()->with('user')->findOrFail($id);
}
Way 2 :
public function show($id){
$post = Post::query()->findOrFail($id);
echo $post->user->firstName;
}
Note :
If you just write $post->user then it will show/return User table according to user_id column.
If you want to access these user row column then . Simply $post->user->column_name;
Enjoy Coding.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
$data['customers'] = DB::table('master_customer')->where('ORG_ID', $user_org)->get();//customer table
$document_cat = DB::table('document_category')->get();//category table
foreach ($data['customers'] as $key => $value) {
foreach ($document_cat as $key1 => $value1) {
$docs = DB::table('master_documents')//docuements saved in this table
->where('cust_id', $value->id)
->get();
$document_cat[$key1]->doc = $docs;
}
$data['customers'][$key]->documents = $document_cat;
}
print_r($data);
die;
Why you dont update eloquent relationships in models? If you do so it could be much easier.
MasterCustemer Model
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument','cust_id');
}
MasterDocument Model
public function masterCustomer()
{
return $this->belongsTo('App\correctfoldertoothermodel\MasterCustomer','cust_id');
}
public function documentCategory()
{
return $this->belongsTo('App\correctfoldertoothermodel\DocumentCategory');
}
DocumentCategory Model
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument');
}
Of course you'll have to provide the correct table names in migration for auto implementation of fk or set tables in relationships too.
When it's done you could retrieve data like it:
$masterCustomers->with('masterDocuments.documentCategory')->get();
$masterCustomers->masterDocuments = $masterCustomers->masterDocuments->groupBy(function($item){
return $item->documentCategory->name;
});
The result will be a collection of customers with and property containing an another collection of categories with its documents.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My controller:
function index:
public function index(Request $request)
{
//there is error Undefined variable: jobs
return view('jobs.index')->with('jobs', $jobs);
}
Why
You need to add a $jobs variable in the method.
public function index(Request $request)
{
$jobs = // Some code here;
return view('jobs.index')
->with('jobs', $jobs);
}
the best way to achieve this is by doing something like this:
public function index(Request $request)
{
$data['jobs'] = DB::table('your-table')->get();
return view('jobs.index',$data);
}
with the above code snippet, you could be able to use the variable job in your blade like this:
#foreach($jobs as $j)
{{ $j->fieldName }}
#endforeach
the above is use when you use get() in your database query. Hope it helps!!!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have the following tables (and relevant fields):
users (id, name)
companies (id, name)
sales (id, company_id, user_id)
products (id, description)
products_sales (id, product_id, sale_id)
Now, I have some report generation with multiple concurrent filters:
company name, user name, product.
I want the Company to be the 'top level' object in the result.
So,
Company::where('name', 'LIKE', "%$company_name%")
then
->whereHas('sales', function ($query) use ($user_name) {
$query->whereHas('user', function ($query2) use ($user_name) {
$query2->where('name', 'LIKE', "%$user_name%")
});
});
This is complex enough already, and I also want to eager load all
relationships, to optimize database access, so I ended up with:
$cond = function($q) use ($user_name) {
$cond2 = function ($q2) use ($user_name) {
$q2->where('name', 'LIKE', "%$user_name%");
};
$q->whereHas('user', $cond2)
->with(['user' => $cond2]);
};
Company::where('name', 'LIKE', "%$company_name%")
->whereHas('sales', $cond)
->with(['sales' => $cond]);
I feel this brings unnecessary repetition. Moreover, when filtering
products the same way, I think the eager loading supersedes the previous one.
So... What's the better way to do this?
Can I use Eloquent for this or should I go back to a 'raw' query?
You can inspect the queries that Eloquent generates by using getQueryLog. If your query is looking "less than optimal", you might consider a raw query.
If your query is optimal but the Eloquent syntax looks too "messy", you might consider building some of the partial query scopes into your models.
For example, you could define a scope in your Sales model to match names:
class Sales extends Model
{
/**
* Scope a query to only include Sales for users with a matching name.
*
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeMatchUserName($query, $user_name)
{
$cond2 = function ($q2) use ($user_name) {
$q2->where('name', 'LIKE', "%$user_name%");
};
return $query->with(['user' => $cond2])
->whereHas('user', $cond2);
}
}
Then your query becomes somewhat simpler (looking):
$cond = function($q) use ($user_name) {
return $q->matchUserName($user_name);
};
Company::where('name', 'LIKE', "%$company_name%")
->with(['sales' => $cond])
->whereHas('sales', $cond);
You can even package that query into your Company model:
class Company extends Model
{
/**
* Scope a query to only include Companies and Sales for a matching user and company name.
*
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeMatchCompanyAndUserName($query, $user_name, $company_name)
{
$cond = function($q) use ($user_name) {
return $q->matchUserName($user_name);
};
return $query->where('name', 'LIKE', "%$company_name%")
->with(['sales' => $cond])
->whereHas('sales', $cond);
}
}
Then all you need to do is:
Company::matchCompanyAndUserName($user_name, $company_name);