I have this error
SQLSTATE[42S02]: Base table or view not found: 1146 `Table 'skin_db.destination_destination_detail'` doesn't exist `(SQL: select * from `destination_destination_detail` where `destination_id` = 8)`
Where in fact I only have destinations and destination_details table. but the error is looking for destination_destination_detail table?
I really do not understand why.
Destination model
public function details(){
return $this->belongsToMany('App\Destination_detail');
}
Controller
$destination = Destination::find($req->destination_id);
$destination->details()->sync($req->provinces);
If you are using the relation many to many you must create another table it's name destination_destination_detail with columns:
destination_detail_id
destination_id
both of them are foreign key
Related
I have a Lumen app.
I just created a new TillSoftware model with this migration file
Schema::create('till_softwares', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name", 120);
$table->timestamps();
});
But when I just run a simple request like this
$softwareList = TillSoftware::all();
I receive an error telling
Next Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.till_software' doesn't exist
It doesn't seem to add the usual extra s at the end of the table name and trying to make a request to till_software instead of till_softwares.
I have more than 20 other models which aren't running just fine, and as far as I know plural of software is softwares.
Am I missing something obvious here?
I still can add an extra
protected $table = 'till_softwares';
on my model but I would prefer to understand what I did incorrectly.
Thank you for your help.
So laravel will use Str::plural() to find the plural of the model name to find the table name as this is standard.
>>> Str::plural('user')
=> "users"
So this is User model into users table
>>> Str::plural('software')
=> "software"
but the plural of software is software, so that is the table name.
If you want to use softwares you will need to do as you said
protected $table = 'till_softwares';
But I would say that using the standard table names would be best, so it should be till_software
I'm getting this error whenever i try to sync an array of inputs to a pivot table:
Illuminate\Database\QueryException
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "items_option_parcel"
violates foreign key constraint "items_option_id_fk_2971521" DETAIL: Key (items_option_id)=(0) is not present in table "items_options". (SQL: insert into "items_option_parcel" ("items_option_id", "parcel_id") values (0, 168))
here is a line of my controller:
$parcel->parcel_options()->sync($request->input('parcel_options', []));
function in the first model:
public function parcelOptionsParcels()
{
return $this->belongsToMany(Parcel::class);
}
function in the 2nd model:
public function parcel_options()
{
return $this->belongsToMany(ItemsOption::class);
}
I found out the issue, i checked my pluck() function, i forgot to pluck the items options ID with their SKUs, that's why every time it says a 0 id is not present in the table because it wasn't getting fetched at all.
I changed this:
$parcel_options = ItemsOption::all()>pluck('item_option_sku')>prepend(trans('global.pleaseSelect'), '');
to this
$parcel_options =
ItemsOption::all()->pluck('item_option_sku','id')->prepend(trans('global.pleaseSelect'), '');
I'm using Laravel Auditing Package to trace changes on my models.
I want to get a specific column of the foreign key in it's primary table before recording the events (create, update, delete) in the audit table.
There is a way the package helps get all the attribute of the foreign key, it's called Audit Transformation but it generates an error for me when displaying the details in the table i want to know if there's any eloquent model method to get the specific column info i need instead of using getattribute() method which gets the entire row of the item_id.
Audit Transformation Method
public function transformAudit(array $data): array
{
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'));
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'));
}
return $data;
}
This is how it's stores in the database ephasis on the item_name.
{"item_id":"1","qty_received":"2","delivered_by":"John",
"received_by":"Abi","supplier":"Stores","date":"2019-11-26","item_name":{"item_id":1,"item_name":"Toner","colour":"Black","status":"Active",
"created_at":"2018-10-25 17:55:26","updated_at":"2018-10-25 17:55:26"}}
And this is the Item table schema
So in my scenario i'd want to store the item_name as Toner not the entire row of the item_id
Any suggestion will be welcomed, thanks in advance.
Add ->item_name->item_name after the function of Find.
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'))->item_name->item_name;
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'))->item_name->item_name;
}
I'm trying to build an application on Laravel 5.3 where my models, database and controllers are in separate folders. I have the following folder structure:
Nitseditor
System
Controllers
Database
2016_12_28_130149_create_domains_table.php
2017_01_06_193355_create_themes_table.php
2017_01_07_140804_create_themes_domains_table.php
Models
Domain.php
Theme.php
I'm making a relationship in domain with many to many relationship i.e.
public function themes()
{
return $this->belongsToMany('Nitseditor\System\Models\Domain');
}
I've named the table domain_theme inside the 2017_01_07_140804_create_themes_domains_table.php
Now I'm trying to get the theme name which belongs to the domain in the controller something like this:
$flashmesage = new Domain;
foreach ($flashmesage->themes as $theme)
{
return $theme->theme_name;
}
I'm getting an error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'nitswebbuilder.domain_domain' doesn't exist (SQL: select domains.*, domain_domain.domain_id as pivot_domain_id from domains inner join domain_domain on domains.id = domain_domain.domain_id where domain_domain.domain_id is null and domains.deleted_at is null)
sorry for my short comment as answer... I have not enough reputation for comment,
change your themes() method to :
public function themes()
{
return $this->belongsToMany('Nitseditor\System\Models\Theme');
}
see Here for more info
Table name should be called domain_theme. If you've used corrent names for foreign keys and have built correct relationships, whereHas() will work for you:
$domainName = 'example.com';
$themes = Theme::whereHas('domains', function($q) ($domainName) {
$q->where('domain_name', $domainName);
})->get();
Then show all themes names:
#foreach ($themes as $theme)
{{ $theme->theme_name }}
#endforeach
I have create a relationship between user and table column, I want show the table list which is belongs to particular user. For example if user_id 1 is logged in the system, the system will only show the information belong to him which is Table 1.
This is my controller code :
public function show(Request $request){
$user_id=Auth::user()->id;
$table= Roundtable::findOrFail($user_id);
return view('users.tables.show')->withTables($table);
}
I know that $table= Roundtable::findOrFail($user_id); is incorrect but I had no idea how to do because I am new for laravel.
If user has just one table and if Roundtable model has user_id you can use this query:
Roundtable::where('user_id', $id)->first();
It will give you user's table or null if table doesn't exist.
Another way to get table is to use relation:
auth()->user()->roundTable;
Well i found the solution, just need to change the code into
$id=Auth::user()->id;
$table= Roundtable::where('user_id',$id)->get();
return view('users.tables.show')->withTables($table);
then the result will return correctly.