trying to execute the following query on the FriendController
$friends = Auth::User()->friends;
and that is the friends function on the User model
public function friends()
{
return $this->belongsToMany('App\Friend', 'friends', 'user_id', 'friend_id');
}
but on hitting the route i get the following error
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'friends' (SQL: select friends.*, friends.user_id as pivot_user_id, friends.friend_id as pivot_friend_id from friends inner join friends on friends.id = friends.friend_id where friends.user_id = 2)
Pivot tables should almost never have a model. If you're trying to use friends as a pivot table for two users, then the relationship should be referencing App\User, not App\Friend (or static which represents the current class).
public function friends()
{
return $this->belongsToMany(static::class, 'friends', 'user_id', 'friend_id');
}
You'd want to return a collection of users, not "friends" since friend isn't an entity, it's a relationship.
I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.id' in
'where clause' (SQL: select * from books where books.id =
98745632564 limit 1)
when I pass id value as id. I have column name bookID in my database but in the above error it is comparing books.id = 98745632564. I could not understand where book.id is coming from.
public function showBook($id){
$book = Book::findOrFail($id);
return $book;
}
The code works perfectly fine when I pass id value with the query as follows
public function showBook($id){
$book = Book::where('bookID', $id)->find();
return $book;
}
You should set:
protected $primaryKey = 'bookID';
in your Book model to make:
$book = Book::findOrFail($id);
version work.
Methods find or findOrFail are using primary key and this is by default set to id, so if you have any custom primary key, you should set it in your Eloquent model.
I got the following:
User, Role, with a role_user pivot table and a belongsToMany
relationship
User, Location, with a location_user pivot table and a belongsToMany relationship
There's 2 roles for the user: owner & gardener
Location has a 'gardeners_max' field
In model Location:
protected $appends = ['is_full'];
public function getIsFullAttribute()
{
return $this->attributes['name'] = $this->remainingGardeners() <= 0;
}
public function countGardeners()
{
return $this->gardeners()->count();
}
public function remainingGardeners()
{
return $this->gardeners_max - $this->countGardeners();
}
Now, doing that :
Location::all();
I get that :
[
{
name: 'LocationA',
gardeners_max: 3,
owners: [...],
garderners: [...]
...
is_full: false
}
]
which is cool. BUT... it's not possible to do a WHERE clause on the appended attribute.
Location::where('is_full',true)->get() // Unknown column 'is_full' in 'where clause'
So i'd like to write a join query so I can do a where clause on is_full
And I just can't find the way. Any help will be greatly appreciated!
IMPORTANT:
I know the filter() method to get the results but I need to do a single scopeQuery here
You could try to manipulate the Collection after loading the object from database:
Location::get()->where('is_full', true)->all();
(You have to use get first then all, not sure it works otherwise)
Not sure it's optimized thought.
You can make scope in your location model like this
public function scopeFull(Builder $query)
{
return $query->where('is_full', true);
}
Now you just get all location like this
Location::full()->get();
I have the following request:
$objects = Object::with("translate")->where(function ($query) use ($request) {
$query->language();
})->with('images')->with("category")->orderBy('id', 'desc')->paginate($limit);
So, in model Object there is method: translate:
public function translate()
{
return $this->hasMany("App\ObjectTranslate", "objectId", "id");
}
So, also in this model is:
public function scopeLanguage($query)
{
$languageHeader = new ModelLibrary();
return $query->where('language', '=', $languageHeader->getLanguage());
}
When I try to call scope in main request:
$query->language();
I get SQL error:
Column not found: 1054 Unknown column 'language' in 'where clause' (SQL:
You need to join the query with ObjectTranslate's table. your scopeLanguage() function will not work , since you are trying to access the column language which is not available in Object model column.
Note : With() will just eager loading, which will load the eloquent defined function, you cannot directly access their column in where clause.
Modify your scopeLanguage function
public function scopeLanguage($query)
{
$languageHeader = new ModelLibrary();
return $query->join('object_translate_table','object_translate_table.objectId','=','object_table.id')->where('object_translate_table.language',$languageHeader->getLanguage());
}
$objects = Object::with("translate","images","category")
->language()
->orderBy('id', 'desc')
->paginate($limit);
This should work
Assumin ObjectTranslate model's table name is object_translate_table and Object model's table name is object_table
I am trying to insert data into sql in laravel 5
My Code:
$a1=Input::get('username');
$a2=Input::get('reg_no');
DB::insert("INSERT INTO ngosignup VALUES($a1,$a2)");
But I am getting SQL error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'#gmial.com,a,a,a,a,9,a,9814937684,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,N'
at line 1 (SQL: INSERT INTO
ngosignup(name,email,pass,confirmpass,address,city,pin,state,mobile,registration,secretary,dest,firstmember,mobile1,secondmember,mobile2,thirdmember,mobile3,objective,dev1,place1,obj1,dev2,place2,obj2,dev3,place3,obj3)
VALUES(a,amit#gmial.com,a,a,a,a,9,a,9814937684,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
PLEASE HELP.
Try Eloquent way.. for example of using your db.. Make a Model for ngosignup table and include it on controller
public function create(\Illuminate\Http\Request $request) {
$input = $this->request->all();
$a1 = $input['username'];
$a2 = $input['reg_no'];
$newngo = new ngosignup; //Replace ngosignup with your Model name.
$newngo->name = $a1; //name and reg_no should be column name
$newngo->reg_no = $a2;
$newngo->save(); //this will add these values into your DB
}
You can do this way.. Easier and Flexible.. You can add validator.. Refer to Laravel docs on their official site :)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ngosignup;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ngosignupController extends Controller
{
public function index() {
$ngo = ngosignup::first(); // this shows first row of table..you can use get() instead of first to get all table as collection.
$data= array(
'ngo ' => $ngo
);
return View('index', $data);
}
Then on Index.Blade.php File.. you can display data from ngosignup table like
{{ $ngo->email }} // replace email with which data to display. if ollection.. use foreach loop to display all data.
Refer to Laravel docs.. they list all methods available..