Why is Eloquent is dumping an array on save()? - laravel

// Look for exact matches
foreach (Game::where("console_id", $id)->get() as $game) {
$search = Info::where("game_title", $game->name)->first();
if ($search) {
$game->info_id = $search->id;
$game->save();
continue;
}
When I run the following code, Laravel simply dumps $game on $game->save(). I've also tried Game::find($game->id)->update(["info_id",$search->id]) and it's behaving the same way. It does actually save before it dumps but it stops execution immediately after.
Unless I'm going crazy, this isn't the expected behavior. I've searched up and down and haven't found anyone else with this issue.
Anyone have any ideas? Thanks!

Related

How to show all data where date is earlier than today laravel

I`ve got code that shows all of the placements for a user. Is there a way to narrow this down to only show placements that are in the future? I've tried to do this using where and carbon::now to no avail.
My current code to show all of the placements :
$placements = Auth::user()->placementsAuthored;
$placements->load('keystage', 'subject', 'dates');
Placements Authored connection to connect a user to a placement :
public function placementsAuthored()
{
return $this->hasMany(Placement::class, 'author_id');
}
My attempt at trying to do this. I get no errors but the code doesn't work. It doesn't seem to take any effect of my where clause any ideas?
$placements ->where('date','>',Carbon::now()->format('Y-m-d'));
After a bit of tweaking, I found that this works but I don't understand why this works and the above doesn't. In my mind, they do the same but this is a longer way of doing it. Any idea why this works and the above doesn't?
// Only load future placements
$placements = Placement::whereHas( 'dates',
function ($q) {
$user_id = Auth::user()->id;
$q->where('author_id', $user_id)->where('date','>=' ,Carbon::now()->format('Y-m-d'));})->get();
You should take a different name for the date column because the date keyword is already reserved in the PHP function this is not a standard way

How to apply And where condition in orWhere?

I want to display data from cv table where user_id is $request->id.
Also, filtering is done so i have to put lots of orWhere.
Because of this my code is not working as expected.
Sorry question is a little bit confusing.
$search = $request->input('search.value');
$results = cv::with(['industrySegments','jobLocations','jobPositions','languages'])->where('user_id',$request->id);
$results->orWhere('name','LIKE',"%{$search}%");
$results->orWhere('gender','LIKE',$search);
$results->orWhere('contact','LIKE',$search);
$results->orWhere('contact2','LIKE',$search);
This code doesn't work.
whenever i type something in search button it displays data that is not supposed to be display.
I want to display only the data whose user_id is $request->id.
i think what you wanted to do is advanced where
your query will be like this
$results = cv::with(['industrySegments','jobLocations','jobPositions','languages'])->where('user_id',$request->id)->where(function($q)use($search){
$q->where('name','LIKE',"%{$search}%")->orWhere(..........;
})->get();
therefore you can play around with where / orWhere
Test next code:
$results = cv::with(['industrySegments','jobLocations','jobPositions','languages']);
if(!empty($request->id))
{
$results->where('user_id',$request->id);
}
else
{
$results->orWhere('name','LIKE',"%{$search}%");
$results->orWhere('gender','LIKE',$search);
$results->orWhere('contact','LIKE',$search);
$results->orWhere('contact2','LIKE',$search);
}

Laravel - Collection with relations take a lot of time

We are developing an API with LUMEN.
Today we had a confused problem with getting the collection of our "TimeLog"-model.
We just wanted to get all time logs with additional informationen from the board model and task model.
In one row of time log we had a board_id and a task_id. It is a 1:1 relation on both.
This was our first code for getting the whole data. This took a lot of time and sometimes we got a timeout:
BillingController.php
public function byYear() {
$timeLog = TimeLog::get();
$resp = array();
foreach($timeLog->toArray() as $key => $value) {
if(($timeLog[$key]->board_id && $timeLog[$key]->task_id) > 0 ) {
array_push($resp, array(
'board_title' => isset($timeLog[$key]->board->title) ? $timeLog[$key]->board->title : null,
'task_title' => isset($timeLog[$key]->task->title) ? $timeLog[$key]->task->title : null,
'id' => $timeLog[$key]->id
));
}
}
return response()->json($resp);
}
The TimeLog.php where the relation has been made.
public function board()
{
return $this->belongsTo('App\Board', 'board_id', 'id');
}
public function task()
{
return $this->belongsTo('App\Task', 'task_id', 'id');
}
Our new way is like this:
BillingController.php
public function byYear() {
$timeLog = TimeLog::
join('oc_boards', 'oc_boards.id', '=', 'oc_time_logs.board_id')
->join('oc_tasks', 'oc_tasks.id', '=', 'oc_time_logs.task_id')
->join('oc_users', 'oc_users.id', '=', 'oc_time_logs.user_id')
->select('oc_boards.title AS board_title', 'oc_tasks.title AS task_title','oc_time_logs.id','oc_time_logs.time_used_sec','oc_users.id AS user_id')
->getQuery()
->get();
return response()->json($timeLog);
}
We deleted the relation in TimeLog.php, cause we don't need it anymore. Now we have a load time about 1 sec, which is fine!
There are about 20k entries in the time log table.
My questions are:
Why is the first method out of range (what causes the timeout?)
What does getQuery(); exactly do?
If you need more information just ask me.
--First Question--
One of the issues you might be facing is having all those huge amount of data in memory, i.e:
$timeLog = TimeLog::get();
This is already enormous. Then when you are trying to convert the collection to array:
There is a loop through the collection.
Using the $timeLog->toArray() while initializing the loop based on my understanding is not efficient (I might not be entirely correct about this though)
Thousands of queries are made to retrieve the related models
So what I would propose are five methods (one which saves you from hundreds of query), and the last which is efficient in returning the result as customized:
Since you have many data, then chunk the result ref: Laravel chunk so you have this instead:
$timeLog = TimeLog::chunk(1000, function($logs){
foreach ($logs as $log) {
// Do the stuff here
}
});
Other way is using cursor (runs only one query where the conditions match) the internal operation of cursor as understood is using Generators.
foreach (TimeLog::where([['board_id','>',0],['task_id', '>', 0]])->cursor() as $timelog) {
//do the other stuffs here
}
This looks like the first but instead you have already narrowed your query down to what you need:
TimeLog::where([['board_id','>',0],['task_id', '>', 0]])->get()
Eager Loading would already present the relationship you need on the fly but might lead to more data in memory too. So possibly the chunk method would make things more easier to manage (even though you eagerload related models)
TimeLog::with(['board','task'], function ($query) {
$query->where([['board_id','>',0],['task_id', '>', 0]]);
}])->get();
You can simply use Transformer
With transformer, you can load related model, in elegant, clean and more controlled methods even if the size is huge, and one greater benefit is you can transform the result without having to worry about how to loop round it
You can simply refer to this answer in order to perform a simple use of it. However incase you don't need to transform your response then you can take other options.
Although this might not entirely solve the problem, but because the main issues you face is based on memory management, so the above methods should be useful.
--Second question--
Based on Laravel API here You could see that:
It simply returns the underlying query builder instance. To my observation, it is not needed based on your example.
UPDATE
For question 1, since it seems you want to simply return the result as response, truthfully, its more efficient to paginate this result. Laravel offers pagination The easiest of which is SimplePaginate which is good. The only thing is that it makes some few more queries on the database, but keeps a check on the last index; I guess it uses cursor as well but not sure. I guess finally this might be more ideal, having:
return TimeLog::paginate(1000);
I have faced a similar problem. The main issue here is that Elloquent is really slow doing massive task cause it fetch all the results at the same time so the short answer would be to fetch it row by row using PDO fetch.
Short example:
$db = DB::connection()->getPdo();
$query_sql = TimeLog::join('oc_boards', 'oc_boards.id', '=', 'oc_time_logs.board_id')
->join('oc_tasks', 'oc_tasks.id', '=', 'oc_time_logs.task_id')
->join('oc_users', 'oc_users.id', '=', 'oc_time_logs.user_id')
->select('oc_boards.title AS board_title', 'oc_tasks.title AS task_title','oc_time_logs.id','oc_time_logs.time_used_sec','oc_users.id AS user_id')
->toSql();
$query = $db->prepare($query->sql);
$query->execute();
$logs = array();
while ($log = $query->fetch()) {
$log_filled = new TimeLog();
//fill your model and push it into an array to parse it to json in future
array_push($logs,$log_filled);
}
return response()->json($logs);

Undefined property: Illuminate\Support\Collection::$election_name in view

In the controller, I store some data in an array.Then I want to send it in myelectionlist.blade.php
$my_election=[];
$i=0;
foreach ($election_list as $election_list)
{
$my_election[$i]=DB::table('election')
->where('id','=',$election_list->election_id)
->get();
$i++;
}
return view::make('myelectionlist')->with('election_list',$my_election);
I check with
return $my_election
It works fine. But In myelectionlist.blade.php when I write
#for($i=0;$i<sizeof($election_list);$i++)
{{$election_list[$i]->election_name}}
#endfor
It does not work.
Undefined property: Illuminate\Support\Collection::$election_name
happens, How to solve the problem?
There's a combination of problems that I would fix in order to bring clarity to your code and explain to you the problem. First of all in your code you have:
foreach ($election_list as $election_list)
I'll rename the variable for you to see:
foreach ($foo as $foo)
So what this does is that inside the foreach loop you will get all the values one by one, like expected but every time you store it to the initial variable. And once you are done with the foreach or break out of it earlier the $election_list will have the last used value. It might have unexpected results if you're trying to use the same $election_list later again. So I would suggest to use a differet variable names perhaps like $election_list as $election
Next, it's a little bit unclear why you would track the $index of the new elements by yourself. Instead you could just push into the array like this:
$my_election[] = $newObj
Actual error message:
Now for the actual error message: ->get() returns a Collection. So in the end you have an array full of Collections. So inside the for loop when you do $election_list[$i] - this will actually be a Collection object instead of the Model and thus the exception.
->get() will always get you the collection. For example with methods like ->first() and find/findOrFail you would get a single model. These functions might also be more appropriate to use since we are requesting with and id anyway.
Additionally, if this question is not a simplified version of your code then what I think you should actually do inside the controller:
$my_election = DB::table('election')
->whereIn('id', $election_list->pluck('election_id')) // pluck will give all the id's
->get();
This way you have a Collection of models assigned to $my_election.

Laravel, first() vs loop

I have an object on which I can run first() but I don't know how to loop through it.
$types = $biz->types()->first();
echo $types->name; //works great and gives me the name
This works fine but if I try:
$types = $biz->types(); //returns a BelongsToMany object
foreach($types as $type){
echo $type->name; //doesn't enter the loop at all
}
Edit: the following wouldn't work:
$biz->types; //returns an empty collection
This one doesn't give me anything and loop wouldn't initiate.
My question: How do I loop through $types and not just the first one?
PS: I wanted to focus on the loop and first so I didn't bring all the details.
You should access it as a property, which gives you access to the Collection of models. Accessing it as a method gives you QueryBuilder as a result.
foreach($biz->types as $type) {
...
}

Resources