Laravel morpToMany issue - mysql

I have this problem, I try to connect movies and casts through morpToMany, but I get a strange error, I don't understand what I'm doing wrong.
Movie
public function casts()
{
return $this->morphToMany(Cast::class, 'movie_casts');
}
Cast
public function movies()
{
return $this->morphToMany(Movie::class, 'movie_casts');
}
MovieCast
public function movies()
{
return $this->morphedByMany(Movie::class, 'movie_casts');
}
public function casts()
{
return $this->morphedByMany(Cast::class, 'movie_casts');
}
movie_casts table
$table->unsignedBigInteger('cast_id')->nullable();
$table->foreign('cast_id')->references('id')->on('casts');
$table->unsignedBigInteger('movie_id')->nullable();
$table->foreign('movie_id')->references('id')->on('movies');
Controller
public function moviesList()
{
$movies = Movie::all();
foreach($movies as $movie){
$moviesWithCast = $movie->casts;
};
return response()->json(
$moviesWithCast,
200
);
}
Getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'movie_casts.movie_casts_id' in 'field list'

You are not using a morph relationship, it's a simple M2M relationship :
Movie
public function casts()
{
return $this->belongsToMany(Cast::class, 'movie_casts');
}
Cast
public function movies()
{
return $this->belongsToMany(Movie::class, 'movie_casts');
}
why would you take the morph approach ?

Related

Fixed updated_at error en Laravel Eloquent

I am trying to update a model in Laravel and I have missed the following error
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime
value: '2020-03-08 02:46:51' for column
deportetotal.news.updated_at at row 1 (SQL: update news set
views = 1, news.updated_at = 2020-03-08 02:46:51 where id =
86)
My class model look like that
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Jenssegers\Date\Date;
class News extends Model
{
protected $guarded=[];
public function getImage(){
return $this->hasOne('App\Image', 'id', 'image' );
}
public function tags(){
return $this->belongsToMany('App\Tag', 'news_tags', 'news_id', 'tags_id');
}
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
public function author()
{
return $this->belongsTo('App\User', 'user_id');
}
public function comments(){
return $this->hasMany('App\Comment', 'news_id');
}
public function getImagePathAttribute(){
return Storage::disk('public')->url($this->image);
}
public function getPublishDateAttribute(){
return new Date($this->pubDate);
}
}
And the fucntion controller
public function show($slug){
$news=News::where('slug',$slug)->first();
$news->views++;
$news->save();
return view('web.news.show', compact('news'));
}

cannot get the relationship data in laravel

this is my function
public function show($id)
{
$newproject = Newproject::find($id);
$data = [
'name' => $newproject->user->name
];
return $data;
}
newproject model
public function user()
{
return $this->hasOne(User::class,'user_id' ,'id');
}
it always getting the error
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.user_id' in 'where clause' (SQL: select * from `users` where `users`.`user_id` = 1 and `users`.`user_id` is not null limit 1)"
exception: "Illuminate\Database\QueryException"
file: "C:\xampp\htdocs\larastart\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line: 669
please help my to solve my problem please.
Check the relationship one-to-one reference
The foreign_key user_id is in newprojects table, you need to build the relationship like this:
User model:
public function newproject()
{
return $this->hasOne(Newproject::class,'user_id');
}
newproject model:
public function user()
{
return $this->belongsTo(User::class);
}
I think it should be something like this,
public function user()
{
return $this->hasOne(User::class,'id' ,'user_id');
}
It means id of users table match with current model's user_id column. And if you have proper naming convention according to laravel then you don't need to pass this extra params.
Laravel hasOne relation
Hope it helps:)
found the problem in my model
in user model
public function newproject()
{
return $this->belongsTo(Newproject::class,'user_id','id');
}
in newproject model
public function user()
{
return $this->hasOne(User::class,'id' , 'user_id');
}
thanks all

Column not found: 1054 Unknown column 'playlist_entity_assets.play_list_id&#039

I use three tables playlists, entityAssets, and playlistEntityAssets.
PlaylistEntityassets table has 2 columns playlist_id, asset_id referring to the other two tables. when I try to get data filter by playlist_id it gives me this error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'playlist_entity_assets.play_list_id' in 'where clause' (SQL: select * from playlist_entity_assets where playlist_entity_assets.play_list_id = 3 and playlist_entity_assets.play_list_id is not null)
this is my get method. even though my table has a "playlist_id" column. this query calls for the "play_list_id" column.
public function show($subdomain, $id) {
$universe = Auth::user()->getCurrentUniverse();
$playlist = PlayList::find($id);
//return empty responce
if( $playlist == null){
return Response::json();
}
$playlistAssets = array();
$playlistEntityAssets = $playlist->playlistEntityAssets;
foreach($playlistEntityAssets as $plEntityAsset){
$asset = $plEntityAsset->entityAsset;
$plAsset = ['asset' => $asset ];
$playlistAssets[] = $plAsset;
}
return Response::json($playlistAssets);
}
I want to fitler all enityAssets data filter by playlist_id in the "playlistEntityAssets" table.
use Illuminate\Database\Eloquent\Model;
class PlaylistEntityAssets extends Model
{
protected $table = 'playlist_entity_assets';
public function user()
{
return $this->belongsTo('asset');
}
public function universe()
{
return $this->belongsTo('Universe');
}
public function playlist(){
return $this->belongsTo('Playlist', 'playlist_id');
}
public function entityAsset(){
return $this->belongsTo('EntityAsset', 'asset_id');
}
I already fixed the problem. The answer is to put the exact column name in the playlist model.
class PlayList extends Eloquent{
public function playlistEntityAssets()
{
return $this->hasMany('PlaylistEntityAssets', 'playlist_id' );
}
}

How can i get value from 3 table Joined with FK in Blade view

table : projects
enter image description here
table : projects_departmentsenter image description here
table : departmentsenter image description here
This is Model Project
public function projects_departments() {
return $this->hasMany(Project_department::class);
}
This is Model Project_department
public function projects()
{
return $this->belongsTo(Project::class);
}
public function departments()
{
return $this->belongsTo(Department::class);
}
This is Model Department
public function projects()
{
return $this->hasMany(Project::class);
}
public function projects_departments() {
return $this->hasMany(Project_department::class);
}
this is my blade
project->departments->department_name
This is MY error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects_departments.project_id' in 'where clause' (SQL: select * from projects_departments where projects_departments.project_id = 48 and projects_departments.project_id is not null)
$project = DB::table('projects')
->select(*)
->join('Department', 'projects_departments.department_id', '=', 'Department.id')
->join('projects_departments', 'projects_departments.project_id', '=', 'projects.id')
->get();
let's try this one

Yii2 before Validate throw PHP Warning

I used beforeValidate($insert) function and it thrown a PHP Warning when I access my post listing page:
http://localhost/yiiapp/backend/web/index.php?r=post/index
PHP Warning – yii\base\ErrorException
Missing argument 1 for app\models\Post::beforeValidate(), called in /var/www/html/yiiapp/vendor/yiisoft/yii2/base/Model.php on line 341 and defined
but when I access my create page, this Exception gone away:
http://localhost/yiiapp/backend/web/index.php?r=post/create
Actually I want to assign value one of my attribute user_id before validation in Post Model.
Here is Post Model:
class Post extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'post';
}
public function beforeValidate($insert)
{
if (parent::beforeValidate($insert)) {
$this->user_id = Yii::$app->user->id;
return true;
}
return false;
}
---
}
Why this Exception?
How I can solve this issue?
According to doc http://www.yiiframework.com/doc-2.0/yii-base-model.html#beforeValidate()-detail method beforeValidate has no attributes. Use it this way:
public function beforeValidate()
{
if (parent::beforeValidate()) {
return true;
}
return false;
}