Datatable server-side using Eloquent with laravel - laravel

im using bllim-datatables package for laravel server side and i don't understand how to do if, for example, i want to display only posts from user = 2 with eloquent.
$posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));
return Datatables::of($posts)
->filter_column('id', 'where', 'User.id', '=', '2')
->make();
I know this is not gonna work but can someone explain to me the correct way, please ?

Related

Laravel 7 Query with() and using Where()

Hey guys I have a query that looks like this
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
I need to filter the transaction based on the iso_id that belons to the current user logged in.
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
The iso_id I need to compare to, is inside the merchant table
auth()->user()->isIso() returns the correct iso_id if true or sends false if not
So my first try at this was to use where('merchant.iso_id', '=', auth()->user()->isIso())
But that returns that the column does not exist because for some reason, it's not switching from the transaction model to the merchant one.
I am not sure how to use the stuff inside with() as a selector for my where()
Any help would be appreciated!
Try using whereHas to add the constraint:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();

How do I query two related models in Laravel

I'm stuck on a Laravel blog project I'm working on. I have 2 related models, User and Post, and then in my blade file a search form which will query both models for result. In my Controller method, I have:
$q = $request->keyword;
$results = Post::where ('title', 'LIKE', '%{$q}%')->orWhere('body', 'LIKE', '%{$q}%')->with(['user' => function($x){
$x->orWhere('name', 'LIKE', '%{$q}%')->get();
}])->get();
return view('result')->withResults($results);
Issue is I don't get a response in my blade file. I suspect there's a bug in my controller method but I just can't wrap my head around it. I am using Laravel 5.4.

Laravel eloquent multi relations search column

Trying to figure out how to make a search on a specific table from the below Laravel Eloquent query:
$user = \App\User::with('profile', 'languages', 'contacts', 'photos', 'jobs', 'offices', 'socials')->get();
What I'm trying to filter is a user where in the profile table the 'full_name_slug', '=', 'john-doe'
I've tried forming the where clause like 'profile.full_name_slug', '=', 'john-doe' but without success.
Probably this is quite simple but couldn't figure it out from all the Laravel 5 documentation.
Thanks in advance for any help
Cheers
The right syntax to do
User::whereHas('profile', function ($query) {
$query->where('full_name_slug', '=', 'john-doe');
})->get();

Form Model Binding with relationship

I already looked at this post but I can't seem to make it right:
Laravel form model binding
I get this error:
https://gyazo.com/2ea7b7bb6a19d588829447ee1a92053e I use laravel 5.2
for this.
some screenshots:
https://gyazo.com/1b2c35e660dfe1aae69a02703733d083
https://gyazo.com/3d6f294473f6e54650a4a4403dc2777e
https://gyazo.com/a59aebc7362f51f9ac27852ea032f962
To expand on my comment:
Your problem is here:
$user = User::where('id', $id) // Here more specifically, Laravel does not know if you mean id of users table or details table
->leftJoin('user_details', user_details.user_id', '=', 'users.id')
->first();
Rewrite your where statement like this:
$user = User::where('users.id', $id)....
And it should work. Basically since you're joining 2 tables and they both got id you need to specify which id you want to query by.

Laravel-4 Pagination in Query Builder with form inputs

Got a question if anyone can help out.
I have a query that has a collection of parameters and displays some results.
DB::table()
->join()
->where()
->orderby()
->select()
->get();
But the wheres are generated by a form input in the view. Basically this is a bunch of filters to get a table of results. I want to paginate that. if I change the get() to paginate(), and call $result->links() in the template, it does indeeed paginate and generates a bunch of results for me, however the problem is that when you move away from page 1, the links are just a _GET parameter and all the filter input does not get applied.
Is there a way I can have the pagination AND filters going at the same time? What is the laravel way of handling that? Or would I have to build some way of handling filters and pages? Any tips on that?
Cheers!
* Solution *
The solution was to make the form use GET method and persist the old filters to the Pagination using ->appends() method. Below is the modified code to do that if anyone else is looking.
$results = $query->paginate($this->report->inputs->per_page);
$query = array_except( Input::query(), Paginator::getPageName() );
$results->appends($query);
Yes, here's how I do it:
$orderBy = Input::get('orderBy', 'created_at');
$order = Input::get('order', 'DESC');
$limit = Input::get('limit', 100);
$name = Input::get('name', '');
$result = DB::table()
->where('name', 'LIKE', '%' . $name . '%')
->orderBy($orderBy, $order)
->paginate($limit);

Resources