Twittler
11 months ago

laravel display restaurants by user_id

Posted 11 months ago by Twittler

Hi guys, i need to display restaurants uploaded by user but i having some problems with finding user_id, with $restaurants = Restaurant::all(); works just fine but then showing all restaurants in db.

User Model:

class User extends Model implements Authenticatable {

protected $table = 'users';

protected $fillable = ['name', 'email', 'password', 'manager'];

use \Illuminate\Auth\Authenticatable;

public function restaurant() {

    return $this -> hasOne('App\Restaurant');
}

}

Restaurant Model :

class Restaurant extends Model {

protected $table = 'restaurant';

protected $fillable = ['name, address, delivery_method, payment_method'];

public function user() {

    return $this ->belongsTo('App\User', 'foreign_key');
}

}

Controller :

class RestaurantController extends Controller {

public function index() {

    $restaurants = Restaurant::all();

    return view('home', [
        'restaurants' => $restaurants
    ]);
}

Please sign in or create an account to participate in this conversation.