what happend to getRelatedIds in Laravel 5.4 - laravel

I was using this function in 5.3 and now when I try to use it in 5.4 like this:
$post->tags()->getRelatedIds();
I am getting errors that function does not exist, I checked the documentation for 5.4 and it's not there anymore.
Anyone knows why this usefull function was removed and what I can do to get all ids from related model?

In Laravel 5.4 and 5.5 the getRelatedIds is replaced by allRelatedIds.
$post->tags()->allRelatedIds();

I can't speak for reasons why it has been removed but if you know the primary key name ahead of time (i.e. all your tables have an id column) you can simply do
$post->tags()->select('id')->pluck('id');
if you want a more generic way you'd need to jump through some hoops
$related = $post->tags();
$post->tags()
->select($related->getQualifiedKeyName())
->pluck($related->getKeyName());

Related

Laravel 5.3 - how do I set fetchmode in query builder?

I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel

Laravel Scout how refresh index with ElasticSearch?

I'm working on a Laravel project and I'm using https://laravel.com/docs/5.3/scout with ElasticSearch on a model Offer.
I already have some offers in my database, so I just run the command
`php artisan scout:import "App\Models\Offer"`
for generate the index for use my datas with ElasticSearch.
After that it's ok, I can search in my offers with, for example :
`$offers = Offer::search($request->keywords)->get();`
Now I have a function for create other offers in my database, and I dont know how can I refresh the index for use these new datas ?
In the documentation https://laravel.com/docs/5.3/scout#adding-records, I can read
all you need to do is save a model instance and it will automatically be added to your search index
I tried this and no, when I save() a new Offer, I can't find it in my index.
I tried to re run the command php artisan scout:import "App\Models\Offer" after add anew OFfer, but it's the same, I can't find it in my index.
Did I miss something ? Any ideas ?
Sorry for a late response on this but I ran into this issue myself when I attempted to use Scout. Everything went fine until I realized that the project's data would scale at a rate that went far beyond what scout could handle. In this case, however you can use the push() method instead of save(). I'm not sure why this isn't documented at all and it's rather frustrating but there's an answer at least.
So use:
->push()
instead of:
->save()
to update your indexes.
If that does not work for your specific version there is another way you can do it but it is "slightly" redundant. It involves a mix of using the queue system with the Artisan system and a command. In this you:
Create a queue/job php artisan make:job JobNameHere (As of Laravel 5.2 - 5.4)
Add use Artisan; to the top of that newly created Job Class so you can pull in the Artisan's functionality
In the handle of that Job Class add
class JobNameHere implements ShouldQueue {
...
...
public function handle() {
Artisan::call('scout:import', ['name' => "App\YourModelNameHere"]);
}
}
Add a dispatch call to that job class right after your DB push() process is called.
Example:
class YourController extends Controller {
public function yourUpdateMethod(Request $request) {
//Some code you wrote
//Some more code you wrote
$update_obj->push( $array_to_update_obj);
dispatch(new JobNameHere());
}
}
Test your index by searching on it
If all went well then you should no longer get any errors. Please leave a comment and let me know how it went... provided you're still having this issue.
I would also like to mention that Laravel Scout does not support ElasticSearch anymore as of August of 2016 (I believe). No one really knows why the support was removed but there are a few tutorials out there to help you get Laravel and Elastic search working together again.
I will also note, based on my research, that if your project is small then you should be fine to use Scout and not need to use ElasticSearch. If your project is going to get huge, then you're probably better off finding a package that supports and well documents how handle Laravel's relationships between models. Elastic search is capable of accomplishing this but there are tons of docs that make figuring it out difficult. Here are some semi-decent tutorials to help get you going down the right path.
https://tirdeamihai.com/blog/laravel-and-elasticsearch
https://laravel-news.com/laravel-and-elasticsearch
Plastic is a package that I would currently recommend as it's being actively worked on. Elasticquent hasn't been touched or updated since last year in June.
https://github.com/sleimanx2/plastic#1---create-a-new-index

New variable stripe_subscription required for Cashier with Laravel 5

I'm upgrading from Laravel 4.2 to Laravel 5.1. I have a Stripe account that works with Cashier in my Laravel 4.2 version that I built watching a Laracast on Stripe and Cashier. However, in Laravel 5.1 a new table column is required called "stripe_subscription." I tried researching the Stripe documentation, the Cashier documentation and the Laravel documentation, but couldn't find anything about what this variable is or does.
After simply adding it to my database with a new migration, it looks like Stripe is up and running again with Cashier in my Laravel 5.1 version. However, I was wondering if anyone knows what this variable does or where I can learn more about it. I just guessed that it should be a string, which I was really unhappy about having to guess, but it seemed to work.
Here is some code relevant to this variable from my project:
From my migration:
$table->string('stripe_subscription')->nullable();
From the Cashier file Billable.php:
public function setStripeSubscription($subscription_id)
{
$this->stripe_subscription = $subscription_id;
return $this;
}
public function getStripeSubscription()
{
return $this->stripe_subscription;
}
public function deactivateStripe()
{
$this->setStripeIsActive(false);
$this->stripe_subscription = null;
return $this;
}
I'm just noticing it does actually say that stripe_subscription is a string in the php file, but I'd rather see something in formal documentation about it. Further, as far as I can tell, this variable is unnecessary because there is also a "stripe_plan" variable. The "stripe_subscription" variable contains a random string, while the "stripe_plan" variable is the name of the plan, like "monthly" or "yearly."
If anyone can give me some guidance on where I can learn more about this variable and how to work with it, I'd appreciate it.

Paginate causes crashing in Laravel 4

I am learning about Laravel 4 and I'm trying its pagination. I created a simple query to test the pagination, yet it always end up hanging. If I use get(), it works fine, but when I replace get() with paginate(), it hangs. Here is my code:
DB::table("samp_tbl")
->select("id","activity")
->whereNull("deleted_at")
->orderBy("id","desc")
->paginate(5);
Could someone tell me what's wrong with my code?
In case anyone else comes across this issue it's because you are using orderBy. In the Note: area on this page http://laravel.com/docs/4.2/pagination#usage it explains that laravel has issues with groupBy. However I also would assume this would go for orderBy as well. Writing a custom query would be recommended in your case.
create a model for your database and it will work fine

Laravel 4: Fluent changes?

I read that Laravel 4 is not going to change much on the surface, saying that much of the old code would be compatible.
I try to use Fluent's insert_get_id like it says in the docs, but the function doesn't exist.
Am I doing it wrong? If not, are there more changes in Fluent and / or Eloquent?
Laravel 4 has been changed to become PSR-1 complaint and as such is now camelCased instead of snake_cased. Try changing to insertGetId().
I believe functions went from underscores to camelCase... try insertGetId

Resources