Category : eloquent

I have this working code with AS count self declared field using eloquent $delimeter = ‘ ‘; $keywords = explode($delimeter, $keyword_search); $base_query = DB::table(‘yeast_entire_search’)->selectRaw(‘ id, part_number, part_name, Round ((Char_length(Concat( LOWER(part_number), LOWER(part_name) )) – Char_length(REPLACE ( Concat( LOWER(part_number), LOWER(part_name) ), LOWER("’.$keyword_search.’"), ""))) / Char_length(LOWER("’.$keyword_search.’"))) AS count ‘); foreach($keywords as $keyword){ $base_query->where(function($query) use ($keyword){ $query->where(‘part_number’, ‘like’, ‘%’ ..

Read more

I am new to laravel and building my first project using it. I want to query two tables (Cases and Detail) using orWhere() function. Please tell me the changes I need to make in my existing code. Controller.php $key = trim($request->get(‘q’)); $cases = Cases::query() ->where(‘cnic’, "$key") ->orWhere(‘eventDate’, "$key") ->get(); return view(‘adminHome’,[‘user’ => $cases ]); The ..

Read more

I am having those two table Schemas (Client and Product Models) with a One to Many relationship. I am using mysql database. Schema::create(‘clients’, function (Blueprint $table) { $table->id(); $table->string(‘name’); $table->string(‘job’); $table->timestamps(); }); Schema::create(‘products’, function (Blueprint $table) { $table->id(); $table->foreignId(‘client_id’)->constrained(); $table->string(‘productName’)->unique(); $table->integer(‘quantity’); $table->float(‘unitPrice’); $table->timestamps(); }); I also tried to use the syntax below instead of $table->foreignId(‘client_id’)->constrained(); ..

Read more

With Laravel, I have this kind of structure in a json db column : [ { "shift": "2", "users[]": [ "2657" ], "products[]": [ "SKU1" ], "users_categories[]": [ "CATEGORY1" ], "products_categories[]": [ "13" ] }, { "shift": "4", "users[]": [], "products[]": [], "users_categories[]": [ "CATEGORY1", "CATEGORY2", "CATEGORY3", "CATEGORY4" ], "products_categories[]": [] } ] How can ..

Read more

$products = product::select([ DB::raw(‘product_no as number’), DB::raw(‘product_name as name’), DB::raw(‘sum(total) as total’), DB::raw(‘sum(product_qty) as qty’), DB::raw(‘sum(product_price) as price’), ]) ->groupBy(‘number’) ->groupBy(‘name’) ->get(); return view (‘view’,[‘products’=>$products]); i need to insert the result to another table in database by laravel Sourc..

Read more

Let’s assume that I have 2 tables: Users: Schema::create(‘users’, function (Blueprint $table) { $table->increments(‘id’); $table->string(‘name’); $table->string(’email’)->unique(); $table->integer(‘age’) $table->string(‘password’); $table->rememberToken(); $table->timestamps(); }); Phones: Schema::create(‘phones’, function (Blueprint $table) { $table->increments(‘id’); $table->integer(‘user_id’)->unsigned(); $table->string(‘phone’); $table->timestamps(); $table->foreign(‘user_id’)->references(‘id’)->on(‘users’) ->onDelete(‘cascade’); }); User Model: public function phone() { return $this->hasOne(‘AppPhone’); } Phone Model: public function user() { return $this->belongsTo(‘AppUser’); } When I perform ..

Read more

I’m stuck on this point: I got some migrations like this: <?php Schema::create(‘product_categories’, function ( $table) { $table->bigIncrements(‘id’); $table->timestamps(); }); Schema::create(‘products’, function ( $table) { $table->bigIncrements(‘id’); $table->unsignedBigInteger(‘category_id’)->nullable(); $table->foreign(‘category_id’)->references(‘id’)->on(‘product_categories’)->onDelete(‘cascade’); }); This results in: CREATE TABLE `product` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `category_id` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `product_category_id_foreign` (`category_id`), CONSTRAINT `product_category_id_foreign` ..

Read more

I am using maatwebsite to export the records to CSV file. Using php laravel for backend. Here is my following code: Controller code: public static function exportCsvReport($params){ header(‘Content-Encoding: UTF-8’); header(‘Content-type: text/csv; charset=UTF-8’); return Excel::download(new UsersExport, ‘invoices.xlsx’); } UserExport model: <?php namespace AppExports; use AppModelsApiv1Tbcall; use MaatwebsiteExcelConcernsFromCollection; class UsersExport implements FromCollection { public function collection() { ..

Read more

I have two models using different tables on two different connections, User and UserInfo. User has a UserInfo hasMany relation: public function userInfo() { return $this->hasMany(‘pathtoUserInfo’,’User_ID’); } and UserInfo has a User belongsTo relation: public function user() { return $this->belongsTo(‘anotherpathtoUser’, ‘User_ID’, ‘User_ID’); } I would like to find the first user with more than one ..

Read more

I just learned laravel web programming about 2 weeks ago I have 2 tables, namely anggotas and simpanans which has one-to-many relationship one anggota (id_anggota as pk) can have many simpanan(id simpanan as pk) I use id_anggota as a foreign key in the simpanans table when I delete one of the data in the simpanans ..

Read more

I have eloquent relationships with category child In category Model public function products() { return $this->hasMany(Products::class, ‘category’, ‘id’); } public function sub() { return $this->hasMany(SELF::class, ‘parent_id’)->withCount([‘products’])->with(‘sub’); } I would like to get in each category and child category has products count Example Category A is the parent Category B and C are child of A ..

Read more

I need to convert this query form PostgreSQL in Eloquent but I failed: select invoice_item_id from ( select invoice_item_id, status, row_number() over (partition by invoice_item_id order by id) as rank, lead(invoice_item_id) over (partition by invoice_item_id) as next from payment_invoice_item_status as piis where invoice_item_id > 6000000 and status in (3191,3238) )d where status=3191 and rank=1 and ..

Read more

I have this Controller called GetChangeInfo public function GetChangeInfo(Request $request){ $customers = Customer::doesntHave(‘bookings’) ->doesntHave(‘product_baskets’) ->whereRaw("CONCAT(customers.id, COALESCE(customers.first_name, ”), ‘ ‘, COALESCE(customers.last_name, ”), ‘ ‘, COALESCE(customers.email, ”), ‘ ‘, COALESCE(customers.phone, ”), ) LIKE ?", ["%{$request->find}%"] ) ->orderBy(‘id’, ‘desc’) ->paginate(10); return response()->json($customers); } I want to insert customers.pending_update_info, 1 this code inside in whereRaw whenever i insert that ..

Read more

1)When I tried to use Model in update function of controller I get the error? What might be the reason for it. I got error: Non-static method IlluminateDatabaseEloquentModel::update() should not be called statically 2)Another problem is that my delete function doesn’t delete the product. It redirects in another page. What might be the mistake in ..

Read more