Category : eloquent

I am creating a movie series database and have models like so: Series $with=["seasons"] hasMany(AppSeasons) Season $with=["episodes"] belongsTo(AppSeries) hasMany(AppEpisode) Episode belongsTo(AppSeason) Question 1 How can I based on Eloquest Series::all() sort desc by Episode ID ? [ [… seasons: [ [… episodes: [ [id] <—- sort by this ] ] ] ] Question 2 Can ..

Read more

I’m using Laravel for a project and i want to filter a collection based on custom method written in the model: Controller: $models= Produs::with(‘categorie’) ->with(‘poza’) ->with(‘element_extra.extras’) ->get() ->where(‘id_stare’, 1) ->where(‘categorie.id_restaurant’, $idRestaurant) ->groupBy(‘categorie.denumire’); $produse = $models->filter(function ($produs, $key) { return $produs->isAvailable(); })->values(); Model: class Produs extends Model { protected $table = "elemente"; public $timestamps = false; ..

Read more

[I’m looking for best practice to handle] I have data structure like below: Category Class protected $fillable = [ ‘id’, ‘price’, ‘name’ ]; public function products() { return $this->hasMany(Product::class, ‘category_id’); } Product Class protected $fillable = [ ‘category_id’, ‘price’ ]; public function category { return $this->belongsTo(Category::class, ‘category_id’); } Let’s say I updated price at category. ..

Read more

I have this query. Currently, this displays the status_count for the logged users. I want to update this query to display all the statuses from the status table and if the logged user has no record for any statuses to display status_count as 0. $orderStatistics = OrderHeader::select(‘status.id’, ‘status.name’, ‘status.description’, ‘status.type’, ‘status.code’, DB::raw(‘order_status,count(*) as status_count’)) ->leftjoin(‘status’, ..

Read more

I have two tables Sensors <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; use AppModelsMeasurement; class Sensor extends Model { use HasFactory; protected $table = ‘sensors’; public function measurements(){ return $this->hasMany(Measurement::class); } } and Measurements <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; use AppModelsSensor; class Measurement extends Model { use HasFactory; protected $table = ‘measurements’; public ..

Read more

I have a query for adding custom "balance" column using sum of values in relation table. User::withCount([‘transactions as balance’ => function($query) { $query->select(DB::raw(‘sum(credit_movement)’)); }]); This works fine for returning "balance" column. Now, I want to filter the users regarding the "balance" column. ->where(‘balance’, ‘<‘, 0); If I chain it to the query, it won’t work ..

Read more

I have a query for adding custom "balance" column using sum of values in relation table. User::withCount([‘transactions as balance’ => function($query) { $query->select(DB::raw(‘sum(credit_movement)’)); }]); This works fine for returning "balance" column. Now, I want to filter the users regarding the "balance" column. ->where(‘balance’, ‘<‘, 0); If I chain it to the query, it won’t work ..

Read more

I’m trying to filter users based on withCount() result and with() relationship. By filter, I mean do not return that user at all. User::with(‘info’) ->withCount([‘transactions as balance’ => function($query) { $query->select(DB::raw(‘sum(credit_movement)’)); }]); This works for returning the "info" relationship and "balance" column for user’s balance. Now I want to query based on 2 things. where(‘balance’, ..

Read more

I need to store Password as hashed to the database, I was able to has the password but when I submit my form the password stores as un-hashed, Here’s my controller Store Function public function store(Request $request) { $hash = [‘password’ => Hash::make($request)]; //dd($hash); // HASHED PASSWORD WAS DISPLAYED HERE $user = User::create($this->validateRequest()); dd(‘User Created’); ..

Read more

I’m trying to get first element every category. Category Class public function product() { return $this->hasMany(Product::class, ‘category_id’, ‘id’); } public function first_product() { return $this->product()->limit(1); } Here is my query $categories = Category::with(‘first_product’)->get(); I got first product only first element of category. why? Sourc..

Read more

I have two models with a many-to-many relationship. FORM and WORKSPACE. So FORM belongsToMany WORKSPACE, and WORKSPACE belongsToMany FORM. Now I need to do a query where I get all FORMs that belong to any of the WORKSPACEs in a collection ($workspaces). Something like this: $forms = Form::whereHas(‘workspaces’, function ($q) use ($workspaces) { $q->whereIn(‘id’, $workspaces); ..

Read more

I have a problem. I am doing a search function where users will enter the products they want to search via a form and will return the products. This is the form. <form action="{{ route(‘shopAll’) }}"> <input type="text" name="search" placeholder="Search Products…"> <button type="submit"><i class="ti-search"></i></button> </form> Product.php class Product extends Model{ protected $fillable = [ ‘name’,’added_by’, ..

Read more

I have a problem. I am doing a search function where users will enter the products they want to search via a form and will return the products. This is the form. <form action="{{ route(‘shopAll’) }}"> <input type="text" name="search" placeholder="Search Products…"> <button type="submit"><i class="ti-search"></i></button> </form> Product.php class Product extends Model{ protected $fillable = [ ‘name’,’added_by’, ..

Read more

im stuck adding column actions for edit and delete button with yajra datatables, im using DataTables Service because im wanna add export button too, here is my my datatables code : public function dataTable($query) { return datatables() ->eloquent($query); } /** * Get query source of dataTable. * * @param AppInfoDataTable $model * @return IlluminateDatabaseEloquentBuilder */ ..

Read more