Reputation: 1001
Must I do database migration to be able to do Eloquent Many to Many Relationship or can I do it without database migration?
My tables are;
k_p_user and fields are; id,name,datee
k_p_tree and fields are; id,user_id,bear_id,name,datee
k_p_bear and fields are; id,user_id,name,datee
k_p_picnic and fields are; id,user_id,bear_id,name,datee
I am using netbeans 8.0.2
This is my Model for k_p_picknic
<?php namespace App
use Illuminate\Database\Eloquent\Model;
class k_p_picnic extends Model{
}
?>
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\k_p_picnic;
use Illuminate\Support\Facades\Request as Request;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function fixoption($table){
$gender= k_p_picnic::all();
dd($gender)
return view('profile',['jj'=>$gender]);
}
}
?>
And this is my error
FatalErrorException in k_p_picnic.php line 8: syntax error, unexpected 'use' (T_USE), expecting \ (T_NS_SEPARATOR) or ';' or '{'
Upvotes: 2
Views: 148
Reputation: 1001
I have seen then error
In my Model
instead of <?php namespace App;
I wrote <?php namespace App
Thank you all for your beautiful answers especially dipesh and Andy Noelker. i now know that i can have a pre-existing database that migration has nothing to do with Eloquent
Upvotes: 0
Reputation: 11269
No, migrations are not necessary for Eloquent relationships. Migrations just provide an easy interface in PHP for creating and maintaining database tables. There is no issue if you already have pre-existing database tables or an alternative way of creating them.
If you are confused about Many To Many relationships, make sure to read up on the docs. For that relationship type, you will need a pivot table that contains the IDs for both models you want to connect. It appears as though your k_p_picnic
table is that. Then you need to define each relationship on the model object. You can include a table name if you are not following normal Laravel table naming conventions. In your case, you are not, so just pass it in as the second argument. For instance, your User model would have this method:
public function bears()
{
return $this->belongsToMany('App\Bear', 'k_p_picnic');
}
Upvotes: 2