Laravel's uuid type - laravel

I was reading Laravel's documentation that I noticed Laravel 7 offers a uuid type in the page for database migrations
Unfortunately I couldn't find much information about using it in the documentation. How is such a column filled when I insert a new record? Does Laravel fill it on its own? Does Laravel guarantee it to be unique or should I worry about collisions? Can I use this field to create short links for the posts of a blog, for example?

Laravel should fill this on its own by setting the keyType(untested) on your model:
protected $keyType = 'string';
The key, whether you fill it manually or not, is unique as laravel depends on Ramsey.
To manually acquire a uuid you may use the provided helper:
use Illuminate\Support\Str;
return (string) Str::uuid();

Related

Query builder with Laravel using Eloquent

I am very new to Laravel and quite confuse with the model and database thing. I understand that a model represent one table. So I created a model using artisan command without migration and it created the code as follow.
namespace App;
use Illuminate\Database\Eloquent\Model;
class RegCars extends Model
{
//
}
Since I have a different table name, so I add protected $table = 'regcars'; and assume that this model can now access the table by running query such as RegCars::where('user_id', $user_id); from controller. But I wasn't able to get anything by running it.
So I am wondering, how does this model able to run the query? Is the migration needed in order to do this? Is there still other area I need to set before I can run any query?
highly recommend to read docs or at least watch a tutorial.
what you get is a query build not a model or a collection of models. laravel(eloquent) doesnt exactly know what you want so you query database then get the data by methods that laravel gives you if assume you want a RegCars you need a method like first:
RegCars::where('user_id', $user_id)->first(); // now you have model
more info: https://laravel.com/docs/8.x/eloquent

Laravel model find other column

Is it possible to overwrite the Laravel model 'find' function?
When I use model::find() it search in for the 'id' column but my table doens't have a id column but a SubscriptionId colum.
I know I can use: model::where('subscriptionId', $id) but my queued jobs are not working right now..
model::where('subscriptionId', $id)->first()
Make sure your subscriptionId is unique.
Let me know if my snippets code not working
Or
model::findOrFail($id, 'subscriptionId')
Find in Laravel searches the table based on the primary key. If you are not using id as your key, you can tell Laravel this within your model.
protected $primaryKey = 'subscriptionId';
This will tell Laravel to search this model based on the subscriptionId primary key instead of id. This should solve it for you. Docs on Primary Keys

Which relation to use in Laravel?

Which relation to use in Laravel to bind two table through third?
When Doctors can be assigned to some Centers. The intermediate table will be as:
doctor_id | center_id
How to create model in Laravel for this case?
You don't need a model for the intermediate table, simply use attach
Example:
$center = Center::create();
$doctor = Doctor::find(1);
$doctor->centers()->attach($doctor->id);
This is a very simple example but should give you the idea, of how to approach it.
All of it of course requires you have set up your Center and Doctor model with the correct many to many relations
Doctor.php model:
public function centers()
{
return $this->belongsToMany(Doctor::class);
}
See the documentation, for more information.
You could obviously create a model called DoctorsCenter and create it manually by doing this, whenever you want to attach a relation.
DoctorsCenter::create(['center_id' => $center->id, 'doctor_id' => $doctor->id]);
I don't see any good reason for doing this, and would not recommend it.
You can use hasMany or belongsTo relationship of Laravel.
See the laravel documentation, for more information

Where to process data in Laravel

Back when I was using CodeIgniter I had functions in my models like
public function GetArticlesFormatted($inactive = FALSE)
and then in my controller I could have had
$articles->GetArticlesFormatted(true);
And so on.
How should I achieve the same with Laravel 5.4? The database of the app I'm building is already built and full and is a mess so most of the "automated" super-restrictive things that Laravel has don't work out of the box.
For example there is a Country Code that I'm retrieving and I need it as is, but in some instances I need it converted in a Country Name which I had in another table.
Right now I just have in my controller wherever I am retrieving data from the model:
$countryResult = Country::where('country_code', $item['country_code'])->first();
$countryArray = $countryResult->toArray();
$item['country'] = $countryArray['country_name'];
Can I somehow achieve that in a more elegant way?
I tried accessors but for some reason couldn't get anything to work for my purposes.
A select query can be used to limit selection to a particular column.
$countryName = Country::select('country_name')
->where('country_code', $item['country_code'])
->first();

Can eloquent ignore irrelevant data in Laravel 4

I have a form that accepts data which will be used to create two new database table entries. The form takes both the users details and their address. The user details will be stored using the User::create(Input::all()) method into the users table, and the address details will be stored using the Address::create(Input::all()) method into the addresses table of the database.
The issue I'm currently having is that Eloquent is complaining that street, city, country etc do not exist on the users table. This is true, that data is to be used for the address side of things.
Is there any way to have eloquent ignore irrelevant data in the Input::all() array when it's passed to the create methods?
P.s. I'm aware that mass-assignment isn't a good idea, I'm only using it here to simplify my question.
Sure enough you can use $fillable array in your model to declare fields allowed for mass-assignment. I believe this is the most sufficient solution in your case.
class User extends Eloquent {
protected $fillable = [
'first_name',
'last_name',
'email'
];
}
Have you tried looking at Input::only('field1','field2',...);, or even Input::except('field3')? They should be able to accomplish what you are looking for.
Source: http://laravel.com/docs/requests
You'll have to unguard that model using these http://laravel.com/docs/eloquent#mass-assignment and then manually unset those values before you execute save(). I highly recommend using a form object or something similar to complete this kind of service for you outside of your model since it's safer and usually clearer to intended behavior.
#cheelahim is correct, When passing an array to Model::create(), all extra values that aren't in Model::fillable will be ignored.
I would however, STRONGLY RECOMMEND that you do not pass Input::all() to a model. You really should be validating and verifying the data before throwing it into a model.

Resources