$posts = Post::with(‘user’)->search(‘title’, $this->search)->latest()->paginate(5); Sourc..
Category : relational-database
$posts = Post::with(‘user’) ->search(‘title’, $this->search) ->latest() ->paginate(5); Sourc..
Assume I use MySQL and there is two tables, students and parents_guardians table. Table students is used to store student’s data. While parents_guardians table is used to store student’s parents and student’s guardians. So, either a student can have father and mother as his parents, or a student can have uncle and aunt as his ..
there is a problem with the output of all data in one collection. There are 3 tables Example: categories, products_group, products. I tried to link tables via lanks but since the product table can be without a group, this is not the case. Migrations categories -id -title … products_group -id -title … products -id -title ..

I made many to many relationships for admin and school tables. "SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘admin_id’ in ‘field list’ (SQL: insert into admin_school (admin_id, school_id) values (91, 2))" Table : admins schools admin_school (pivot table) Here is structure pivot table Model Admin public function schools(){ return $this->belongsToMany(‘AppModelsSchool’, ‘admin_school’, ‘admin_id’, ‘school_id’); } Model ..
I need some help designing my database relationships. I have these tables: documents, companies, individuals and users. A document can be internal or external. If it is external, it can be signed by a company or an individual. If it is internal it has to be signed by a user. In any case, the document ..
I want to do change with method by where like this User::where(〜)->with(‘post’)->orWhere(…)->with(‘post2’)->paginate(10) please help me. Sourc..
I want to do change「with」 method by 「where」. like this. User::where(〜)->with(‘post’)->orWhere(…)->with(‘post2’)->paginate(10) For example(This example is not work. This is for image) $user = User::Where(function ($query) use ($id1, $id2) { $query->where(‘user_id’, $id1) ->whereIn(‘monster_id’, $id2) ->with([‘user’ => function ($query) { $query->with(‘residence’)->get(); }]); })->orWhere(function ($query) use ($id1, $id2) { $query->where(‘monster_id’, $id1) ->where(‘user_id’, $id2) ->with([‘user2’ => function ($query) { ..
Message Tables Column is | messages_id(pk) | user_id | target_id | message | created_at User_id has sender user’s id. Target_id has receiver user’s id And simple user table has relation to MessageTable by HasMany. public function message() { return $this->hasMany(‘AppModelTableModelMessage’, ‘user_id’, ‘user_id’); } I want to get Userdata and each last message from user_id=[1,2,3,4] to ..

I have an One-to-One relationships Between the user (penggunas) table and the role table, but when I access it in the controller it returns null and column not found Route.php Route::get(‘/test’, ‘[email protected]’); PenggunaController.php use AppPengguna; class PenggunaController extends Controller { public function index() { $pengguna = Pengguna::find(3); return response()->json([ ‘data’ => $pengguna ]); } } ..
I’m producing an online school platform and in part of it student can see it’s classes with their informations..but the related function return this error : Undefined offset: 0 (View: C:xampphtdocsOnlineSchoolresourcesviewsadminstudentClassReport.blade.php) there are some relations between models My Models: USER(students and teachers) ، LEVEL(level of classes) ، Classroom. => there is a many to many ..
I have Book, Magazine and Song models. I want to make a Collection model that will contain models of these three types, so I could query it like this (pseudo code): Collection->get() and get a result similar to this: [ {Book_1} {Magazine_1} {Book_2} {Song_1} {Book_3} … ] Is it possible to do in Laravel, and ..
I have meals model which look like these class meals extends Model { use HasFactory; protected $appends = [ ‘image_full_path’, ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ ‘name’, ‘text’,’price’,’image_url’,’rating’,’discount’,’sub_catogry_id’ ]; public function sub_catogry() { return $this->belongsTo(‘AppModelssub_catogry’); } public function order_meal_list() { return $this->hasMany(order_meal_list::class); } ..
I have a small blogging app which allows users to upload photos and audio. Users can also search for other blogs. While searching, I’d like to add a ranking to my query with the following precedence: 1) Users with most photos 2) Users with most audio User model is constructed as such: public function blog() ..
I have tow table named A and B that have n-m relation so I must have third table named C. (table C keep A id and B id !) In Laravel models, can I define relations? Sourc..
I want the permissions table to be filled in the database when selecting roles My tables: permissions -> id | name roles -> id | name permission_role -> permission_id | role_id permission_user -> permission_id | user_id role_user -> role_id | user_id I create the permissions and roles data in the admin panel and When I ..
I am trying to retrieve the number of relations using the withCount method, but using a where condition that is limiting it to a specific amount and between specific dates. for example, i want to count how much withCount results will give me 1/2/3. the methods i am using now is this, but when selecting ..

you can see a part of my database in the image below: the goal of this design was to able the admins send alerts to users filtered by center and field(dynamically). if you think this is a bad design tell me (please say why and how should I improve my design). now if I want ..
I have 4 tables Packages: id name Tests: id name subcategory_id category: id name parent_id = 0 // if main category packages_tests:(pivotTable) id package_id test_id I want to get tests inside each package with category like that packages: [ name: package1 categories: [ { name: mainCategory1 tests: [ {test1}, {test2} ] }, { name: mainCategory2 ..
I have two Models (Store, Product) and Relationship hasMany public function products(){ return $this->hasMany(Product::class); } And I want to return response collection, in class StoresCollection extends ResourceCollection public function toArray($request) { return $this->collection->map(function ($item) { return [ ‘id’ => $item->id, ‘seller_id’ => $item->seller_id, ‘store_product’ => $item->products()->get(), ]; }); } But I don’t want to return ..