SQLSTATE error[42S22]: Column not found: 1054 Unknown column '' Laravel project, table with two foreign keys

Asked

Viewed 770 times

0

Controller function for project voting has two foreign keys, user_id and projeto_idProjeto, follows function:

public function votar($id){    
        $usuario = Auth::user()->id;
        $votoss = Votos::where('projeto_idProjeto','=',$id)->where('users_id','=',$usuario)->first();
        if($votoss == null) {
           Votos::create([
          'users_id' => $usuario,
          'projeto_idProjeto' => $id
      ]);
          $projeto = Projetos::find($id);
          $projeto->numvotos += 1;
          if($projeto->numvotos == $projeto->metaVotos){
              $projeto->statusProjeto = 1;
          }
          $projeto->save();
        }
    return redirect ('/projetos');
}

Model Votos:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Votos extends Model
{
  protected $primaryKey = 'idVoto';
  protected $table = 'votos';
  protected $fillable = ['users_id','projeto_idProjeto'];

    public function projetos(){
        return $this->belongsTo('App\Projetos');
    }
    public function user(){
        return $this->belongsTo('App\User');
    }
}

Table votes:

Nome                 Tipo              Nulo     
idVoto               Primária          nao      
users_id             int(11)           nao              
projetos_idProjeto   int(11)           nao
created_at      timestamp               
updated_at      timestamp   
  • Put the Model Votos and also the table votos?

  • My dear post yes but I am looking for the site for question editing and have not found so far, srss

  • 1

    https://answall.com/posts/290211/edit is the link for editing and right after the tags, after sharing

  • Caraca mano a platform like this and the guys abandoned UX/UI totally , in moral could not see this menu below the tags, a gray font with white background , ta de sacanagem,rsrs

  • 1

    Faboanalist can make complaints, clearly explaining the problems in a legal way at https://pt.meta.stackoverflow.com/ ( that is the goal for discussions) , blz !!!

  • I will give that touch there , it is surely valid I will edit the question and then put the suggestion.

  • Typo: $votoss = Votos::where('projeto_idProjeto','=',$id)->where('users_id','=',$usuario)->first(); the field is projetos_idProjeto That’s what’s on your chart! something else their relationship does not work because it does not follow the nomenclature of the Laravel see links -> https://answall.com/questions/175842/salvar-related-11-no-laravel-5-3/175886#175886#https://answall.com/questions/173966/howto usehasmany-no-Laravel-5-2/173976#173976 and https://answall.com/questions/168781/salvar-varios-attributto-para-um-mesmo-objeto-no-laravel/168840#168840

  • When working with Laravel there are ways to configure the fields that do not follow the nomenclature of Eloquent, but in your case besides the typo have errors in the model because it is not configured.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.