Eloquent firstOrCreate documentation or usage - laravel

I'm attempting to seek a table to see if a column named "name" exists if so return the value and if not create that row with a null value i saw firstOrCreate but i cannot figure out how to use it for the life of me.
This is what i currently have, can someone lend a hand?
class Settings extends Eloquent
{
protected $table = 'settings';
protected $primaryKey = 'name';
public static function get($settingName)
{
return self::firstOrCreate(array('name' => $settingName));
// return self::select(array('value'))->where('name', '=', $settingName)->first()->value;
}
}

The create() method does mass assignment and this is a big security issue, so Laravel has a protection against it. Internally it has guarded = ['*'], so all your columns will be protected against mass assignment. You have some options:
Set the fillable columns of your model:
class User extends Eloquent {
protected $fillable = array('first_name', 'last_name', 'email');
}
Or set only the ones you want to keep guarded:
class User extends Eloquent {
protected $guarded = array('password');
}
You may, at your own risk also do:
class User extends Eloquent {
protected $guarded = array();
}
And everything will be unguarded.
Take a look a the docs: http://laravel.com/docs/eloquent#mass-assignment
You also could use your Facade to call it:
class Settings extends Eloquent
{
protected $table = 'settings';
protected $primaryKey = 'name';
public static function get($settingName)
{
return Settings::firstOrCreate(array('name' => $settingName));
}
}

Related

nested eager loading or hasManyThrough in laravel is not working

i want to get nama from Supplier table using PembelianDetail table through Pembelian table using this code :
$detail = PembelianDetail::with('supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();
with this model :
class Supplier extends Model
{
use HasFactory;
protected $table = 'supplier';
protected $primaryKey = 'id_supplier';
protected $guarded = [];
public function pembelian()
{
return $this->hasMany(Pembelian::class, 'id_pembelian', 'id_pembelian');
}
public function pembelian_detail(){
return $this->hasManyThrough(PembelianDetail::class, Pembelian::class );
}
}
class Pembelian extends Model
{
use HasFactory;
protected $table = 'pembelian';
protected $primaryKey = 'id_pembelian';
protected $guarded = [];
public function supplier()
{
return $this->belongsTo(Supplier::class, 'id_supplier', 'id_supplier');
}
public function pembelian_detail()
{
return $this->hasMany(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
}
}
class PembelianDetail extends Model
{
use HasFactory;
protected $table = 'pembelian_detail';
protected $primaryKey = 'id_pembelian_detail';
protected $guarded = [];
public function pembelian()
{
return $this->belongsTo(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
}
}
and keep getting this error :
Call to undefined relationship [supplier] on model [App\Models\PembelianDetail].
with the same way i dont understand how that works in this tutorial https://www.youtube.com/watch?v=5s-_SnVl-1g and i just followed the same steps but keep getting that error.
i also tried nested eager loading according laravel documentation by using :
$detail = PembelianDetail::with('pembelian.supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();
but get the same error and other queries works fine
Am i missing something here?

Laravel 5.6.17 Model hasOne over multiple tables

I'm quite new to Laravel and now I'm trying to move parts of a former application from a small self written framework into Laravel. The address book is multilingual therefor the table structure is a little more complicated.
db-structure
And this is my source code:
AddressBookController.php
namespace App\Http\Controllers;
use App\AddressBook as AB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class AddressBookController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$entries = AB::all();
return view('addressBook')->with([
'class' => __CLASS__,
'function' => __FUNCTION__,
'line' => __LINE__,
'entries' => $entries,
]);
}
}
Model AddressBook.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddressBook extends Model
{
protected $table = 'address';
protected $primaryKey = 'address_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
protected $searchable = [
'columns' => [
'address.address_surname' => 10,
'address.address_company' => 5,
'address.address_vatid' => 2,
],
];
public function country() {
return $this->hasOne('country', 'country_id', 'country_id');
}
public function addresstype() {
return $this->hasOne('addresstype', 'addresstype_id', 'addresstype_id');
}
}
Model Country.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $table = 'country';
protected $primaryKey = 'country_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function translation() {
return $this->hasOne('translations', 'translations_id', 'translations_id');
}
public function addressbook() {
return $this->belongsTo('address', 'country_id', 'country_id');
}
}
Model AddressType
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddressType extends Model
{
protected $table = 'addresstype';
protected $primaryKey = 'addresstype_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function translation() {
return $this->hasOne('translations', 'translations_id', 'translations_id');
}
public function addressbook() {
return $this->belongsTo('address', 'addresstype_id', 'addresstype_id');
}
}
Model Translation.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Translation extends Model
{
protected $table = 'translations';
protected $primaryKey = 'translations_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function country() {
return $this->belongsTo('country', 'translations_id', 'translations_id');
}
public function addresstype() {
return $this->belongsTo('addresstype', 'translations_id', 'translations_id');
}
}
The request "$entries = AB::all();" works in general but I get the id's and maybe I'm completely wrong here but I thought that data from foreign keys will be replaced with the respective models (if configured correctly). so my question is:
a. did I make a mistake during configuration and if yes, where exactly is the error?
or
b. is my assumption of replacing id's with objects is completly wrong?
Thanks in advance!
Steve
Laravel Eloquent models do not replace the foreign keys of the active records with the relational data, it only appends a new property with the same name as the method relating the classes and in that property it puts all the Model instances of the resulted query, this ONLY if you access the property, its called Eager Loading.
It is explained here (Ofiicial Documentation)
$addressBook = App\AddressBook::find(1); //this only will return the active record with the id 1 and nothig more.
$addressBook->country; // The property "country" does not exist in the AddressBook Classs but eloquent models will return a "fake" property with the value of the result of the query by the method with the same name (only if the method returns an Eloquent Relation).
This Eloquent behavior is natural, and is a very clever way to minimize the number of queries, Eloquent will never load a relation if there is no need to.
If you want to load a relation in a set the models at the same time that those models are retriving from the db you need to explicitly especify wich relations you want to load.
$addressBook = App\AddressBook::with(['country', 'addresstype', 'anotherRelation'])->get(); // this will retrive all the addressBook models and in each one will attach the relations specified.
[EDITED]
Also you have to place the entire namespace of the related model class in the relation methods so you need to replaced like:
class Translation extends Model
{
protected $table = 'translations';
protected $primaryKey = 'translations_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
// ****** You need to put the entire namespace of the Model class
public function country() {
return $this->belongsTo('App\Country', 'translations_id', 'translations_id');
}

Two foreign keys pointing to the same table / model

I have two models:
TRUCK
DRIVER
TRUCK has two fields which are FKs. Driver (FK) and Driver2 (FK).
When I try to get the truck with driver and driver2, I get two same records.
$truck = $this->instance->truck()->where('id', $id)
->with(['driver', 'driver2',])
->firstOrFail();
My Truck Model:
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver');
}
My Driver Model:
class Driver extends Model
{
use SoftDeletes;
protected $table = 'drivers';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function truck()
{
return $this->belongsTo('App\Models\Truck');
}
I am still new to laravel and something I get stuck. Should I create another model instead maybe?
By default laravel will use default foreign key,
Eloquent assumes the foreign key of the relationship based on the
model name #Further reading
So both relation are pointing to the same FK, So you need to specify the foreign key as below
return $this->hasOne('App\Models\Driver', 'Driver');
return $this->hasOne('App\Models\Driver', 'Driver2');
Full code
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver', 'Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver', 'Driver2');
}

laravel muti lavel association

I have 3 tables and these table are not according laravel
job_post Table
job_post_id
name
job_functional_area Table (has many with job_post table and belongsTo with functional_area)
job_functional_area_id
job_post_id
functional_area_id
functional_area
functional_area_id
name
now I want to fetch job_post functional area
Relation like this job_post > job_functional_area > functional_area
In cakephp we use contain in laravel i don't have an idea.
I have try Has Many Through but it will not work in this condition. Please help me
Models
class JobPost extends Model
{
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
}
Your models should look something like this.
class JobPost extends Model {
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
public function jobFunctionalAreas() {
return $this->belongsToMany('App\JobFunctionalArea');
}
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
public function jobPosts() {
return $this->hasMany('App\JobPost');
}
public function functionalArea() {
return $this->belongsTo('App\FunctionalArea');
}
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
public function jobFunctionalAreas() {
return $this->hasMany('App\JobFunctionalArea');
}
}
Also in the relations you can add your own custom $primaryKey.

Inverse relation returns null using Eloquent

I have a simple one-to-many relationship, where one authentication type has many client:
class Client extends Model {
protected $table = 'client';
protected $primaryKey = 'client_id';
protected $fillable = [
'client_id',
'auth_type_id',
];
public function auth()
{
return $this->belongsTo('App\AuthType', 'foreign_key', 'id');
}
}
class AuthType extends Model {
protected $table = 'authType';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'name'
];
public function users()
{
return $this->hasMany('App\User', 'foreign_key', 'auth_type_id');
}
}
So what I want is, to access the name of the authentication, when the $client is known.
$authType = $client->auth();
$authType->name;
And I get:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
How can I access the name property of Auth, if I have a Client ?
If you want to get the relation result you have to use
$authType = $client->auth;
If you use $client->auth() you'll get back a BelongsTo relation.

Resources