How to do to get the last insert of below laravel code? - laravel

How to do to get the last insert of below laravel code?
$v_videos = VCategory::find($id)->videos->toArray();
Can anyone help? Thanks.

I use the reverse() method and fixed it.
VCategory::find($id)->videos->reverse()->toArray();
Thanks you.

If you're using timestamps for your models you can do this:
VCategory::find($id)->videos()->orderBy('created_at', 'desc')->first();
Otherwise you can still use the id (given its auto-increment)
VCategory::find($id)->videos()->orderBy('id', 'desc')->first();
Edit
There's also a little shortcut for the orderBy part.
VCategory::find($id)->videos()->latest()->first();
Or
without timestamps:
VCategory::find($id)->videos()->latest('id')->first();
Thanks #Joseph Silber for `latest()`

Related

Scope left Join Sub doesn't work - Laravel

I'm new to Laravel I'm making a clone of Twitter. I'm making a scope to get all the likes from the DB, but I get an error from Tinker
I know some basic SQL Queries, but this one is quite complicated, so I've got no idea what to do now.
Tweet model
public function scopeWithLikes(Builder $query)
{
$query->leftJoinSub(
'select tweet_id, sum(liked) likes, sum(!liked) dislikes from likes group by tweet_id',
'likes',
'likes.tweet_id',
'tweet.id'
);
}
Tinker command
App\Tweet::withLikes()->first();
Tinker error
TypeError: Argument 1 passed to App/Tweet::scopeWithLikes() must be an
instance of Illuminate/Database/Query/Builder, instance of
Illuminate/Database/Eloquent/Builder given, called in
C:/wamp64/www/laravel/tweety/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1164
I hope I explained it well, but if you need more information please ask me.
Thanks for your answer!
i think that you are using the wrong class for your scope, scope use
Illuminate/Database/Query/Builder
as a parameter while you pass
Illuminate/Database/Eloquent/Builder
in your Twit model file, on the top ...
remove:
use Illuminate/Database/Eloquent/Builder;
and paste:
use Illuminate/Database/Query/Builder;
Did this solve the problem? Because I run into exactly the same issue.
When I change Eloquent to Query, the error message is still the same.
Also the source that is given on Git uses use
Illuminate\Database\Eloquent\Builder;
Hubert

Validating an input which i know just part of its name in laravel

I have a form with dynamic inputs which are like: min_1,$min_2,...$min_1000,$max_1000, I want to validate these elements to be numeric and to be required. As i am not sure about the name of inputs to be validated, how can i write the role. I need some thing like
$min_(what ever): required|numeric
I appreciate any help. Thank you
Please try the following:
foreach($request->all() as $item)
{
$rule[$item] => "required|string"
}
Validator::make($request->all(),$rule);
Let me know If you get any errors.
Don't forget to mark it answer if works
Hope it helps you
Thank you

How to use orderBy with Laravel 6 LazyCollection?

It's been a few days since the Laravel released its latest version 6. I am trying to implement Laravel LazyCollection instead of the normal Collection class.
Following line works-
Drug::where('deactive',0)->orderBy('code')->get();
And when I use
Drug::cursor()->where('deactive',0)->orderBy('code')->get();
I get an error Method Illuminate\Support\LazyCollection::orderBy does not exist.
Can anyone help me how to use OrderBy with LazyCollection?
UPDATE
After getting the answer here is the correct syntax of above query
Drug::cursor()->where('deactive',0)->sortBy('code');
Offcourse, orderBy method is Query builder method. You can use sortBy as defined in the docs
I think you can do this.
Drug::where('deactive',0)->orderBy('code')->cursor();

Laravel 5.5 where condition

I have problem in searching, I am using Laravel 5.5 version, the situation like this: I have groups which study during some period of time. On filtering page, Admin enters study starting date and ending date, the result must show all groups which studied or studying between given time period. A comparing in simple way is not working, I would like to use strtotime function, but when I use:
->where(strtotime('edu_ending_date'),'=<',strtotime($edu_ending_date));
the eloquent is saying there is not such a column name ...
if(!empty($input['daterange']))
{
$q->where(function($query)use($edu_starting_date,$edu_ending_date){
$query->where('edu_starting_date', '>=', "$edu_starting_date")
->where('edu_ending_date','=<',"$edu_ending_date");
});
}
if you need to use dates in your where clauses I might want to have a look at this article and this section of laravel docs where they mention additional where clauses which include also whereDate, whereDay, etc. Might come in handy. In your case I would suggest you to do two whereDate conditions to act as between:
$query->whereDate('edu_starting_date', '>=', $edu_starting_date)
->whereDate('edu_ending_date', '=<', $edu_ending_date)
Note that $edu_starting_date and $edu_ending_date are recommended to be a Carbon object or output of PHP's date function. If you want to use strings according to the Laravel docs it should be possible. Hopefully this helps you :)
You can use
$q->whereBetween('edu_starting_date', [$edu_starting_date,$edu_ending_date])
try this:
if(!empty($input['daterange']))
{
$q->where(function($query)use($edu_starting_date,$edu_ending_date){
$query->where('edu_starting_date', '>=', $edu_starting_date)
->where('edu_ending_date','=<',$edu_ending_date);
});
}
The date is saved as a string in my database, is that ok, that is why I would like to use
something like this:
->where(strtotime('edu_ending_date'),'=<',strtotime($edu_ending_date));
You can use DB::raw
->where(DB::raw("strtotime('edu_ending_date')"),'=<',strtotime($edu_ending_date));
Thank you guys, I found my stupid mistake, I stored date as a string.

Laravel 4 pagination

My pagination doesn't seem to work. Here's how I done it:
$players = Auth::user()->players->paginate(2);
And then done all the stuff that's needed in the view, but:
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
It shows the line, where that variable is.
Try
Auth::user()->players()->paginate(2);
Auth::user()->players returns a collection, the paginate function belongs to the Builder class of Eloquent.
Nevermind, solved it myself:
Solution:
Auth::user()->players()->paginate(2);

Resources