(First of all sorry if my english is bad)... Well I'm trying to get the relationship from several tables wich are in a Many to Many structure and all I need to do is reading data using ajax requests and pass it to selection controls (or comboboxes) in the view, just that.
But I can't solve it, I'm stuck for almost two days with this and the presure is to high right now. I'm also new to laravel and also I'm new in this job so the code i'm using and the database structure was not written by me
The Database is defined this way: Services, Markets and Marketplace wich is the table that stores the relation between all of them. So there are..
Markets:
- market_code
- market_name
Services:
- service_code
- service_description
Marketplace:
- id
- market_code
- service_code
So when the user select the market it triggers an ajax request in js and using route it executes this in the controller:
use App\market;
use App\marketplace;
class MarketsControllerextends Controller
{
...
public function getMarkets(Request $request)
{
if ($request->ajax())
{
....//get $idRoute and $idMarket from request
$markets= marketplace::marketService($idRoute, $idMarket)
return response()->json($markets);
}
}
...
}
The intermediate table model:
...
namespace App;
use Illuminate\Database\Eloquent\Model;
class marketplace extends Model
{
...
public static function marketService($idRoute, $idMarket)
{
$result = marketplace ::where('route_id', '=', $idRoute)
->where('market_code', '=',$idMarket)
->get();
return $result;
}
}
The response return this json:
[{id: 832, customer_code: "1", market_code: "1", service_code: "1077", route_id: 18,…},…]
0:{id: 832, customer_code: "1", market_code: "1", service_code: "1077", route_id: 18,…}
customer_code:"1"
created_at: "2016-08-05 16:40:37"
id: 832
market_code: "1"
route_id: 18
service_code: "1077"
updated_at: "2016-08-05 16:40:37"
1:{id: 833, customer_code: "1", market_code: "1", service_code: "1078", route_id: 18,…}
customer_code: "1"
created_at: "2016-08-05 16:40:37"
id: 833
market_code: "1"
route_id: 18
service_code: "1078"
updated_at: "2016-08-05 16:40:37"
So far so good, but then I need to get the the service_description
for that service_code
in that json response in order to fill the selection control (combobox) in the view.
Basically i have tried the return $this->belongsToMany('App\market', 'marketplace');
using ->withPivot('...');
and ->wherePivot('...');
and the inverse relation in market model... but I cant achieve this and I have a lot of presure, so if you guys could help me to achive my goal and understand how laravel works I really will appreciate it so so so much!!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire