Model relation
---------------------
language.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'language_id');
}
attribute.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'attribute_id');
}
attributeDetail.php
----
public function language()
{
return $this->belongsTo(Language::class);
}
public function attribute()
{
return $this->belongsTo(Attribute::class);
}
I want to show the json object like this
{
'attribute_id' => 101,
'available_language' => [
{'id' => 1,'language_name' => 'English'},
{'id' => 2,'language_name' => 'French'}
],
}
table structure:
languages(`id`, `language_name`, `translate_version`, `is_default`, `status`);
attributes(`id`, `required`, `type`, `status`);
attributedetails(id`, `attribute_id`, `language_id`, `attribute_name`, `status`);
Try somthing like this,
$results = Attribute::select('id')->with(['attributeDetail.language' => function ($query) {
$query->select('id', 'language_name');
}])->get();
Related
When calling an edit function the prop mealService is passing null values and will not populate form fields with values. It looks like the controller isn't loading the model to query the single record. The Create and Store functions work fine.
First time posting. And very new to coding. Please let me know if more info is needed for the question.
edit.vue
export default {
components: {
Head,
Link,
LoadingButton,
SelectInput,
TrashedMessage,
},
layout: Layout,
props: {
mealService: Object,
sites: Array,
},
remember: 'form',
data() {
return {
form: this.$inertia.form({
site_id: this.mealService.site_id,
meal_type: this.mealService.meal_type,
adults: this.mealService.adults,
tally: this.mealService.tally,
}),
}
},
MealServiceController
public function edit(MealService $meal_service)
{
return Inertia::render('MealServices/Edit', [
'mealService' => [
'id' => $meal_service->id,
'site_id' => $meal_service->site_id,
'meal_type' => $meal_service->meal_type,
'adults' => $meal_service->adults,
'tally' => $meal_service->tally,
],
'sites' => Auth::user()->sfa
->sites()
->orderBy('name')
->get()
->map
->only('id', 'name'),
]);
}
MealService Model
class MealService extends Model
{
use HasFactory;
use SoftDeletes;
public function resolveRouteBinding($value, $field = null)
{
return $this->where($field ?? 'id', $value)->withTrashed()->firstOrFail();
}
public function site()
{
return $this->belongsTo(Site::class);
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->WhereHas('site', function ($query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
});
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
if ($trashed === 'with') {
$query->withTrashed();
} elseif ($trashed === 'only') {
$query->onlyTrashed();
}
});
}
}
Route
Route::get('mealServices/{mealService}/edit', [MealServicesController::class, 'edit'])
->name('mealServices.edit')
->middleware('auth');
In your edit function you are using meal_service while in your route you use mealService
Try naming the one in the edit function mealService too
I have a problem getting media from a joined table in laravel-medailibrary, I used getFirstMediaUrl("images") to get photos from one table and it works, but if I join two or three tables it not work, how can I solve it?
I want to get photos from those posts that shared by a user.
this post table:
this is share_tb table:
this is users table:
this is the media table:
I find my answer after trying some ways:
public function getPosts(Request $request)
{
$result = [];
$postID = DB::table("share_tb")->where("user_id", Auth::user()->id)->get();
foreach ($postID as $id) {
if (count(Post::where("id", $id->related_id)->get()) > 0) {
$posts = Post::where("id", $id->related_id)->get();
foreach ($posts as $post) {
// $result = $post->getMedia('images');
array_push($result, [
"comment_count" => getTotalComment($post->id),
"course_id" => $post->course_id,
"id" => $post->id,
'post_image' => count($post->getMedia('images')) > 0 ? $post->getMedia('images')[0]->getFullUrl('big') : "",
'logo'=>GetCourseLogo::collection(Course::where('course_id',$post->course_id)->get()),
"post_author" => $post->post_author,
"post_date" => $post->post_date,
"post_excerpt" => $post->post_excerpt,
"post_modified" => $post->post_modified,
"post_parent" => $post->post_parent,
"post_title" => $post->post_title,
"post_type" => $post->post_type,
]);
}
}
}
return Response()->json($result);
}
and by this resource collection, I get the logo:
class GetCourseLogo extends JsonResource
{
public function toArray($request)
{
return $this->getFirstMediaUrl('logo');
}
}
I have 3 tables like this
Student:
id | name
class Student extends Model
{
protected $table = 'students';
public function marks () {
return $this->belongsToMany( 'App\Models\Subject', 'marks' )->using( 'App\Models\Mark');
}
}
Subject:
id | name
class Subject extends Model
{
protected $table = 'subjects';
public function marks () {
return $this->belongsToMany( 'App\Models\Student', 'marks' )->using( 'App\Models\Mark');
}
}
Mark:
subject_id | student_id | type_of_exam | times_taking_exam | mark
class Mark extends Pivot
{
protected $table = 'marks';
public function student() {
return $this->belongsTo('App\Models\Student','student_id');
}
public function subject() {
return $this->belongsTo('App\Models\Student','subject_id');
}
public $timestamps = false;
}
The table Mark which has 4 column primary key (id_subject, id_student, type_of_exam, times_taking_exam)
I don't know my design database is correct or not, but if it did, how can I use sync to update or insert mark for my student.
Because I understand that the method sync is like this:
$student->marks()->sync([
$subject_id => [
'type_of_exam' => 1,
'times_taking_exam' => 1,
'mark' => 10,
]
]);
But it is not correct for this situation cause it will run this code like this:
Mark::updateOrCreate( [
'student_id' => $student_id,
'subject_id' => $subject_id,
], [
'type_of_exam' => 1,
'times_taking_exam' => 1,
'mark' => 10
] );
Instead, I want this:
Mark::updateOrCreate( [
'student_id' => $student_id,
'subject_id' => $subject_id,
'type_of_exam' => 1,
'times_taking_exam' => 1,
], [
'mark' => 10
] );
I have tried use Mark as Model but I think it's not the best way.
I am using OctoberCMS and I have created a custom component. I am trying to create a frontend filter to filter Packages by the Tour they are assigned to.
This is what I have so far. The issue is that the code is looking for a tour field within the packages table rather than using the tour relationship. Does anyone have any ideas?
<?php namespace Jakefeeley\Sghsportingevents\Components;
use Cms\Classes\ComponentBase;
use JakeFeeley\SghSportingEvents\Models\Package;
use Illuminate\Support\Facades\Input;
class FilterPackages extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Filter Packages',
'description' => 'Displays filters for packages'
];
}
public function onRun() {
$this->packages = $this->filterPackages();
}
protected function filterPackages() {
$tour = Input::get('tour');
$query = Package::all();
if($tour){
$query = Package::where('tour', '=', $tour)->get();
}
return $query;
}
public $packages;
}
I really appreciate any help you can provide.
Try to query the relationship when the filter input is provided.
This is one way to do it;
public $packages;
protected $tourCode;
public function init()
{
$this->tourCode = trim(post('tour', '')); // or input()
$this->packages = $this->loadPackages();
}
private function loadPackages()
{
$query = PackagesModel::query();
// Run your query only when the input 'tour' is present.
// This assumes the 'tours' db table has a column named 'code'
$query->when(!empty($this->tourCode), function ($q){
return $q->whereHas('tour', function ($qq) {
$qq->whereCode($this->tourCode);
});
});
return $query->get();
}
If you need to support pagination, sorting and any additional filters you can just add their properties like above. e.g;
protected $sortOrder;
public function defineProperties(): array
{
return [
'sortOrder' => [
'title' => 'Sort by',
'type' => 'dropdown',
'default' => 'id asc',
'options' => [...], // allowed sorting options
],
];
}
public function init()
{
$filters = (array) post();
$this->tourCode = isset($filters['tour']) ? trim($filters['tour']) : '';
$this->sortOrder = isset($filters['sortOrder']) ? $filters['sortOrder'] : $this->property('sortOrder');
$this->packages = $this->loadPackages();
}
If you have a more complex situation like ajax filter forms or dynamic partials then you can organize it in a way to load the records on demand vs on every request.e.g;
public function onRun()
{
$this->packages = $this->loadPackages();
}
public function onFilter()
{
if (request()->ajax()) {
try {
return [
"#target-container" => $this->renderPartial("#packages",
[
'packages' => $this->loadPackages()
]
),
];
} catch (Exception $ex) {
throw $ex;
}
}
return false;
}
// call component-name::onFilter from your partials..
You are looking for the whereHas method. You can find about here in the docs. I am not sure what your input is getting. This will also return a collection and not singular record. Use ->first() instead of ->get() if you are only expecting one result.
$package = Package::whereHas('tour', function ($query) {
$query->where('id', $tour);
})->get();
I have no table in my database in this name (location_id) but it gives me the error.
"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table
'pht.location_id' doesn't exist (SQL: select locations.*,
location_id.location_trails_id as pivot_location_trails_id,
location_id.location_id as pivot_location_id from locations
inner join location_id on locations.id =
location_id.location_id where location_id.location_trails_id
in (11, 13, 15, 16, 121, 123, 124, 181))",
I cannot find how to resolve it. Please Help me To resolve this issue. Thanks In advance.
Here is the code of my controller.
public function get($id)
{
$locations = LocationTrails::whereTrailId($id)->with('locations')->orderBy('ordered','ASC')->get();
dd($locations);
// $locations = Trail::findOrFail($id)->locations()->paginate($this->perPage);
// dd($locations);
$tdata = [];
foreach($locations->items() as $location) {
$hints = $location->hints;
$hintsData = [];
foreach($hints as $hint) {
$hintsData[] = [
'title' => $hint->title,
'hint_text' => $hint->hint_text,
'hint_solution_text' => $hint->hint_solution_text,
'hint_image_path' => $hint->hint_image_path,
'hint_video_path' => $hint->hint_video_path,
'complexity_order' => $hint->complexity_order
];
}
$tdata[] = [
'id' => $location->id,
'title' => $location->title,
'radius' => $location->radius,
'latitude' => $location->latitude,
'longitude' => $location->longitude,
'rewards_points' => $location->rewards_points,
'location_info_link' => $location->location_info_link,
'hints' => $hintsData
];
}
$data = [];
$data['data'] = $tdata;
$data['meta']['paginator'] = $this->getPaginatorInfo($locations);
return $data;
}
Here is my model of location
class Location extends Model
{
protected $table = 'locations';
protected $fillable = [
'title', 'radius', 'latitude', 'longitude', 'rewards_points', 'location_info_link', 'location_order'
];
/********************* RELATIONS *********************/
public function trails()
{
return $this->belongsToMany(Trail::class);
}
public function hints()
{
return $this->hasMany(LocationHint::class);
}
public function location_trails()
{
return $this->belongsTo(LocationTrails::class,'location_id');
}
Here is my locationtrail model:
class LocationTrails extends Model
{
protected $table = 'location_trail';
protected $fillable = [
'location_id', 'trail_id', 'ordered', 'created_at', 'updated_at',
];
/********************* RELATIONS *********************/
public function trails()
{
return $this->belongsToMany(Trail::class,'trail_id');
}
public function locations()
{
return $this->belongsToMany(Location::class,'location_id');
}
problem is in your LocationTrails model. You define your method like $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id'); so, laravel think location_id is a table name and the error thrown.
If it is one to many relationship.
public function locations()
{
return $this->hasMany(Location::class,'location_id');
}
If it is many to many relationship then you can see this.
https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
same for your trails() method.
public function trails()
{
return $this->hasMany(Trail::class,'trail_id');
}