I am creating a blog with posts in Laravel 5.2. Every post is going to have multiple tags. I created a relationship between posts and tags. When the post is created, I am trying to attach multiple tags to it.
Controller function
public function store(Request $request)
{
$attributes = [
'author_id' => Auth::user()->id,
'title' => $request->input('title'),
'text' => $request->input('text'),
];
$post = Post::create($attributes);
// Get the tag ids from the the tags table in the database
$tagsList = $request->input('tags');
$tags = explode(",", $tagsList);
$tagIds = array();
foreach($tags as $tag) {
$tagIds[] = Tag::select('id')->where('name', $tag)->limit('5')->get();
}
dd($tagIds); // Output below
$post->tags()->attach($tagIds); // not working
}
dd($tagIds) output
array:2 [▼
0 => Collection {#316 ▼
#items: array:1 [▼
0 => Tag {#317 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:1 [▼
"id" => 32
]
#original: array:1 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
1 => Collection {#318 ▼
#items: array:1 [▼
0 => Tag {#319 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:1 [▼
"id" => 36
]
#original: array:1 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
The returned array contains correct ids of the tags. I just do not know how to get them into a format, so that they can be passed to the attach method.
I would be very thankful for any kind of help!
For convenience, attach and detach also accept arrays of IDs as input:
$tagIds = array();
foreach($tags as $tag) {
$tagIds[] = Tag::select('id')->where('name', $tag)->first()->id;
}
dd($tagIds); // Output below
/*
Array
(
[0] => 1
[1] => 3
[2] => 5
)
*/
$post->tags()->attach($tagIds);
You can just use pluck to get the array of ids from collection, no need to loop to get the ids of tags.
example:
$tags = Tag::pluck('id');
$post->tags()->attach($tags);
Please also refer to the documentation
https://laravel.com/docs/9.x/collections#method-pluck
Related
I have the following eloquent in my controller,
$getJobs=JobTitle::where('department_id',DepartmentUser::where('user_id', $user->id)->first()->department_id)->get();
This gives me following collection
Illuminate\Database\Eloquent\Collection {#2970 ▼
#items: array:3 [▼
0 => App\JobTitle {#2967 ▶}
1 => App\JobTitle {#2968 ▶}
2 => App\JobTitle {#2962 ▶}
]
#escapeWhenCastingToString: false
}
And one model element looks like this
1 => App\JobTitle {#2968 ▼
#connection: "mysql"
#table: "job_titles"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:9 [▼
"id" => 898
"title" => "driver"
"company_id" => 635
"department_id" => 252
"created_by" => null
"created_at" => "2022-04-20 05:30:38"
"updated_at" => "2022-04-20 05:30:38"
"deleted_at" => null
"archived_at" => null
]
#original: array:9 [▶]
#changes: []
#casts: array:2 [▶]
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: array:3 [▶]
#guarded: array:1 [▶]
+archives: true
#forceDeleting: false
}
Now I want to store all the job title (title) value's into single array
"title" => "driver"
In the above scenario, I have array elements and therefore I need store all three respective job titles to one single array...
$jobs = ['driver', 'chef', 'engineer'];
I tried adding an foreach, but it was giving me the following error,
#foreach ($getJobs as $getJob)
dd ($getJob->title);
#endforeach
ParseError syntax error, unexpected token "foreach"
What is the best way to achieve this?
You can simply loop on $getJobs and collect the title names in an array like below:
<?php
$getJobs = JobTitle::where('department_id',DepartmentUser::where('user_id', $user->id)->first()->department_id)->get();
$titles = [];
foreach($getJobs as $job){
$titles[] = $job->title;
}
dd($titles);
in first i can write this \App\CourseStart->find(any)->students->count() and will work fine
put I am forced to concatenate the variables same $object->$k->count()
$object = (`\App\CourseStart->find(any)) collation laravel
App\CourseStart {#1309 ▼
#dates: array:2 [▶]
#fillable: array:15 [▶]
#connection: "mysql"
#table: "course_starts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▶]
#changes: []
#casts: []
#classCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
$k = 'students' is relation
so $object->{$k} = \App\CourseStart->find(any)->students this work 100%
Illuminate\Database\Eloquent\Collection {#1310 ▼
#items: array:2 [▼
0 => App\Student {#1311 ▶}
1 => App\Student {#1314 ▶}
]
}
$object->{$k}->count(); work get resulte "2"
$f = ['count()','','']
now i try write this $object->{$k}->$f[0] and $object->$k->{$f[0]}
i have this msg error
Exception
Property [count] does not exist on this collection instance.
now how concatenate function count() to $object->{$k}
Not sure which address is appropriate for the question?
after some experiments
$f = ['count','','']// remove brackets of the functions
$f2 = $f[0];// assign to a variable for extracting the function from the array
$object->{$k}->$f2()// this will work
source: https://www.php.net/manual/en/functions.variable-functions.php
I tested it in a project of mine as beloved:
$x = Page::where ( 'slug', $page_slug );
$fA = ['count','get',''];
$f = $fA[0];
dd( $x->$f());
I have Collection form my Form obj, and iam trying to get all pages belongs to that form,
when I do $survey = Forms::find(68)->with('pages')->get(); Iam getting this:
Illuminate\Database\Eloquent\Collection {#522 ▼
#items: array:18 [▼
0 => App\Models\Forms {#562 ▼
#table: "forms"
#fillable: array:4 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"pages" => Illuminate\Database\Eloquent\Collection {#596 ▼
#items: array:5 [▼
0 => App\Models\Pages {#628 ▶}
1 => App\Models\Pages {#632 ▶}
2 => App\Models\Pages {#633 ▶}
3 => App\Models\Pages {#634 ▶}
4 => App\Models\Pages {#635 ▶}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
but can't get property of relations, when I do $form->pages Iam getting null
Its works fine for other property like questions (same relations)
here is my model:
class Forms extends Model
{
protected $table = "forms";
protected $fillable = array('name', 'description', 'status', 'token');
public function pages()
{
return $this->hasMany('App\Models\Pages');
}
public function questions()
{
return $this->hasMany('App\Models\Question');
}
}
class Pages extends Model
{
protected $table = "pages";
public $timestamps = false;
protected $fillable = array('name', 'description' ,'priority', 'token');
public function form()
{
return $this->belongsTo('App\Models\Forms');
}
}
and finally, method where Iam trying to get results:
public function index()
{
$survey = Forms::with('pages')->find(68);//Did update regarding to sugestion
dd($survey);
return view("pages.surveys", compact('survey'));
}
Thanks for any help.
Greg
I think you should reverse the order of the with() and find() functions.
$survey = Forms::with('pages')->find(68);
Note that find() actually executes the query, therefore you don't have to use get() again. As a rule of thumb: if you need to get only one result out of your query you should use first() or find() (the latter could also be used to retrieve collections), otherwise use all() or get() to fetch a collection of results.
$form = Forms::find(68);
dd($form->pages);
OR
$form = Form::find(68)->pages->get(); //It will return all the pages having relation
try this, It will first find form having id '68' then as per relation given it will fetch the records
Seems almost of your codes are okay. This might be a problem only with your foreign key. In your pages relationship in your forms, laravel will expect a foreign key in your pages table namely 'form_id' or 'forms_id' since you are not passing a second argument in your pages relationship. Make sure that in your pages migration your foreign key is set to to form_id also
when i do dd($survey);
App\Models\Forms {#513 ▼
#table: "forms"
#fillable: array:4 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"pages" => Illuminate\Database\Eloquent\Collection {#521 ▼
#items: array:6 [▼
0 => App\Models\Pages {#552 ▼
#table: "pages"
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => App\Models\Pages {#553 ▶}
2 => App\Models\Pages {#554 ▶}
3 => App\Models\Pages {#555 ▶}
4 => App\Models\Pages {#556 ▶}
5 => App\Models\Pages {#557 ▶}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
I have the following tables in my application:
projects
projects_plot_types
Projects can have many plot types. I have the following relationship setup in the projects model.
/**
* The plot types that belong to the project.
*
* #return Object
*/
public function plotTypes()
{
return $this->hasMany('App\Models\Project\ProjectsPlotTypes');
}
I want to be able to get the plot type by querying it's name on the project model.
I have tried this, but it does not work:
$project->with('plotTypes')->whereHas('plotTypes', function ($query) use ($row) {
$query->where('name', $row->plot_name);
})->first()->plotTypes->first()->id;
Can someone point me in the right direction?
The output of $result as per below comment is:
Project {#705 ▼
#table: "projects"
#fillable: array:7 [▶]
+timestamps: true
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:10 [▶]
#original: array:10 [▶]
#relations: array:1 [▼
"plotTypes" => Collection {#769 ▼
#items: array:1 [▼
0 => ProjectsPlotTypes {#774 ▼
#table: "projects_plot_types"
#fillable: array:2 [▶]
+timestamps: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:4 [▼
"id" => "2"
"project_id" => "1"
"name" => "TYPE 3 - VENTILATION"
"budget" => "245.69"
]
#original: array:4 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
You may also use join query like below
$result = Illuminate\Support\Facades\DB::table('projects')
->join('projects_plot_types', 'projects_plot_types.id', '=', 'projects.project_plot_id')
->where('projects_plot_types.name', '=', $row->plot_name)
->first();
print_r($result);
Maybe this is what you are looking for:
$result = $project->with(['plotTypes' => function($query) use ($row) {
return $query->where('name', $row->plot_name)->first();
}])->first();
dd($result); // print the result and die
UPDATE 1
Fetch the first item from the collection that has BelongsTo relationship:
$plotTypeResult = $result->plotTypes[0]->id;
I have a variable passed though my controller in my view :
name is $others
and when I do a foreach and print it like
#foreach($others as $other)
{{ $other }}
#endforeach
the output is :
[{"employeeID":"9125123981003","email":"admin#gaincafe.com","fullName":"Pranshu Jain"}] [{"employeeID":"45755757577","email":"yoyo#pranshu.com","fullName":"Pranshu Jain"}]
What I want is to access single properties from these ? Are they collections ?
I tried like this
#foreach($others as $other)
{{ $other->email }}
#endforeach
Here is how I get this $others filled :
public function show($id)
{
$this->data['leave_application'] = Attendance::find($id);
$date_applied_on = $this->data['leave_application']->date;
$emp_id = $this->data['leave_application']->employeeID;
$other_on_leave = Attendance::where('date', '=', $date_applied_on)->get();
$employees_on_leave = [];
foreach($other_on_leave as $others) {
if($emp_id != $others->employeeID) {
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName'));
$employees_on_leave[] = $emptyarray;
}
}
$this->data['others'] = $employees_on_leave;
return view('admin.leave_applications.show', $this->data);
}
But it does not work. Kindly help and guide a little bit. I am familiar with array and objects. I used to do well with them. but this seems little complicated.
If i do dd($others) then i get
array:2 [▼
0 => Collection {#344 ▼
#items: array:1 [▼
0 => Employee {#345 ▼
#guarded: array:1 [▼
0 => "id"
]
#hidden: array:1 [▼
0 => "password"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:3 [▶]
#original: array:3 [▶]
#relations: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
1 => Collection {#346 ▼
#items: array:1 [▼
0 => Employee {#347 ▼
#guarded: array:1 [▶]
#hidden: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:3 [▶]
#original: array:3 [▶]
#relations: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
Try first() instead of get() here:
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->first(array('employeeID','email','fullName'));
If you still wanted to use get(), then do as follow:
foreach($other_on_leave as $others) {
if($emp_id != $others->employeeID) {
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName'));
foreach($emptyarray as $arr){
$employees_on_leave[] = $arr;
}
}
}
And change this line also, so the view can understand what to loop through:
return view('admin.leave_applications.show', ['others' => $this->data['others', 'leave_application' => $this->data['leave_application']]);
Let me know if it worked.