Tag : Laravel Eloquent


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 | | 902 Viewer | Category : Laravel
READ MORE

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 | | 998 Viewer | Category : Laravel
READ MORE

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 | | 1036 Viewer | Category : Laravel
READ MORE

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 | | 1783 Viewer | Category : Laravel
READ MORE

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 | | 3273 Viewer | Category : Laravel
READ MORE

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 | | 1468 Viewer | Category : Laravel
READ MORE

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 | | 1888 Viewer | Category : Laravel
READ MORE

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 | | 2272 Viewer | Category : Laravel
READ MORE

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 | | 2899 Viewer | Category : Laravel
READ MORE

    Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch.

    Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch.

    Open Post

    Laravel 5 database backup using Laravel Backup Package

    Laravel 5 database backup using Laravel Backup Package

    Open Post

    Laravel 5.3 - import export csv and excel file into database

    Laravel 5.3 - import export csv and excel file into database

    Open Post

    PHP Image upload without refreshing page using jquery example

    PHP Image upload without refreshing page using jquery example

    Open Post

    Simple Highcharts Chart Example using PHP MySQL Database

    Simple Highcharts Chart Example using PHP MySQL Database

    Open Post

    Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

    Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

    Open Post