I want to add a new hotel in "Hotel" table, but the picture's path will be saved to "HotelPicture" table, which have tow column "hotel_id" & "picture"
Hotel model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Hotel extends Model {
protected $fillable = [
            'user_id',
            'name', 
            'description', 
            'phone', 
            'country_id', 
            'city_id', 
 ];
public function user()
{
    return $this->belongsTo('App\User');
}
public function images()
{
    return $this->hasMany(HotelPicture::class);
}
}
HotelController
public function storeHotel(AddHotelRequest $request) { $user = auth()->user(); $input = $request->all();
    $input['status'] = 0;
    $input['user_id'] = $user->id;
    if( $request->get('submit') == 'Publish' )
    {
        $input['status'] = 1;
    }
    Hotel::create($input);
    $photo = $request-> file('picture');
    if( $request->hasFile('picture') && $photo->isValid() ) {
        $fileName = $request->get('name') .'-' . time() . '.' . $photo->getClientOriginalExtension();
        $photo->move( public_path('uploads/hotels'), $fileName );
        $hotlepic['hotel_id'] = Hotel::id;
        $hotlepic['picture'] = asset('uploads/hotels/' . $fileName);
        HotelPicture::create($hotlepic);
    }
    session()->flash('success', 'Your hotel has been added successfully');
    return redirect()->back();
}
via Chebli Mohamed
 
Aucun commentaire:
Enregistrer un commentaire