Eloquent where condition based on a “belongs to” relationship

You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas(‘director’, function($q) { $q->where(‘name’, ‘great’); })->get(); Also if you reverse the query like: $directorsWithMovies = Director::with(‘movies’)->where(‘name’, ‘great’)->get(); // Access the movies collection $movies = $directorsWithMovies->movies; For this you need to declare a hasmany relationship in your Director model: public function movies() { return … Read more

SQL Server : check if variable is Empty or NULL for WHERE clause

Just use If @searchType is null means ‘return the whole table’ then use WHERE p.[Type] = @SearchType OR @SearchType is NULL If @searchType is an empty string means ‘return the whole table’ then use WHERE p.[Type] = @SearchType OR @SearchType=”” If @searchType is null or an empty string means ‘return the whole table’ then use … Read more

Use a calculated column in a where clause

Using Derived Columns in a predicate You’ll need to wrap the inner query in a derived table or CTE in order to be able to use derived columns in the WHERE clause (Also, note SUM() is specified just once, using the results of the multiplication): SELECT x.Code, x.AccountNumber, x.Sales FROM ( SELECT p.Code, c.AccountNumber, SUM(p.UnitPrice … Read more

C# FindAll VS Where Speed

The FindAll method of the List<T> class actually constructs a new list object, and adds results to it. The Where extension method for IEnumerable<T> will simply iterate over an existing list and yield an enumeration of the matching results without creating or adding anything (other than the enumerator itself.) Given a small set, the two … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)