I am new to laravel and I am having error in adding paginate using laravel eloquent.
This code work without paginate(). If paginate was added, got error
Method paginate does not exist.
$articles = Article::orderBy('updated_at', 'DESC')
->findOrFail([1,2,3,4,5])
->where('status','p')
->paginate(7);
As people have mentioned in the comments, you can not use findOrFail() with paginate() since they are both ways to execute a query. You can instead use whereIn().
To get what you're after you can do:
$articles = Article::orderBy('updated_at', 'DESC')
->whereIn('id', [1,2,3,4,5]) //assuming "id" is the primary key for the table
->where('status','p')
->paginate(7);
Related
There's a problem about laravel paginate with orderby.
$products->with(['cover'])
->orderBy('priority', 'asc')
->paginate(12);
Code above returns different result then code below;
$products = $products->with(['cover'])
->orderBy('priority', 'asc')
->get();
All the first 12 records are different.
Whats wrong with it?
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();
I have a relationship in Eloquent that I'm trying to query.
$deliveryOverride = $product->days->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();
I keep getting the the error
Method whereNotNull does not exist.
Any ideas?
When you call a relation property directly on an Eloquent object, the query is executed and a Collection is returned. There is no WhereNotNull function on collections.
If you want to query the relation using that function, you will have to call the relation function directly. This will also be better for performance as the query will happen on the database.
$deliveryOverride = $product->days() // Call relation function here
->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();
More information about this can be found in the documentation right here: https://laravel.com/docs/5.7/eloquent-relationships#querying-relations
How to use whereMonth, whereDate in Eloquent ORM? (I know it can in DB query). Im using 5.4
You can use all the methods from DB query in Eloquent models, Check the following samples you can easily capture it. Both the samples, produce the same result
Using query builder
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();
Using model
$users = Users::whereDate('created_at', '2016-12-31')
->get();
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.