Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Tuesday, February 28, 2017

[SOLVED]How to get last inserted id with insert method in laravel

In my laravel project I am inserting multiple records at time with modelname::insert method. Now I want to get last inserted id of it.I read somewhere when you insert multiple records with single insert method and try to get the last_record_id it will gives you the first id of the last inserted query bunch. But my first question is how to get last record id with following code .If I am able to get first id of the bunch .I ll make other ids for other record by my own using incremental variable.
Code to insert multiple record
if(!empty($req->contract_name) && count($req->contract_name)>0)
            {

                for($i=0; $i<count($req->contract_name); $i++)
                {
                    $contract_arr[$i]['client_id'] = $this->id;
                    $contract_arr[$i]['contract_name'] = $req->contract_name[$i];
                    $contract_arr[$i]['contract_code'] = $req->contract_code[$i];
                    $contract_arr[$i]['contract_type'] = $req->contract_type[$i];
                    $contract_arr[$i]['contract_ext_period'] = $req->contract_ext_period[$i];
                    $contract_arr[$i]['contract_email'] = $req->contract_email[$i];
                    $contract_arr[$i]['created_at'] = \Carbon\Carbon::now();
                    $contract_arr[$i]['updated_at'] = \Carbon\Carbon::now();
                    $contract_arr[$i]['created_by'] = Auth::user()->id;
                    $contract_arr[$i]['updated_by'] = Auth::user()->id;
                    if($req->startdate[$i] != ''){
                        $contract_arr[$i]['startdate'] = date('Y-m-d',strtotime($req->startdate[$i]));
                    }
                    if($req->enddate[$i] != ''){
                        $contract_arr[$i]['enddate'] = date('Y-m-d',strtotime($req->enddate[$i]));
                    }
                }   
                if(!empty($contract_arr)){

                    Contract::insert($contract_arr);

                }
            }




via Alfiza malek

Monday, February 27, 2017

[SOLVED]Laravel Eloquent Relationships -- equivalent of MYSQL "join"

It seems that in all that Laravel Eloquent relationships can do, it isn't possible to have it add an INNER JOIN clause. Am I correct in that? whereHas clauses add a WHERE EXISTS subquery, and with clauses preform "eager loading" that just loads the relationship in separate queries.
Obviously, there are a lot of cases where an INNER JOIN is going to be way more efficient than running a bunch of queries, so I have written a join clause for these queries.
  1. Is it possible to use an eloquent relationship to define a join clause so that I don't have to rewrite the same join query every time I use the 2 tables together?
  2. If not, why? It seems like joining 2 tables that are related would be something that should be standardized. I can't really see how adding a subquery like WHERE EXISTS would be a suitable substitute. (according to my tests, it generates much slower queries....)


from Latest question asked on Laravel tag.


via Skeets O'Reilly

[SOLVED]Show only number of letters from the text in laravel [on hold]

I'm trying to show at my design only number of letters and the others will be like that ...
How can i do that ?



via Dina Shaldoum

Advertisement