创建添加后重定向到显示页面

时间:2018-02-24 17:53:55

标签: php laravel laravel-5

我有一个应用程序,用户可以通过商店路线创建一个pproperty添加。如果这是成功的,我想使用返回重定向来遵循控制显示页面的路线。路线是/ property / id

这些是商店,并显示控制器。

  public function store(Request $request, $id){
      $Advert = new PropertyAdvert();

      $Advert::create(
        [
          "photo"       => base64_encode(file_get_contents($request->photo->path())),
          "address"     => $request->address,
          "county"      => $request->county,
          "town"        => $request->town,
          "type"        => $request->type,
          "rent"        => $request->rent,
          "date"        => $request->date,
          "bedrooms"    => $request->bedrooms,
          "bathrooms"   => $request->bathrooms,
          "furnished"   => $request->furnished,
          "description" => $request->description
        ]
      );

    return redirect('/property/id');
    }

    public function show($id){
      $user = Auth::user();
    $Advert = PropertyAdvert::where('id', $id)->first();
    return view('pages/Advert/show', compact('Advert', 'user'));
  }

使用/ property / id似乎不起作用,就像在键入时在路径中一样。

路线     Route :: post('/ createadvert','AdvertisementController @ store');

Route::get('/property/{id}', 'AdvertisementController@show');

2 个答案:

答案 0 :(得分:0)

public function store(Request $request){
      $Advert = new PropertyAdvert();

      $new = $Advert::create(
        [
          "photo"       => base64_encode(file_get_contents($request->photo->path())),
          "address"     => $request->address,
          "county"      => $request->county,
          "town"        => $request->town,
          "type"        => $request->type,
          "rent"        => $request->rent,
          "date"        => $request->date,
          "bedrooms"    => $request->bedrooms,
          "bathrooms"   => $request->bathrooms,
          "furnished"   => $request->furnished,
          "description" => $request->description
        ]
      );

    return redirect('/property/'.$Advert->id);
    }

答案 1 :(得分:0)

<?php
public function store(Request $request){
    $Advert = PropertyAdvert::create([
        "photo"       => base64_encode(file_get_contents($request->photo->path())),
        "address"     => $request->address,
        "county"      => $request->county,
        "town"        => $request->town,
        "type"        => $request->type,
        "rent"        => $request->rent,
        "date"        => $request->date,
        "bedrooms"    => $request->bedrooms,
        "bathrooms"   => $request->bathrooms,
        "furnished"   => $request->furnished,
        "description" => $request->description
    ]);

    $id = $Advert->id;

    return redirect("/property/$id");
}

public function show($id){
    $user = Auth::user();
    $Advert = PropertyAdvert::where('id', $id)->first();
    return view('pages/Advert/show', compact('Advert', 'user'));
}

?>

:: create适用于类,而不适用于类实例。