Tag : Laravel Eloquent

ItSolutionStuff.com have tutorials for Laravel Eloquent tag, here you can study articles of Laravel Eloquent tag, Laravel Eloquent tag posts collection, most popular and useful tutorials of Laravel Eloquent tag, here you can find list of all relevant posts and example about Laravel Eloquent tag, we have lists of tutorials and examples about Laravel Eloquent tag. very simple and quick example collection of Latest Laravel Eloquent tag.

laravel 5.4 New Feature - Add eloquent whereKey method

Yesterday release Laravel 5.4 new version with new features and many upgrade. Laravel also provide documentation for Laravel 5.4 on their website. There are several update in Laravel 5.4 like in collections, mail, factory helper, Bootstrappers etc. In this post we are going to see whereKey method in Laravel Eloquent. whereKey is very simple way to use and pretty interesting feature. laravel provide us where condition with add key(column) name and then value, But in this method you have write Key = name of column. So i am going to give one example for whereKey() that way you can understa.....

By Hardik Savani | January 25, 2017 | | 27976 Viewer | Category : Laravel

How to disable model timestamps in Laravel 5.3?

Sometimes we require to disable created_at and updated_at timestamps on Model on Laravel, so we can do it simply by using <strong class="path">$timestamps</strong> variable of model. It is very small things but important to understand and how to use it. When you create new item or user using model at that time created_at and updated_at column set default time by default but you can prevent to set false value of $timestamps variable. I am creating new records using create method of model like as bellow example: <pre class="prettyprint lang-php"> Item::create(['title'=>'ItSolutionStuff.com']).....

By Hardik Savani | October 12, 2016 | | 33227 Viewer | Category : Laravel

How to get table name from model in Laravel 5?

If you need to get table name from model in your laravel application then this post can help you to get table name. Laravel based on MVC. So, we require to create model for every table that way we can write database logic on model. But if you require to get table name from model then you can get using getTable() of model. Laravel eloquent model provide several methos like for all(), get(), first() etc, that helps to get table record from model table. You can get table name simply call getTable() of model object, so how to call this method as bellow example. <strong>Example:</strong> <pre c.....

By Hardik Savani | August 22, 2016 | | 34697 Viewer | Category : Laravel

How to use groupby having with DB::raw in Laravel Query Builder?

We most probably require to use group by having because if you work on small project then it's not need to use generally. But if you work with big project like e-commerce, social or ERP level project then you may require to use having clause. having clause, we can use simply if we have to compare with number or static value like as bellow: <pre> ->having("total_quantity","<",10) </pre> But we need to compare with 10 instead of column name then it can't directly use column name. At that time we should use DB::raw() with column name. In this example i want to show minimum quantity items, I .....

By Hardik Savani | July 6, 2016 | | 34418 Viewer | Category : Laravel

How to insert multiple records in Laravel 5?

If we work on big project and then we maybe require to add multiple rows on database using laravel eloquent. Laravel provide insert method for bulk records create on db. In bellow example you can see i use multidimensional $myItems array variable and that insert multiple records same time using DB::insert(). So let's see and try this. <strong>Example:</strong> <pre class="prettyprint lang-php"> $myItems = [ ['title'=>'HD Topi','description'=>'It solution stuff'], ['title'=>'HD Topi 2','description'=>'It solution stuff 2'], ['title'=>'HD Topi 3','descriptio.....

By Hardik Savani | June 13, 2016 | | 49830 Viewer | Category : Laravel

Laravel 5 select with count query with group by example

We always required to get count of number of records or number of inserted record of current month etc in laravel. We can use mysql count function in laravel eloquent. We have two way to get count of column value. first we can use laravel count() of query builder and another one we can use with directly with select statement using DB::raw(). I give you both example you can see and use any one as perfect for you. <strong>Example 1:</strong> <pre class="prettyprint lang-php"> $data = DB::table("click")->count(); print_r($data); </pre> <strong>Example 2:</strong> <pre class="prettyprint lang-php.....

By Hardik Savani | June 9, 2016 | | 77207 Viewer | Category : Laravel

How to convert object to array in Laravel 5?

If you require to convert object data from db into array then you can do it using DB facade and Model Eloquent. Sometimes we need to give array data only so we must get array data from db. I have two examples so it can helps you. In first example, If you use Model Eloquent for get data from database then you can do it using toArray(). toArray() will help to convert object into array data. So let's see bellow example and check it. <strong>Example 1:</strong> <pre class="prettyprint lang-php"> $users = User::get()->toArray(); print_r($users); </pre> In this example, if you use DB facade for ge.....

By Hardik Savani | June 2, 2016 | | 52018 Viewer | Category : Laravel

Laravel 5 - Orderby random using rand() and DB::raw() example

Sometimes, we require to get randomly data using laravel query builder. you can use mysql rand() with order by. If you want to get random data using laravel eloquent then we need to use DB::raw(). In bellow example you can i use DB::raw(). In this example i use DB::raw('RAND()') inside orderBy() and also add limit of 8 number of record, so you can easily implement in your laravel project. so let's try... <pre class="prettyprint lang-php"> return DB::table("posts") ->select("posts.*") ->orderBy(DB::raw('RAND()')) ->take(8) ->get(); </pre>

By Hardik Savani | May 4, 2016 | | 34567 Viewer | Category : Laravel

How to select concat columns with Laravel Query Builder?

We are always wanted to use sql query for concatenate fullname or something to concat using mysql concat function, now if you think how to use concat function in laravel query builder then this post can help you. So, if you need to use concat mysql function then see bellow example: <strong>Example:</strong> <pre class="prettyprint lang-php"> $user = User::select("users.*" ,DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name")) ->get(); print_r($user); </pre>

By Hardik Savani | April 30, 2016 | | 47734 Viewer | Category : Laravel

Laravel 5 - inner join with multiple conditions example using Query Builder

In this post you can learn how to add multiple condition in join query of Laravel Eloquent. if you use data relationship then you don't need to use but if you need to get manually join with two or more condition then it can help. in this example i also add how to add simple inner join in laravel and how to add multiple conditions with inner join i mean using "on" method. You can see bellow example: <strong>Example:</strong> <pre class="prettyprint lang-php"> $user = User::select("users.*","items.id as itemId","jobs.id as jobId") ->join("items","items.user_id","=","users.id") .....

By Hardik Savani | April 30, 2016 | | 75684 Viewer | Category : Laravel