laravel/mysql,user/favorite表有问题

odopli94  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(226)

有一个id,users\u id,firm\u id的favorite表(laravel/mysql)。我希望一个用户可以创建一个firm favorite,然后删除它(favorite)。我让事情在某种程度上工作,我的问题是,当一个用户使一个公司的最爱,下一个用户登录时,他没有选择使这同一个公司作为最爱,他可以只是取消它。
有点困惑,希望你能理解,并帮助我。
控制器

class favourite_controller extends Controller
{

    public function save_firm_favourite($firm_id){

        $user_id = auth::id();

        $save_favourite = new favourite;
        $save_favourite->user_id = $user_id;
        $save_favourite->firm_id = $firm_id;
        $save_favourite->save();

        return redirect()->back()->with('success' , 'Uspješno spremljeno u favorite');

    }

    public function remove_firm_favourite($firm_id){

        $remove_from_favourites = favourite::where('firm_id' , $firm_id);
        $remove_from_favourites->delete();

        return redirect()->back()->with('success' , 'Uspješno uklonjeno iz favorita');

    }

}

路线

Route::get('/save_to_favourites/{firm_id}' , 'favourite_controller@save_firm_favourite')->name('save_user_firm_favourite');
Route::get('/remove_from_favourites/{firm_id}' , 'favourite_controller@remove_firm_favourite')->name('remove_firm_favourite');

sql语句

id
user_id
firm_id
created_at
updated_at
vxf3dgd4

vxf3dgd41#

thx的兴趣,但我做了,我只需要做简单的查询数据库,这个查询就像过滤器

$user_id = auth::id();

    $show_fav_buttton = 0;
    $show_remove_fav_button = 1;

    $favourite_check =  DB::table('favourites')->where('user_id' , '=' , $user_id)->where('firm_id' , '=' , $id)->first();

    if(!$favourite_check){
        return view('opg')->with('my_opg' , $find_opg)
    ->with('favourite_check_show_fav_button' ,  $show_fav_buttton);

    }else{
        return view('opg')->with('my_opg' , $find_opg)
    ->with('favourite_check_show_remove_button' , $show_remove_fav_button );
    }

相关问题